package net.minecraft.client.resources.language;

import org.apache.logging.log4j.LogManager;
import com.ibm.icu.text.ArabicShapingException;
import com.ibm.icu.text.Bidi;
import com.ibm.icu.text.ArabicShaping;
import java.util.regex.Matcher;
import java.io.InputStream;
import java.io.IOException;
import net.minecraft.server.packs.resources.Resource;
import java.util.Iterator;
import com.google.common.collect.ImmutableMap;
import java.io.FileNotFoundException;
import net.minecraft.resources.ResourceLocation;
import com.google.common.collect.Maps;
import java.util.List;
import net.minecraft.server.packs.resources.ResourceManager;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.logging.log4j.Logger;
import net.minecraft.locale.Language;

public class ClientLanguage extends Language {
    private static final Logger LOGGER;
    private static final Pattern FORMAT_PATTERN;
    private final Map<String, String> storage;
    private final boolean requiresReordering;
    
    private ClientLanguage(final Map<String, String> map, final boolean boolean2) {
        this.storage = map;
        this.requiresReordering = boolean2;
    }
    
    public static ClientLanguage loadFrom(final ResourceManager abc, final List<LanguageInfo> list) {
        final Map<String, String> map3 = Maps.newHashMap();
        boolean boolean4 = false;
        for (final LanguageInfo emr6 : list) {
            boolean4 |= emr6.isBidirectional();
            final String string7 = String.format("lang/%s.json", emr6.getCode());
            for (final String string8 : abc.getNamespaces()) {
                try {
                    final ResourceLocation uh10 = new ResourceLocation(string8, string7);
                    appendFrom(abc.getResources(uh10), map3);
                }
                catch (FileNotFoundException ex) {}
                catch (Exception exception10) {
                    ClientLanguage.LOGGER.warn("Skipped language file: {}:{} ({})", string8, string7, exception10.toString());
                }
            }
        }
        return new ClientLanguage(ImmutableMap.copyOf(map3), boolean4);
    }
    
    private static void appendFrom(final List<Resource> list, final Map<String, String> map) {
        for (final Resource abb4 : list) {
            try (final InputStream inputStream5 = abb4.getInputStream()) {
                Language.loadFromJson(inputStream5, map::put);
            }
            catch (IOException iOException5) {
                ClientLanguage.LOGGER.warn("Failed to load translations from {}", abb4, iOException5);
            }
        }
    }
    
    @Override
    public String getOrDefault(final String string) {
        return this.storage.getOrDefault(string, string);
    }
    
    @Override
    public boolean has(final String string) {
        return this.storage.containsKey(string);
    }
    
    @Override
    public boolean requiresReordering() {
        return this.requiresReordering;
    }
    
    @Override
    public String reorder(String string, final boolean boolean2) {
        if (!this.requiresReordering) {
            return string;
        }
        if (boolean2 && string.indexOf(37) != -1) {
            string = wrapFormatCodes(string);
        }
        return this.reorder(string);
    }
    
    public static String wrapFormatCodes(final String string) {
        final Matcher matcher2 = ClientLanguage.FORMAT_PATTERN.matcher(string);
        final StringBuffer stringBuffer3 = new StringBuffer();
        int integer4 = 1;
        while (matcher2.find()) {
            final String string2 = matcher2.group(1);
            final String string3 = (string2 != null) ? string2 : Integer.toString(integer4++);
            final String string4 = matcher2.group(2);
            final String string5 = Matcher.quoteReplacement("\u2066%" + string3 + "$" + string4 + "\u2069");
            matcher2.appendReplacement(stringBuffer3, string5);
        }
        matcher2.appendTail(stringBuffer3);
        return stringBuffer3.toString();
    }
    
    private String reorder(final String string) {
        try {
            final Bidi bidi3 = new Bidi(new ArabicShaping(8).shape(string), 127);
            bidi3.setReorderingMode(0);
            return bidi3.writeReordered(10);
        }
        catch (ArabicShapingException ex) {
            return string;
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
        FORMAT_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z])");
    }
}
