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

import E3.v;
import S4.p;
import W.c;
import W.d;
import W0.e;
import X0.AbstractC0808v;
import X0.C0788a;
import X0.C0789b;
import X0.E;
import X0.H;
import X0.I;
import X0.r;
import X0.y;
import Y0.a;
import Y0.b;
import a1.AbstractC0958a;
import android.content.res.AssetManager;
import androidx.media3.common.C;
import com.revenuecat.purchases.FontAlias;
import com.revenuecat.purchases.UiConfig;
import com.revenuecat.purchases.paywalls.DownloadedFont;
import com.revenuecat.purchases.paywalls.DownloadedFontFamily;
import com.revenuecat.purchases.paywalls.components.properties.FontStyle;
import com.revenuecat.purchases.ui.revenuecatui.R;
import com.revenuecat.purchases.ui.revenuecatui.components.properties.FontSpec;
import com.revenuecat.purchases.ui.revenuecatui.errors.PaywallValidationError;
import com.revenuecat.purchases.ui.revenuecatui.helpers.Logger;
import com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider;
import com.revenuecat.purchases.ui.revenuecatui.helpers.Result;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import kotlin.jvm.internal.o;
import w4.k;
import w4.q;
import x4.AbstractC3229F;
import x4.AbstractC3251q;
import x4.AbstractC3253s;

/* JADX INFO: loaded from: classes3.dex */
public final /* synthetic */ class FontSpecKt {
    private static final a GoogleFontsProvider = new a("com.google.android.gms.fonts", "com.google.android.gms", R.array.com_google_android_gms_fonts_certs);

    public /* synthetic */ class WhenMappings {
        public static final /* synthetic */ int[] $EnumSwitchMapping$0;

        static {
            int[] iArr = new int[FontStyle.values().length];
            try {
                iArr[FontStyle.NORMAL.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                iArr[FontStyle.ITALIC.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            $EnumSwitchMapping$0 = iArr;
        }
    }

    private static final FontSpec determineFontSpec(ResourceProvider resourceProvider, UiConfig.AppConfig.FontsConfig fontsConfig, Map<String, FontSpec.Resource> map, Map<String, FontSpec.Asset> map2) {
        FontSpec.Asset asset;
        UiConfig.AppConfig.FontsConfig.FontInfo android2 = fontsConfig.getAndroid();
        if (android2 instanceof UiConfig.AppConfig.FontsConfig.FontInfo.GoogleFonts) {
            return new FontSpec.Google(((UiConfig.AppConfig.FontsConfig.FontInfo.GoogleFonts) android2).getValue());
        }
        if (!(android2 instanceof UiConfig.AppConfig.FontsConfig.FontInfo.Name)) {
            throw new v();
        }
        UiConfig.AppConfig.FontsConfig.FontInfo.Name name = (UiConfig.AppConfig.FontsConfig.FontInfo.Name) android2;
        FontSpec.Generic genericFontSpec = getGenericFontSpec(name);
        if (genericFontSpec != null) {
            return genericFontSpec;
        }
        if (name.getFamily() != null) {
            asset = map.get(name.getFamily());
            if (asset == null) {
                asset = map2.get(name.getFamily());
            }
        } else {
            asset = null;
        }
        if (asset != null) {
            return asset;
        }
        FontSpec.Downloaded downloadedFontSpec = getDownloadedFontSpec(resourceProvider, name);
        if (downloadedFontSpec != null) {
            return downloadedFontSpec;
        }
        FontSpec.System system = new FontSpec.System(name.getValue());
        Logger.INSTANCE.d("Could not find a font resource named `" + name.getValue() + "`. Assuming it's an OEM system font. If it isn't, make sure the font exists in the `res/font` folder. See for more info: https://developer.android.com/develop/ui/views/text-and-emoji/fonts-in-xml");
        return system;
    }

    public static final Map determineFontSpecs(Map map, ResourceProvider resourceProvider) {
        String family;
        o.h(map, "<this>");
        o.h(resourceProvider, "resourceProvider");
        Set setH0 = AbstractC3251q.H0(map.values());
        ArrayList<k> arrayList = new ArrayList();
        Iterator it = setH0.iterator();
        while (true) {
            k kVar = null;
            if (!it.hasNext()) {
                break;
            }
            UiConfig.AppConfig.FontsConfig.FontInfo android2 = ((UiConfig.AppConfig.FontsConfig) it.next()).getAndroid();
            UiConfig.AppConfig.FontsConfig.FontInfo.Name name = android2 instanceof UiConfig.AppConfig.FontsConfig.FontInfo.Name ? (UiConfig.AppConfig.FontsConfig.FontInfo.Name) android2 : null;
            if (name != null && (family = name.getFamily()) != null) {
                kVar = new k(family, name);
            }
            if (kVar != null) {
                arrayList.add(kVar);
            }
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        for (k kVar2 : arrayList) {
            String str = (String) kVar2.f25469a;
            Object arrayList2 = linkedHashMap.get(str);
            if (arrayList2 == null) {
                arrayList2 = new ArrayList();
                linkedHashMap.put(str, arrayList2);
            }
            ((List) arrayList2).add((UiConfig.AppConfig.FontsConfig.FontInfo.Name) kVar2.f25470b);
        }
        ArrayList<k> arrayList3 = new ArrayList();
        for (Map.Entry entry : linkedHashMap.entrySet()) {
            String str2 = (String) entry.getKey();
            FontSpec.Resource fontSpecResource = toFontSpecResource((List) entry.getValue(), resourceProvider);
            k kVar3 = fontSpecResource != null ? new k(str2, fontSpecResource) : null;
            if (kVar3 != null) {
                arrayList3.add(kVar3);
            }
        }
        int iU = AbstractC3229F.u(AbstractC3253s.G(arrayList3, 10));
        if (iU < 16) {
            iU = 16;
        }
        LinkedHashMap linkedHashMap2 = new LinkedHashMap(iU);
        for (k kVar4 : arrayList3) {
            linkedHashMap2.put((String) kVar4.f25469a, (FontSpec.Resource) kVar4.f25470b);
        }
        LinkedHashMap linkedHashMap3 = new LinkedHashMap();
        for (Map.Entry entry2 : linkedHashMap.entrySet()) {
            if (!linkedHashMap2.containsKey(entry2.getKey())) {
                linkedHashMap3.put(entry2.getKey(), entry2.getValue());
            }
        }
        ArrayList<k> arrayList4 = new ArrayList();
        for (Map.Entry entry3 : linkedHashMap3.entrySet()) {
            String str3 = (String) entry3.getKey();
            FontSpec.Asset fontSpecAsset = toFontSpecAsset((List) entry3.getValue(), resourceProvider);
            k kVar5 = fontSpecAsset != null ? new k(str3, fontSpecAsset) : null;
            if (kVar5 != null) {
                arrayList4.add(kVar5);
            }
        }
        int iU2 = AbstractC3229F.u(AbstractC3253s.G(arrayList4, 10));
        if (iU2 < 16) {
            iU2 = 16;
        }
        LinkedHashMap linkedHashMap4 = new LinkedHashMap(iU2);
        for (k kVar6 : arrayList4) {
            linkedHashMap4.put((String) kVar6.f25469a, (FontSpec.Asset) kVar6.f25470b);
        }
        Set setH02 = AbstractC3251q.H0(map.values());
        int iU3 = AbstractC3229F.u(AbstractC3253s.G(setH02, 10));
        LinkedHashMap linkedHashMap5 = new LinkedHashMap(iU3 >= 16 ? iU3 : 16);
        for (Object obj : setH02) {
            linkedHashMap5.put(obj, determineFontSpec(resourceProvider, (UiConfig.AppConfig.FontsConfig) obj, linkedHashMap2, linkedHashMap4));
        }
        LinkedHashMap linkedHashMap6 = new LinkedHashMap(AbstractC3229F.u(map.size()));
        for (Map.Entry entry4 : map.entrySet()) {
            linkedHashMap6.put(entry4.getKey(), (FontSpec) AbstractC3229F.t((UiConfig.AppConfig.FontsConfig) entry4.getValue(), linkedHashMap5));
        }
        return linkedHashMap6;
    }

    private static final FontSpec.Downloaded getDownloadedFontSpec(ResourceProvider resourceProvider, UiConfig.AppConfig.FontsConfig.FontInfo.Name name) {
        DownloadedFontFamily cachedFontFamilyOrStartDownload = resourceProvider.getCachedFontFamilyOrStartDownload(name);
        if (cachedFontFamilyOrStartDownload != null) {
            return new FontSpec.Downloaded(cachedFontFamilyOrStartDownload);
        }
        return null;
    }

    /* JADX INFO: renamed from: getFontSpec-pDyximM, reason: not valid java name */
    public static final /* synthetic */ Result m744getFontSpecpDyximM(Map getFontSpec, String alias) {
        o.h(getFontSpec, "$this$getFontSpec");
        o.h(alias, "alias");
        FontSpec fontSpec = (FontSpec) getFontSpec.get(FontAlias.m396boximpl(alias));
        return fontSpec != null ? new Result.Success(fontSpec) : new Result.Error(new PaywallValidationError.MissingFontAlias(alias, null));
    }

    private static final FontSpec.Generic getGenericFontSpec(UiConfig.AppConfig.FontsConfig.FontInfo.Name name) {
        String value = name.getValue();
        if (o.c(value, C.SANS_SERIF_NAME)) {
            return FontSpec.Generic.SansSerif.INSTANCE;
        }
        if (o.c(value, C.SERIF_NAME)) {
            return FontSpec.Generic.Serif.INSTANCE;
        }
        if (o.c(value, "monospace")) {
            return FontSpec.Generic.Monospace.INSTANCE;
        }
        return null;
    }

    public static final Result recoverFromFontAliasError(Result result) {
        o.h(result, "<this>");
        if (result instanceof Result.Success) {
            return result;
        }
        if (!(result instanceof Result.Error)) {
            throw new v();
        }
        PaywallValidationError paywallValidationError = (PaywallValidationError) ((Result.Error) result).getValue();
        boolean z6 = paywallValidationError instanceof PaywallValidationError.MissingFontAlias;
        if (z6 && p.Q0(((PaywallValidationError.MissingFontAlias) paywallValidationError).m1094getAliasBejUyPs())) {
            return new Result.Success(null);
        }
        if (!z6) {
            return new Result.Error(paywallValidationError);
        }
        Logger.INSTANCE.w("Font named '" + ((Object) FontAlias.m401toStringimpl(((PaywallValidationError.MissingFontAlias) paywallValidationError).m1094getAliasBejUyPs())) + "' was not found in the font config. Try re-adding it in the Paywall editor.");
        return new Result.Success(null);
    }

    /* JADX INFO: renamed from: resolve-RetOiIg, reason: not valid java name */
    public static final AbstractC0808v m745resolveRetOiIg(FontSpec resolve, I weight, int i6) {
        o.h(resolve, "$this$resolve");
        o.h(weight, "weight");
        if (resolve instanceof FontSpec.Resource) {
            return ((FontSpec.Resource) resolve).getFontFamily();
        }
        if (resolve instanceof FontSpec.Asset) {
            return ((FontSpec.Asset) resolve).getFontFamily();
        }
        if (resolve instanceof FontSpec.Google) {
            String name = ((FontSpec.Google) resolve).getName();
            if (name.length() > 0) {
                return W.a.K(new b(name, GoogleFontsProvider, weight, i6, true));
            }
            throw new IllegalArgumentException("name cannot be empty");
        }
        if (resolve instanceof FontSpec.Generic) {
            if (resolve.equals(FontSpec.Generic.SansSerif.INSTANCE)) {
                return AbstractC0808v.f8523b;
            }
            if (resolve.equals(FontSpec.Generic.Serif.INSTANCE)) {
                return AbstractC0808v.f8524c;
            }
            if (resolve.equals(FontSpec.Generic.Monospace.INSTANCE)) {
                return AbstractC0808v.f8525d;
            }
            throw new v();
        }
        if (!(resolve instanceof FontSpec.Downloaded)) {
            if (!(resolve instanceof FontSpec.System)) {
                throw new v();
            }
            String name2 = ((FontSpec.System) resolve).getName();
            if (name2.length() <= 0) {
                AbstractC0958a.a("name may not be empty");
            }
            return W.a.K(new r(name2, weight, i6, new H(new E[0])));
        }
        List<DownloadedFont> fonts = ((FontSpec.Downloaded) resolve).getDownloadedFontFamily().getFonts();
        ArrayList arrayList = new ArrayList(AbstractC3253s.G(fonts, 10));
        for (DownloadedFont downloadedFont : fonts) {
            File file = new File(downloadedFont.getFile().getPath());
            I i7 = new I(downloadedFont.getWeight());
            int composeFontStyle = toComposeFontStyle(downloadedFont.getStyle());
            arrayList.add(new C0789b(file, i7, composeFontStyle, e.d(i7, composeFontStyle, new E[0])));
        }
        return new y(arrayList);
    }

    private static final int toComposeFontStyle(FontStyle fontStyle) {
        int i6 = WhenMappings.$EnumSwitchMapping$0[fontStyle.ordinal()];
        if (i6 == 1) {
            return 0;
        }
        if (i6 == 2) {
            return 1;
        }
        throw new v();
    }

    private static final List<q> toFontResourceIdAndData(List<UiConfig.AppConfig.FontsConfig.FontInfo.Name> list, ResourceProvider resourceProvider) {
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        ArrayList arrayList = new ArrayList();
        for (UiConfig.AppConfig.FontsConfig.FontInfo.Name name : list) {
            if (!linkedHashSet.contains(name.getValue())) {
                int resourceIdentifier = resourceProvider.getResourceIdentifier(name.getValue(), "font");
                Integer numValueOf = Integer.valueOf(resourceIdentifier);
                if (resourceIdentifier == 0) {
                    numValueOf = null;
                }
                if (numValueOf != null) {
                    linkedHashSet.add(name.getValue());
                    Integer numValueOf2 = Integer.valueOf(numValueOf.intValue());
                    Integer weight = name.getWeight();
                    FontStyle style = name.getStyle();
                    qVar = new q(numValueOf2, weight, style != null ? new X0.C(toComposeFontStyle(style)) : null);
                }
            }
            if (qVar != null) {
                arrayList.add(qVar);
            }
        }
        return arrayList;
    }

    private static final FontSpec.Asset toFontSpecAsset(List<UiConfig.AppConfig.FontsConfig.FontInfo.Name> list, ResourceProvider resourceProvider) {
        C0788a c0788aE;
        ArrayList arrayList = new ArrayList(AbstractC3253s.G(list, 10));
        Iterator<T> it = list.iterator();
        while (it.hasNext()) {
            arrayList.add(((UiConfig.AppConfig.FontsConfig.FontInfo.Name) it.next()).getValue());
        }
        Map<String, String> assetFontPaths = resourceProvider.getAssetFontPaths(arrayList);
        AssetManager assetManager = resourceProvider.getAssetManager();
        if (assetFontPaths == null || assetManager == null) {
            return null;
        }
        ArrayList arrayList2 = new ArrayList();
        for (UiConfig.AppConfig.FontsConfig.FontInfo.Name name : list) {
            String str = assetFontPaths.get(name.getValue());
            if (str == null) {
                c0788aE = null;
            } else {
                Integer weight = name.getWeight();
                I i6 = weight != null ? new I(weight.intValue()) : I.f8446k;
                FontStyle style = name.getStyle();
                c0788aE = d.e(str, assetManager, i6, style != null ? toComposeFontStyle(style) : 0);
            }
            if (c0788aE != null) {
                arrayList2.add(c0788aE);
            }
        }
        return new FontSpec.Asset(new y(arrayList2));
    }

    private static final FontSpec.Resource toFontSpecResource(List<UiConfig.AppConfig.FontsConfig.FontInfo.Name> list, ResourceProvider resourceProvider) {
        AbstractC0808v xmlFontFamily;
        List<q> fontResourceIdAndData = toFontResourceIdAndData(list, resourceProvider);
        if (fontResourceIdAndData.isEmpty()) {
            return null;
        }
        if (fontResourceIdAndData.size() == 1 && (xmlFontFamily = resourceProvider.getXmlFontFamily(((Number) ((q) AbstractC3251q.e0(fontResourceIdAndData)).f25479a).intValue())) != null) {
            return new FontSpec.Resource(xmlFontFamily);
        }
        ArrayList arrayList = new ArrayList(AbstractC3253s.G(fontResourceIdAndData, 10));
        for (q qVar : fontResourceIdAndData) {
            int iIntValue = ((Number) qVar.f25479a).intValue();
            Integer num = (Integer) qVar.f25480b;
            I i6 = num != null ? new I(num.intValue()) : I.f8446k;
            X0.C c6 = (X0.C) qVar.f25481c;
            arrayList.add(c.c(iIntValue, i6, c6 != null ? c6.f8434a : 0, 8));
        }
        return new FontSpec.Resource(new y(arrayList));
    }
}
