package fd;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.LocaleList;
import androidx.annotation.Nullable;
import com.google.android.material.textfield.w;
import dd.k;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
public final class b {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final k f26044a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Context f26045b;

    public b(Context context, k kVar) {
        a aVar = new a(this);
        this.f26045b = context;
        this.f26044a = kVar;
        kVar.setLocalizationMessageHandler(aVar);
    }

    public static Locale a(String str) {
        String str2;
        String[] strArrSplit = str.replace('_', '-').split("-", -1);
        String str3 = strArrSplit[0];
        String str4 = "";
        int i10 = 1;
        if (strArrSplit.length <= 1 || strArrSplit[1].length() != 4) {
            str2 = "";
        } else {
            str2 = strArrSplit[1];
            i10 = 2;
        }
        if (strArrSplit.length > i10 && strArrSplit[i10].length() >= 2 && strArrSplit[i10].length() <= 3) {
            str4 = strArrSplit[i10];
        }
        return new Locale(str3, str4, str2);
    }

    public final void b(Configuration configuration) {
        ArrayList<Locale> arrayList = new ArrayList();
        LocaleList locales = configuration.getLocales();
        int size = locales.size();
        for (int i10 = 0; i10 < size; i10++) {
            arrayList.add(locales.get(i10));
        }
        k kVar = this.f26044a;
        kVar.getClass();
        ArrayList arrayList2 = new ArrayList();
        for (Locale locale : arrayList) {
            locale.getLanguage();
            locale.getCountry();
            locale.getVariant();
            arrayList2.add(locale.getLanguage());
            arrayList2.add(locale.getCountry());
            arrayList2.add(locale.getScript());
            arrayList2.add(locale.getVariant());
        }
        kVar.f25122a.invokeMethod("setLocale", arrayList2);
    }

    @Nullable
    public Locale resolveNativeLocale(@Nullable List<Locale> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        int i10 = Build.VERSION.SDK_INT;
        Context context = this.f26045b;
        if (i10 < 26) {
            LocaleList locales = context.getResources().getConfiguration().getLocales();
            for (int i11 = 0; i11 < locales.size(); i11++) {
                Locale locale = locales.get(i11);
                for (Locale locale2 : list) {
                    if (locale.equals(locale2)) {
                        return locale2;
                    }
                }
                for (Locale locale3 : list) {
                    if (locale.getLanguage().equals(locale3.toLanguageTag())) {
                        return locale3;
                    }
                }
                for (Locale locale4 : list) {
                    if (locale.getLanguage().equals(locale4.getLanguage())) {
                        return locale4;
                    }
                }
            }
            return list.get(0);
        }
        ArrayList arrayList = new ArrayList();
        LocaleList locales2 = context.getResources().getConfiguration().getLocales();
        int size = locales2.size();
        for (int i12 = 0; i12 < size; i12++) {
            Locale locale5 = locales2.get(i12);
            String language = locale5.getLanguage();
            if (!locale5.getScript().isEmpty()) {
                StringBuilder sbD = u.a.d(language, "-");
                sbD.append(locale5.getScript());
                language = sbD.toString();
            }
            if (!locale5.getCountry().isEmpty()) {
                StringBuilder sbD2 = u.a.d(language, "-");
                sbD2.append(locale5.getCountry());
                language = sbD2.toString();
            }
            arrayList.add(w.n(language));
            w.p();
            arrayList.add(w.n(locale5.getLanguage()));
            w.p();
            arrayList.add(w.n(locale5.getLanguage() + "-*"));
        }
        Locale localeLookup = Locale.lookup(arrayList, list);
        return localeLookup != null ? localeLookup : list.get(0);
    }
}
