package com.revenuecat.purchases.common.offlineentitlements;

import A0.a;
import com.revenuecat.purchases.InternalRevenueCatAPI;
import com.revenuecat.purchases.common.HTTPResponseOriginalSource;
import com.revenuecat.purchases.common.HTTPResponseOriginalSourceKt;
import com.revenuecat.purchases.common.LogWrapperKt;
import com.revenuecat.purchases.common.networking.HTTPResult;
import com.revenuecat.purchases.utils.JSONObjectExtensionsKt;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import q.AbstractC2761L;
import x4.AbstractC3229F;

/* JADX INFO: loaded from: classes3.dex */
@InternalRevenueCatAPI
public final class ProductEntitlementMapping {
    private static final String BASE_PLAN_ID_KEY = "base_plan_id";
    public static final Companion Companion = new Companion(null);
    private static final String ENTITLEMENTS_KEY = "entitlements";
    private static final String ORIGINAL_SOURCE_KEY = "rc_original_source";
    private static final String PRODUCT_ENTITLEMENT_MAPPING_KEY = "product_entitlement_mapping";
    private static final String PRODUCT_ID_KEY = "product_identifier";
    private final boolean loadedFromCache;
    private final Map<String, Mapping> mappings;
    private final HTTPResponseOriginalSource originalSource;

    public static final class Companion {
        public /* synthetic */ Companion(h hVar) {
            this();
        }

        public static /* synthetic */ ProductEntitlementMapping fromJson$purchases_defaultsBc8Release$default(Companion companion, JSONObject jSONObject, boolean z6, int i6, Object obj) {
            if ((i6 & 2) != 0) {
                z6 = false;
            }
            return companion.fromJson$purchases_defaultsBc8Release(jSONObject, z6);
        }

        /* JADX WARN: Removed duplicated region for block: B:16:0x008b  */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final ProductEntitlementMapping fromJson$purchases_defaultsBc8Release(JSONObject json, boolean z6) throws JSONException {
            HTTPResponseOriginalSource hTTPResponseOriginalSourceValueOf;
            o.h(json, "json");
            JSONObject jSONObject = json.getJSONObject(ProductEntitlementMapping.PRODUCT_ENTITLEMENT_MAPPING_KEY);
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            Iterator<String> itKeys = jSONObject.keys();
            o.g(itKeys, "productsObject.keys()");
            while (itKeys.hasNext()) {
                String mappingIdentifier = itKeys.next();
                JSONObject jSONObject2 = jSONObject.getJSONObject(mappingIdentifier);
                String productIdentifier = jSONObject2.getString("product_identifier");
                String strOptNullableString = JSONObjectExtensionsKt.optNullableString(jSONObject2, ProductEntitlementMapping.BASE_PLAN_ID_KEY);
                JSONArray jSONArray = jSONObject2.getJSONArray("entitlements");
                ArrayList arrayList = new ArrayList();
                int length = jSONArray.length();
                for (int i6 = 0; i6 < length; i6++) {
                    String string = jSONArray.getString(i6);
                    o.g(string, "entitlementsArray.getString(entitlementIndex)");
                    arrayList.add(string);
                }
                o.g(mappingIdentifier, "mappingIdentifier");
                o.g(productIdentifier, "productIdentifier");
                linkedHashMap.put(mappingIdentifier, new Mapping(productIdentifier, strOptNullableString, arrayList));
            }
            String strOptNullableString2 = JSONObjectExtensionsKt.optNullableString(json, "rc_original_source");
            if (strOptNullableString2 != null) {
                try {
                    hTTPResponseOriginalSourceValueOf = HTTPResponseOriginalSource.valueOf(strOptNullableString2);
                } catch (IllegalArgumentException e6) {
                    LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", Z0.o.o("Invalid original source when reading it from JSON: ", strOptNullableString2, ". Defaulting to MAIN."), e6);
                    hTTPResponseOriginalSourceValueOf = null;
                }
                if (hTTPResponseOriginalSourceValueOf == null) {
                    hTTPResponseOriginalSourceValueOf = HTTPResponseOriginalSource.MAIN;
                }
            }
            return new ProductEntitlementMapping(linkedHashMap, hTTPResponseOriginalSourceValueOf, z6);
        }

        public final ProductEntitlementMapping fromNetwork$purchases_defaultsBc8Release(JSONObject json, HTTPResult httpResult) throws JSONException {
            o.h(json, "json");
            o.h(httpResult, "httpResult");
            JSONObject jsonWithSource = JSONObjectExtensionsKt.copy(json, false).put("rc_original_source", HTTPResponseOriginalSourceKt.getOriginalDataSource(httpResult).name());
            o.g(jsonWithSource, "jsonWithSource");
            return fromJson$purchases_defaultsBc8Release(jsonWithSource, false);
        }

        private Companion() {
        }
    }

    @InternalRevenueCatAPI
    public static final class Mapping {
        private final String basePlanId;
        private final List<String> entitlements;
        private final String productIdentifier;

        public Mapping(String productIdentifier, String str, List<String> entitlements) {
            o.h(productIdentifier, "productIdentifier");
            o.h(entitlements, "entitlements");
            this.productIdentifier = productIdentifier;
            this.basePlanId = str;
            this.entitlements = entitlements;
        }

        /* JADX WARN: Multi-variable type inference failed */
        public static /* synthetic */ Mapping copy$default(Mapping mapping, String str, String str2, List list, int i6, Object obj) {
            if ((i6 & 1) != 0) {
                str = mapping.productIdentifier;
            }
            if ((i6 & 2) != 0) {
                str2 = mapping.basePlanId;
            }
            if ((i6 & 4) != 0) {
                list = mapping.entitlements;
            }
            return mapping.copy(str, str2, list);
        }

        public final String component1() {
            return this.productIdentifier;
        }

        public final String component2() {
            return this.basePlanId;
        }

        public final List<String> component3() {
            return this.entitlements;
        }

        public final Mapping copy(String productIdentifier, String str, List<String> entitlements) {
            o.h(productIdentifier, "productIdentifier");
            o.h(entitlements, "entitlements");
            return new Mapping(productIdentifier, str, entitlements);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof Mapping)) {
                return false;
            }
            Mapping mapping = (Mapping) obj;
            return o.c(this.productIdentifier, mapping.productIdentifier) && o.c(this.basePlanId, mapping.basePlanId) && o.c(this.entitlements, mapping.entitlements);
        }

        public final String getBasePlanId() {
            return this.basePlanId;
        }

        public final List<String> getEntitlements() {
            return this.entitlements;
        }

        public final String getProductIdentifier() {
            return this.productIdentifier;
        }

        public int hashCode() {
            int iHashCode = this.productIdentifier.hashCode() * 31;
            String str = this.basePlanId;
            return this.entitlements.hashCode() + ((iHashCode + (str == null ? 0 : str.hashCode())) * 31);
        }

        public String toString() {
            StringBuilder sb = new StringBuilder("Mapping(productIdentifier=");
            sb.append(this.productIdentifier);
            sb.append(", basePlanId=");
            sb.append(this.basePlanId);
            sb.append(", entitlements=");
            return a.o(sb, this.entitlements, ')');
        }
    }

    public ProductEntitlementMapping(Map<String, Mapping> mappings, HTTPResponseOriginalSource originalSource, boolean z6) {
        o.h(mappings, "mappings");
        o.h(originalSource, "originalSource");
        this.mappings = mappings;
        this.originalSource = originalSource;
        this.loadedFromCache = z6;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static /* synthetic */ ProductEntitlementMapping copy$default(ProductEntitlementMapping productEntitlementMapping, Map map, HTTPResponseOriginalSource hTTPResponseOriginalSource, boolean z6, int i6, Object obj) {
        if ((i6 & 1) != 0) {
            map = productEntitlementMapping.mappings;
        }
        if ((i6 & 2) != 0) {
            hTTPResponseOriginalSource = productEntitlementMapping.originalSource;
        }
        if ((i6 & 4) != 0) {
            z6 = productEntitlementMapping.loadedFromCache;
        }
        return productEntitlementMapping.copy(map, hTTPResponseOriginalSource, z6);
    }

    public final Map<String, Mapping> component1() {
        return this.mappings;
    }

    public final HTTPResponseOriginalSource component2() {
        return this.originalSource;
    }

    public final boolean component3() {
        return this.loadedFromCache;
    }

    public final ProductEntitlementMapping copy(Map<String, Mapping> mappings, HTTPResponseOriginalSource originalSource, boolean z6) {
        o.h(mappings, "mappings");
        o.h(originalSource, "originalSource");
        return new ProductEntitlementMapping(mappings, originalSource, z6);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof ProductEntitlementMapping)) {
            return false;
        }
        ProductEntitlementMapping productEntitlementMapping = (ProductEntitlementMapping) obj;
        return o.c(this.mappings, productEntitlementMapping.mappings) && this.originalSource == productEntitlementMapping.originalSource && this.loadedFromCache == productEntitlementMapping.loadedFromCache;
    }

    public final boolean getLoadedFromCache() {
        return this.loadedFromCache;
    }

    public final Map<String, Mapping> getMappings() {
        return this.mappings;
    }

    public final HTTPResponseOriginalSource getOriginalSource() {
        return this.originalSource;
    }

    public int hashCode() {
        return Boolean.hashCode(this.loadedFromCache) + ((this.originalSource.hashCode() + (this.mappings.hashCode() * 31)) * 31);
    }

    public final JSONObject toJson$purchases_defaultsBc8Release() throws JSONException {
        JSONObject jSONObject = new JSONObject();
        Map<String, Mapping> map = this.mappings;
        LinkedHashMap linkedHashMap = new LinkedHashMap(AbstractC3229F.u(map.size()));
        Iterator<T> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Object key = entry.getKey();
            Mapping mapping = (Mapping) entry.getValue();
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("product_identifier", mapping.getProductIdentifier());
            String basePlanId = mapping.getBasePlanId();
            if (basePlanId != null) {
                jSONObject2.put(BASE_PLAN_ID_KEY, basePlanId);
            }
            jSONObject2.put("entitlements", new JSONArray((Collection) mapping.getEntitlements()));
            linkedHashMap.put(key, jSONObject2);
        }
        jSONObject.put(PRODUCT_ENTITLEMENT_MAPPING_KEY, new JSONObject(linkedHashMap));
        jSONObject.put("rc_original_source", this.originalSource.name());
        return jSONObject;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("ProductEntitlementMapping(mappings=");
        sb.append(this.mappings);
        sb.append(", originalSource=");
        sb.append(this.originalSource);
        sb.append(", loadedFromCache=");
        return AbstractC2761L.h(sb, this.loadedFromCache, ')');
    }

    public /* synthetic */ ProductEntitlementMapping(Map map, HTTPResponseOriginalSource hTTPResponseOriginalSource, boolean z6, int i6, h hVar) {
        this(map, (i6 & 2) != 0 ? HTTPResponseOriginalSource.MAIN : hTTPResponseOriginalSource, (i6 & 4) != 0 ? false : z6);
    }
}
