package com.revenuecat.purchases.subscriberattributes.caching;

import S4.p;
import android.content.SharedPreferences;
import com.revenuecat.purchases.subscriberattributes.SubscriberAttribute;
import com.revenuecat.purchases.subscriberattributes.SubscriberAttributesFactoriesKt;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import kotlin.jvm.internal.o;
import org.json.JSONObject;
import x4.AbstractC3229F;
import x4.AbstractC3253s;
import x4.z;

/* JADX INFO: loaded from: classes3.dex */
public final class SubscriberAttributesMigrationExtensionsKt {
    public static final synchronized Map<String, Map<String, SubscriberAttribute>> getAllLegacyStoredSubscriberAttributes(SubscriberAttributesCache subscriberAttributesCache) {
        LinkedHashMap linkedHashMap;
        Map mapBuildLegacySubscriberAttributes;
        try {
            o.h(subscriberAttributesCache, "<this>");
            String strLegacySubscriberAttributesCacheKey = legacySubscriberAttributesCacheKey(subscriberAttributesCache, "");
            Set<String> setFindKeysThatStartWith$purchases_defaultsBc8Release = subscriberAttributesCache.getDeviceCache$purchases_defaultsBc8Release().findKeysThatStartWith$purchases_defaultsBc8Release(strLegacySubscriberAttributesCacheKey);
            int iU = AbstractC3229F.u(AbstractC3253s.G(setFindKeysThatStartWith$purchases_defaultsBc8Release, 10));
            if (iU < 16) {
                iU = 16;
            }
            linkedHashMap = new LinkedHashMap(iU);
            for (String str : setFindKeysThatStartWith$purchases_defaultsBc8Release) {
                String str2 = (String) p.d1(str, new String[]{strLegacySubscriberAttributesCacheKey}, 6).get(1);
                JSONObject jSONObjectOrNull$purchases_defaultsBc8Release = subscriberAttributesCache.getDeviceCache$purchases_defaultsBc8Release().getJSONObjectOrNull$purchases_defaultsBc8Release(str);
                if (jSONObjectOrNull$purchases_defaultsBc8Release == null || (mapBuildLegacySubscriberAttributes = SubscriberAttributesFactoriesKt.buildLegacySubscriberAttributes(jSONObjectOrNull$purchases_defaultsBc8Release)) == null) {
                    mapBuildLegacySubscriberAttributes = z.f26066a;
                }
                linkedHashMap.put(str2, mapBuildLegacySubscriberAttributes);
            }
        } catch (Throwable th) {
            throw th;
        }
        return linkedHashMap;
    }

    public static final String legacySubscriberAttributesCacheKey(SubscriberAttributesCache subscriberAttributesCache, String appUserID) {
        o.h(subscriberAttributesCache, "<this>");
        o.h(appUserID, "appUserID");
        return subscriberAttributesCache.getSubscriberAttributesCacheKey$purchases_defaultsBc8Release() + '.' + appUserID;
    }

    public static final synchronized void migrateSubscriberAttributes(SubscriberAttributesCache subscriberAttributesCache, Map<String, ? extends Map<String, SubscriberAttribute>> legacySubscriberAttributesForAppUserID, SharedPreferences.Editor cacheEditor) {
        try {
            o.h(subscriberAttributesCache, "<this>");
            o.h(legacySubscriberAttributesForAppUserID, "legacySubscriberAttributesForAppUserID");
            o.h(cacheEditor, "cacheEditor");
            Map<String, Map<String, SubscriberAttribute>> allStoredSubscriberAttributes = subscriberAttributesCache.getAllStoredSubscriberAttributes();
            LinkedHashMap linkedHashMapI = AbstractC3229F.I(allStoredSubscriberAttributes);
            for (Map.Entry<String, ? extends Map<String, SubscriberAttribute>> entry : legacySubscriberAttributesForAppUserID.entrySet()) {
                String key = entry.getKey();
                Map<String, SubscriberAttribute> value = entry.getValue();
                Map<String, SubscriberAttribute> map = allStoredSubscriberAttributes.get(key);
                if (map == null) {
                    map = z.f26066a;
                }
                linkedHashMapI.put(key, AbstractC3229F.z(value, map));
                cacheEditor.remove(legacySubscriberAttributesCacheKey(subscriberAttributesCache, key));
            }
            cacheEditor.putString(subscriberAttributesCache.getSubscriberAttributesCacheKey$purchases_defaultsBc8Release(), CachingHelpersKt.toJSONObject(linkedHashMapI).toString());
        } catch (Throwable th) {
            throw th;
        }
    }

    public static final synchronized void migrateSubscriberAttributesIfNeeded(SubscriberAttributesCache subscriberAttributesCache, SharedPreferences.Editor cacheEditor) {
        o.h(subscriberAttributesCache, "<this>");
        o.h(cacheEditor, "cacheEditor");
        Map<String, Map<String, SubscriberAttribute>> allLegacyStoredSubscriberAttributes = getAllLegacyStoredSubscriberAttributes(subscriberAttributesCache);
        if (allLegacyStoredSubscriberAttributes.isEmpty()) {
            allLegacyStoredSubscriberAttributes = null;
        }
        if (allLegacyStoredSubscriberAttributes != null) {
            migrateSubscriberAttributes(subscriberAttributesCache, allLegacyStoredSubscriberAttributes, cacheEditor);
        }
    }
}
