package com.revenuecat.purchases.ui.revenuecatui.helpers;

import S4.w;
import X0.AbstractC0808v;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import androidx.compose.runtime.internal.StabilityInferred;
import com.revenuecat.purchases.Purchases;
import com.revenuecat.purchases.UiConfig;
import com.revenuecat.purchases.paywalls.DownloadedFontFamily;
import com.revenuecat.purchases.ui.revenuecatui.utils.FontFamilyXmlParser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import kotlin.jvm.internal.o;
import w4.k;
import x4.AbstractC3229F;

/* JADX INFO: loaded from: classes3.dex */
@StabilityInferred(parameters = 0)
public final class PaywallResourceProvider implements ResourceProvider {
    public static final int $stable = 8;
    private final String applicationName;
    private final String packageName;
    private final Resources resources;

    public PaywallResourceProvider(String applicationName, String packageName, Resources resources) {
        o.h(applicationName, "applicationName");
        o.h(packageName, "packageName");
        o.h(resources, "resources");
        this.applicationName = applicationName;
        this.packageName = packageName;
        this.resources = resources;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider
    public String getApplicationName() {
        return this.applicationName;
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x0051  */
    @Override // com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Map<String, String> getAssetFontPaths(List<String> names) throws IOException {
        String strConcat;
        String str;
        o.h(names, "names");
        String[] list = this.resources.getAssets().list("fonts");
        ArrayList arrayList = new ArrayList();
        Iterator<T> it = names.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            String str2 = (String) it.next();
            int i6 = 0;
            String strConcat2 = w.s0(str2, ".ttf", false) ? str2 : str2.concat(".ttf");
            if (list != null) {
                int length = list.length;
                while (true) {
                    if (i6 >= length) {
                        str = null;
                        break;
                    }
                    str = list[i6];
                    if (o.c(str, strConcat2)) {
                        break;
                    }
                    i6++;
                }
                strConcat = str != null ? "fonts/".concat(str) : null;
            }
            k kVar = strConcat != null ? new k(str2, strConcat) : null;
            if (kVar != null) {
                arrayList.add(kVar);
            }
        }
        Map<String, String> mapF = AbstractC3229F.F(arrayList);
        if (mapF.isEmpty()) {
            return null;
        }
        return mapF;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider
    public AssetManager getAssetManager() {
        return this.resources.getAssets();
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider
    public DownloadedFontFamily getCachedFontFamilyOrStartDownload(UiConfig.AppConfig.FontsConfig.FontInfo.Name fontInfo) {
        o.h(fontInfo, "fontInfo");
        Purchases.Companion companion = Purchases.Companion;
        if (companion.isConfigured()) {
            return companion.getSharedInstance().getCachedFontFamilyOrStartDownload(fontInfo);
        }
        Logger.INSTANCE.e("getCachedFontFileOrStartDownload called before Purchases is configured. Returning null.");
        return null;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider
    public Locale getLocale() {
        Locale locale = this.resources.getConfiguration().getLocales().get(0);
        o.g(locale, "resources.configuration.locales.get(0)");
        return locale;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider
    public int getResourceIdentifier(String name, String type) {
        o.h(name, "name");
        o.h(type, "type");
        return this.resources.getIdentifier(name, type, this.packageName);
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider
    public String getString(int i6, Object... formatArgs) {
        o.h(formatArgs, "formatArgs");
        String string = this.resources.getString(i6, Arrays.copyOf(formatArgs, formatArgs.length));
        o.g(string, "resources.getString(resId, *formatArgs)");
        return string;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider
    public AbstractC0808v getXmlFontFamily(int i6) {
        try {
            XmlResourceParser xml = this.resources.getXml(i6);
            o.g(xml, "try {\n            resour…    return null\n        }");
            try {
                AbstractC0808v abstractC0808v = FontFamilyXmlParser.INSTANCE.parse(xml);
                xml.close();
                return abstractC0808v;
            } catch (Throwable th) {
                try {
                    Object resourceEntryNameOrNull = PaywallResourceProviderKt.getResourceEntryNameOrNull(this.resources, i6);
                    Logger logger = Logger.INSTANCE;
                    StringBuilder sb = new StringBuilder("Error parsing XML font family with resource ID ");
                    if (resourceEntryNameOrNull == null) {
                        resourceEntryNameOrNull = Integer.valueOf(i6);
                    }
                    sb.append(resourceEntryNameOrNull);
                    logger.e(sb.toString(), th);
                    xml.close();
                    return null;
                } catch (Throwable th2) {
                    xml.close();
                    throw th2;
                }
            }
        } catch (Resources.NotFoundException unused) {
            return null;
        }
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public PaywallResourceProvider(Context context) {
        o.h(context, "context");
        Context applicationContext = context.getApplicationContext();
        o.g(applicationContext, "context.applicationContext");
        String strApplicationName = PaywallResourceProviderKt.applicationName(applicationContext);
        String packageName = context.getPackageName();
        o.g(packageName, "context.packageName");
        Resources resources = context.getResources();
        o.g(resources, "context.resources");
        this(strApplicationName, packageName, resources);
    }
}
