package com.revenuecat.purchases.ui.revenuecatui.components.ktx;

import E3.v;
import I0.C0307p;
import S4.p;
import S4.w;
import b1.C1186a;
import com.revenuecat.purchases.paywalls.components.common.LocaleId;
import com.revenuecat.purchases.paywalls.components.common.LocalizationData;
import com.revenuecat.purchases.paywalls.components.common.LocalizationKey;
import com.revenuecat.purchases.paywalls.components.properties.ThemeImageUrls;
import com.revenuecat.purchases.paywalls.components.properties.ThemeVideoUrls;
import com.revenuecat.purchases.ui.revenuecatui.errors.PaywallValidationError;
import com.revenuecat.purchases.ui.revenuecatui.helpers.NonEmptyList;
import com.revenuecat.purchases.ui.revenuecatui.helpers.NonEmptyListKt;
import com.revenuecat.purchases.ui.revenuecatui.helpers.NonEmptyMap;
import com.revenuecat.purchases.ui.revenuecatui.helpers.NonEmptyMapKt;
import com.revenuecat.purchases.ui.revenuecatui.helpers.Result;
import com.revenuecat.purchases.ui.revenuecatui.helpers.ResultKt;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import kotlin.jvm.internal.o;
import n2.AbstractC2612c;
import w4.k;
import x4.AbstractC3229F;
import x4.AbstractC3253s;
import x4.y;
import y4.C3274b;

/* JADX INFO: loaded from: classes3.dex */
public final class LocalizationKt {
    private static final Map<String, String> scriptByRegion = AbstractC3229F.w(new k("CN", "Hans"), new k("SG", "Hans"), new k("MY", "Hans"), new k("TW", "Hant"), new k("HK", "Hant"), new k("MO", "Hant"));

    /* JADX INFO: renamed from: getBestMatch-64pKzr8, reason: not valid java name */
    public static final /* synthetic */ Object m683getBestMatch64pKzr8(Map getBestMatch, String localeId) {
        o.h(getBestMatch, "$this$getBestMatch");
        o.h(localeId, "localeId");
        String strM684getBestMatch64pKzr8 = m684getBestMatch64pKzr8(getBestMatch.keySet(), localeId);
        if (strM684getBestMatch64pKzr8 != null) {
            return getBestMatch.get(LocaleId.m571boximpl(strM684getBestMatch64pKzr8));
        }
        return null;
    }

    private static final List<Locale> getFallbackLanguages(Locale locale) {
        String language = locale.getLanguage();
        return o.c(language, "nb") ? AbstractC2612c.q(new Locale("no", locale.getCountry(), locale.getVariant())) : o.c(language, "nn") ? AbstractC2612c.q(new Locale("no", locale.getCountry(), locale.getVariant())) : y.f26065a;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX INFO: renamed from: image-7v81vok, reason: not valid java name */
    public static final /* synthetic */ Result m685image7v81vok(NonEmptyMap image, String key) {
        o.h(image, "$this$image");
        o.h(key, "key");
        Object obj = image.get(LocalizationKey.m607boximpl(key));
        String str = null;
        Object[] objArr = 0;
        ThemeImageUrls themeImageUrlsM586unboximpl = obj instanceof LocalizationData.Image ? ((LocalizationData.Image) obj).m586unboximpl() : null;
        return themeImageUrlsM586unboximpl != null ? new Result.Success(themeImageUrlsM586unboximpl) : new Result.Error(new PaywallValidationError.MissingImageLocalization(key, str, 2, objArr == true ? 1 : 0));
    }

    /* JADX INFO: renamed from: imageForAllLocales-7v81vok, reason: not valid java name */
    public static final Result m686imageForAllLocales7v81vok(NonEmptyMap imageForAllLocales, String key) {
        o.h(imageForAllLocales, "$this$imageForAllLocales");
        o.h(key, "key");
        Map map = imageForAllLocales.all;
        LinkedHashMap linkedHashMap = new LinkedHashMap(AbstractC3229F.u(map.size()));
        for (Map.Entry entry : map.entrySet()) {
            Object key2 = entry.getKey();
            String strM577unboximpl = ((LocaleId) entry.getKey()).m577unboximpl();
            Result resultM685image7v81vok = m685image7v81vok((NonEmptyMap) entry.getValue(), key);
            if (!(resultM685image7v81vok instanceof Result.Success)) {
                if (!(resultM685image7v81vok instanceof Result.Error)) {
                    throw new v();
                }
                resultM685image7v81vok = new Result.Error(NonEmptyListKt.nonEmptyListOf(new PaywallValidationError.MissingImageLocalization(key, strM577unboximpl, null), new PaywallValidationError.MissingImageLocalization[0]));
            }
            linkedHashMap.put(key2, resultM685image7v81vok);
        }
        NonEmptyMap nonEmptyMapNonEmptyMapOf = NonEmptyMapKt.nonEmptyMapOf(new k(imageForAllLocales.getEntry().getKey(), AbstractC3229F.t(imageForAllLocales.getEntry().getKey(), linkedHashMap)), linkedHashMap);
        LinkedHashMap linkedHashMap2 = new LinkedHashMap();
        ArrayList arrayList = new ArrayList();
        Result success = (Result) nonEmptyMapNonEmptyMapOf.getEntry().getValue();
        if (success instanceof Result.Success) {
            success = new Result.Success((ThemeImageUrls) ((Result.Success) success).getValue());
        } else if (!(success instanceof Result.Error)) {
            throw new v();
        }
        if (success instanceof Result.Success) {
            success = new Result.Success(new k(nonEmptyMapNonEmptyMapOf.getEntry().getKey(), ((Result.Success) success).getValue()));
        } else if (!(success instanceof Result.Error)) {
            throw new v();
        }
        if (!(success instanceof Result.Success) && (success instanceof Result.Error)) {
            arrayList.addAll((NonEmptyList) ((Result.Error) success).getValue());
        }
        Iterator it = nonEmptyMapNonEmptyMapOf.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry2 = (Map.Entry) it.next();
            Object key3 = entry2.getKey();
            Result result = (Result) entry2.getValue();
            if (result instanceof Result.Success) {
                if (arrayList.isEmpty() && !o.c(key3, nonEmptyMapNonEmptyMapOf.getEntry().getKey())) {
                    linkedHashMap2.put(key3, (ThemeImageUrls) ((Result.Success) result).getValue());
                }
            } else if (result instanceof Result.Error) {
                arrayList.addAll((Collection) ((Result.Error) result).getValue());
            }
        }
        NonEmptyList nonEmptyListOrNull = NonEmptyListKt.toNonEmptyListOrNull(arrayList);
        return nonEmptyListOrNull != null ? new Result.Error(nonEmptyListOrNull) : new Result.Success(NonEmptyMapKt.nonEmptyMapOf((k) ResultKt.getOrThrow(success), linkedHashMap2));
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX INFO: renamed from: string-7v81vok, reason: not valid java name */
    public static final /* synthetic */ Result m687string7v81vok(NonEmptyMap string, String key) {
        o.h(string, "$this$string");
        o.h(key, "key");
        Object obj = string.get(LocalizationKey.m607boximpl(key));
        String str = null;
        Object[] objArr = 0;
        String strM595unboximpl = obj instanceof LocalizationData.Text ? ((LocalizationData.Text) obj).m595unboximpl() : null;
        return strM595unboximpl != null ? new Result.Success(strM595unboximpl) : new Result.Error(new PaywallValidationError.MissingStringLocalization(key, str, 2, objArr == true ? 1 : 0));
    }

    /* JADX INFO: renamed from: stringForAllLocales-7v81vok, reason: not valid java name */
    public static final Result m688stringForAllLocales7v81vok(NonEmptyMap stringForAllLocales, String key) {
        o.h(stringForAllLocales, "$this$stringForAllLocales");
        o.h(key, "key");
        Map map = stringForAllLocales.all;
        LinkedHashMap linkedHashMap = new LinkedHashMap(AbstractC3229F.u(map.size()));
        for (Map.Entry entry : map.entrySet()) {
            Object key2 = entry.getKey();
            String strM577unboximpl = ((LocaleId) entry.getKey()).m577unboximpl();
            Result resultM687string7v81vok = m687string7v81vok((NonEmptyMap) entry.getValue(), key);
            if (!(resultM687string7v81vok instanceof Result.Success)) {
                if (!(resultM687string7v81vok instanceof Result.Error)) {
                    throw new v();
                }
                resultM687string7v81vok = new Result.Error(NonEmptyListKt.nonEmptyListOf(new PaywallValidationError.MissingStringLocalization(key, strM577unboximpl, null), new PaywallValidationError.MissingStringLocalization[0]));
            }
            linkedHashMap.put(key2, resultM687string7v81vok);
        }
        NonEmptyMap nonEmptyMapNonEmptyMapOf = NonEmptyMapKt.nonEmptyMapOf(new k(stringForAllLocales.getEntry().getKey(), AbstractC3229F.t(stringForAllLocales.getEntry().getKey(), linkedHashMap)), linkedHashMap);
        LinkedHashMap linkedHashMap2 = new LinkedHashMap();
        ArrayList arrayList = new ArrayList();
        Result success = (Result) nonEmptyMapNonEmptyMapOf.getEntry().getValue();
        if (success instanceof Result.Success) {
            success = new Result.Success((String) ((Result.Success) success).getValue());
        } else if (!(success instanceof Result.Error)) {
            throw new v();
        }
        if (success instanceof Result.Success) {
            success = new Result.Success(new k(nonEmptyMapNonEmptyMapOf.getEntry().getKey(), ((Result.Success) success).getValue()));
        } else if (!(success instanceof Result.Error)) {
            throw new v();
        }
        if (!(success instanceof Result.Success) && (success instanceof Result.Error)) {
            arrayList.addAll((NonEmptyList) ((Result.Error) success).getValue());
        }
        Iterator it = nonEmptyMapNonEmptyMapOf.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry2 = (Map.Entry) it.next();
            Object key3 = entry2.getKey();
            Result result = (Result) entry2.getValue();
            if (result instanceof Result.Success) {
                if (arrayList.isEmpty() && !o.c(key3, nonEmptyMapNonEmptyMapOf.getEntry().getKey())) {
                    linkedHashMap2.put(key3, (String) ((Result.Success) result).getValue());
                }
            } else if (result instanceof Result.Error) {
                arrayList.addAll((Collection) ((Result.Error) result).getValue());
            }
        }
        NonEmptyList nonEmptyListOrNull = NonEmptyListKt.toNonEmptyListOrNull(arrayList);
        return nonEmptyListOrNull != null ? new Result.Error(nonEmptyListOrNull) : new Result.Success(NonEmptyMapKt.nonEmptyMapOf((k) ResultKt.getOrThrow(success), linkedHashMap2));
    }

    public static final /* synthetic */ C1186a toComposeLocale(Locale locale) {
        o.h(locale, "<this>");
        String languageTag = locale.toLanguageTag();
        o.g(languageTag, "toLanguageTag()");
        return new C1186a(languageTag);
    }

    /* JADX INFO: renamed from: toComposeLocale-_KYeFs0, reason: not valid java name */
    public static final /* synthetic */ C1186a m689toComposeLocale_KYeFs0(String toComposeLocale) {
        o.h(toComposeLocale, "$this$toComposeLocale");
        return new C1186a(w.x0(toComposeLocale, '_', '-'));
    }

    public static final Locale toJavaLocale(C1186a c1186a) {
        o.h(c1186a, "<this>");
        Locale localeForLanguageTag = Locale.forLanguageTag(c1186a.f13226a.toLanguageTag());
        o.g(localeForLanguageTag, "forLanguageTag(toLanguageTag())");
        return localeForLanguageTag;
    }

    /* JADX INFO: renamed from: toJavaLocale-_KYeFs0, reason: not valid java name */
    public static final /* synthetic */ Locale m690toJavaLocale_KYeFs0(String toJavaLocale) {
        o.h(toJavaLocale, "$this$toJavaLocale");
        Locale localeForLanguageTag = Locale.forLanguageTag(w.x0(toJavaLocale, '_', '-'));
        o.g(localeForLanguageTag, "forLanguageTag(value.replace('_', '-'))");
        return localeForLanguageTag;
    }

    public static final String toLocaleId(C1186a c1186a) {
        o.h(c1186a, "<this>");
        return LocaleId.m572constructorimpl(w.x0(c1186a.f13226a.toLanguageTag(), '-', '_'));
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX INFO: renamed from: video-7v81vok, reason: not valid java name */
    public static final /* synthetic */ Result m691video7v81vok(NonEmptyMap video, String key) {
        o.h(video, "$this$video");
        o.h(key, "key");
        Object obj = video.get(LocalizationKey.m607boximpl(key));
        String str = null;
        Object[] objArr = 0;
        ThemeVideoUrls themeVideoUrlsM604unboximpl = obj instanceof LocalizationData.Video ? ((LocalizationData.Video) obj).m604unboximpl() : null;
        return themeVideoUrlsM604unboximpl != null ? new Result.Success(themeVideoUrlsM604unboximpl) : new Result.Error(new PaywallValidationError.MissingVideoLocalization(key, str, 2, objArr == true ? 1 : 0));
    }

    /* JADX INFO: renamed from: videoForAllLocales-7v81vok, reason: not valid java name */
    public static final Result m692videoForAllLocales7v81vok(NonEmptyMap videoForAllLocales, String key) {
        o.h(videoForAllLocales, "$this$videoForAllLocales");
        o.h(key, "key");
        Map map = videoForAllLocales.all;
        LinkedHashMap linkedHashMap = new LinkedHashMap(AbstractC3229F.u(map.size()));
        for (Map.Entry entry : map.entrySet()) {
            Object key2 = entry.getKey();
            String strM577unboximpl = ((LocaleId) entry.getKey()).m577unboximpl();
            Result resultM691video7v81vok = m691video7v81vok((NonEmptyMap) entry.getValue(), key);
            if (!(resultM691video7v81vok instanceof Result.Success)) {
                if (!(resultM691video7v81vok instanceof Result.Error)) {
                    throw new v();
                }
                resultM691video7v81vok = new Result.Error(NonEmptyListKt.nonEmptyListOf(new PaywallValidationError.MissingVideoLocalization(key, strM577unboximpl, null), new PaywallValidationError.MissingVideoLocalization[0]));
            }
            linkedHashMap.put(key2, resultM691video7v81vok);
        }
        NonEmptyMap nonEmptyMapNonEmptyMapOf = NonEmptyMapKt.nonEmptyMapOf(new k(videoForAllLocales.getEntry().getKey(), AbstractC3229F.t(videoForAllLocales.getEntry().getKey(), linkedHashMap)), linkedHashMap);
        LinkedHashMap linkedHashMap2 = new LinkedHashMap();
        ArrayList arrayList = new ArrayList();
        Result success = (Result) nonEmptyMapNonEmptyMapOf.getEntry().getValue();
        if (success instanceof Result.Success) {
            success = new Result.Success((ThemeVideoUrls) ((Result.Success) success).getValue());
        } else if (!(success instanceof Result.Error)) {
            throw new v();
        }
        if (success instanceof Result.Success) {
            success = new Result.Success(new k(nonEmptyMapNonEmptyMapOf.getEntry().getKey(), ((Result.Success) success).getValue()));
        } else if (!(success instanceof Result.Error)) {
            throw new v();
        }
        if (!(success instanceof Result.Success) && (success instanceof Result.Error)) {
            arrayList.addAll((NonEmptyList) ((Result.Error) success).getValue());
        }
        Iterator it = nonEmptyMapNonEmptyMapOf.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry2 = (Map.Entry) it.next();
            Object key3 = entry2.getKey();
            Result result = (Result) entry2.getValue();
            if (result instanceof Result.Success) {
                if (arrayList.isEmpty() && !o.c(key3, nonEmptyMapNonEmptyMapOf.getEntry().getKey())) {
                    linkedHashMap2.put(key3, (ThemeVideoUrls) ((Result.Success) result).getValue());
                }
            } else if (result instanceof Result.Error) {
                arrayList.addAll((Collection) ((Result.Error) result).getValue());
            }
        }
        NonEmptyList nonEmptyListOrNull = NonEmptyListKt.toNonEmptyListOrNull(arrayList);
        return nonEmptyListOrNull != null ? new Result.Error(nonEmptyListOrNull) : new Result.Success(NonEmptyMapKt.nonEmptyMapOf((k) ResultKt.getOrThrow(success), linkedHashMap2));
    }

    /* JADX INFO: renamed from: withFallbackLanguages-_KYeFs0, reason: not valid java name */
    private static final List<Locale> m693withFallbackLanguages_KYeFs0(String str) {
        Locale localeM690toJavaLocale_KYeFs0 = m690toJavaLocale_KYeFs0(str);
        List<Locale> fallbackLanguages = getFallbackLanguages(localeM690toJavaLocale_KYeFs0);
        if (fallbackLanguages.isEmpty()) {
            return AbstractC2612c.q(localeM690toJavaLocale_KYeFs0);
        }
        C3274b c3274bK = AbstractC2612c.k();
        c3274bK.add(localeM690toJavaLocale_KYeFs0);
        c3274bK.addAll(fallbackLanguages);
        return AbstractC2612c.e(c3274bK);
    }

    /* JADX INFO: renamed from: getBestMatch-64pKzr8, reason: not valid java name */
    public static final String m684getBestMatch64pKzr8(Set getBestMatch, String localeId) {
        LocaleId localeIdM571boximpl;
        String strM572constructorimpl;
        String strM572constructorimpl2;
        Object next;
        Object next2;
        Object next3;
        o.h(getBestMatch, "$this$getBestMatch");
        o.h(localeId, "localeId");
        if (getBestMatch.contains(LocaleId.m571boximpl(localeId))) {
            return localeId;
        }
        Iterator<T> it = m693withFallbackLanguages_KYeFs0(localeId).iterator();
        while (true) {
            if (!it.hasNext()) {
                localeIdM571boximpl = null;
                break;
            }
            Locale locale = (Locale) it.next();
            String language = locale.getLanguage();
            String country = locale.getCountry();
            String it2 = locale.getScript();
            o.g(it2, "it");
            if (p.Q0(it2)) {
                it2 = null;
            }
            if (it2 == null) {
                it2 = scriptByRegion.get(country);
            }
            o.g(language, "language");
            String strM572constructorimpl3 = LocaleId.m572constructorimpl(language);
            if (it2 != null) {
                strM572constructorimpl = LocaleId.m572constructorimpl(language + '_' + it2);
            } else {
                strM572constructorimpl = null;
            }
            if (it2 != null) {
                strM572constructorimpl2 = LocaleId.m572constructorimpl(language + '_' + it2 + '_' + country);
            } else {
                strM572constructorimpl2 = null;
            }
            String localeId2 = toLocaleId(locale);
            C3274b c3274bK = AbstractC2612c.k();
            c3274bK.add(LocaleId.m571boximpl(localeId2));
            if (strM572constructorimpl2 != null) {
                c3274bK.add(LocaleId.m571boximpl(strM572constructorimpl2));
            }
            if (strM572constructorimpl != null) {
                c3274bK.add(LocaleId.m571boximpl(strM572constructorimpl));
            }
            c3274bK.add(LocaleId.m571boximpl(strM572constructorimpl3));
            ListIterator listIterator = AbstractC2612c.e(c3274bK).listIterator(0);
            while (true) {
                C0307p c0307p = (C0307p) listIterator;
                if (!c0307p.hasNext()) {
                    next = null;
                    break;
                }
                next = c0307p.next();
                if (getBestMatch.contains(LocaleId.m571boximpl(((LocaleId) next).m577unboximpl()))) {
                    break;
                }
            }
            LocaleId localeId3 = (LocaleId) next;
            String strM577unboximpl = localeId3 != null ? localeId3.m577unboximpl() : null;
            if (strM577unboximpl == null) {
                Set set = getBestMatch;
                ArrayList arrayList = new ArrayList(AbstractC3253s.G(set, 10));
                Iterator it3 = set.iterator();
                while (it3.hasNext()) {
                    arrayList.add(m690toJavaLocale_KYeFs0(((LocaleId) it3.next()).m577unboximpl()));
                }
                Iterator it4 = arrayList.iterator();
                while (true) {
                    if (!it4.hasNext()) {
                        next2 = null;
                        break;
                    }
                    next2 = it4.next();
                    Locale locale2 = (Locale) next2;
                    if (o.c(locale2.getLanguage(), language) && o.c(locale2.getScript(), it2)) {
                        break;
                    }
                }
                Locale locale3 = (Locale) next2;
                if (locale3 != null) {
                    strM577unboximpl = toLocaleId(locale3);
                } else {
                    Iterator it5 = arrayList.iterator();
                    while (true) {
                        if (!it5.hasNext()) {
                            next3 = null;
                            break;
                        }
                        next3 = it5.next();
                        if (o.c(((Locale) next3).getLanguage(), language)) {
                            break;
                        }
                    }
                    Locale locale4 = (Locale) next3;
                    strM577unboximpl = locale4 != null ? toLocaleId(locale4) : null;
                }
            }
            localeIdM571boximpl = strM577unboximpl != null ? LocaleId.m571boximpl(strM577unboximpl) : null;
            if (localeIdM571boximpl != null) {
                break;
            }
        }
        if (localeIdM571boximpl != null) {
            return localeIdM571boximpl.m577unboximpl();
        }
        return null;
    }

    public static final /* synthetic */ String toLocaleId(Locale locale) {
        o.h(locale, "<this>");
        String languageTag = locale.toLanguageTag();
        o.g(languageTag, "toLanguageTag()");
        return LocaleId.m572constructorimpl(w.x0(languageTag, '-', '_'));
    }
}
