package com.revenuecat.purchases.common.caching;

import A0.a;
import S4.w;
import android.content.Context;
import android.content.SharedPreferences;
import com.revenuecat.purchases.JsonTools;
import com.revenuecat.purchases.LogHandler;
import com.revenuecat.purchases.LogLevel;
import com.revenuecat.purchases.common.Config;
import com.revenuecat.purchases.common.LogWrapperKt;
import com.revenuecat.purchases.common.UtilsKt;
import g5.f;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import l5.AbstractC2458c;
import x4.AbstractC3253s;

/* JADX INFO: loaded from: classes3.dex */
public final class LocalTransactionMetadataStore {
    private static final Companion Companion = new Companion(null);

    @Deprecated
    public static final String KEY_PREFIX = "local_transaction_metadata_";
    private final String apiKey;
    private final AbstractC2458c json;
    private final SharedPreferences sharedPreferences;

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

        public final SharedPreferences initializeSharedPreferences(Context context, String apiKey) {
            o.h(context, "context");
            o.h(apiKey, "apiKey");
            SharedPreferences sharedPreferences = context.getSharedPreferences("com.revenuecat.purchases.transaction_metadata.".concat(apiKey), 0);
            o.g(sharedPreferences, "context.getSharedPrefere…DE_PRIVATE,\n            )");
            return sharedPreferences;
        }

        private Companion() {
        }
    }

    public LocalTransactionMetadataStore(Context context, String apiKey, SharedPreferences sharedPreferences, AbstractC2458c json) {
        o.h(context, "context");
        o.h(apiKey, "apiKey");
        o.h(sharedPreferences, "sharedPreferences");
        o.h(json, "json");
        this.apiKey = apiKey;
        this.sharedPreferences = sharedPreferences;
        this.json = json;
    }

    private final String getTokenHash(String str) {
        return UtilsKt.sha1(str);
    }

    private final boolean hasCachedData(String str) {
        return this.sharedPreferences.contains(KEY_PREFIX + str);
    }

    public final synchronized void cacheLocalTransactionMetadata(String purchaseToken, LocalTransactionMetadata data) {
        o.h(purchaseToken, "purchaseToken");
        o.h(data, "data");
        String tokenHash = getTokenHash(purchaseToken);
        if (hasCachedData(tokenHash)) {
            LogLevel logLevel = LogLevel.DEBUG;
            LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
            if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                currentLogHandler.d("[Purchases] - " + logLevel.name(), "Purchase data already cached for token: " + purchaseToken + ". Skipping cache.");
            }
            return;
        }
        try {
            String str = KEY_PREFIX + tokenHash;
            String strD = this.json.d(LocalTransactionMetadata.Companion.serializer(), data);
            SharedPreferences.Editor editor = this.sharedPreferences.edit();
            o.g(editor, "editor");
            editor.putString(str, strD);
            editor.apply();
            LogLevel logLevel2 = LogLevel.DEBUG;
            LogHandler currentLogHandler2 = LogWrapperKt.getCurrentLogHandler();
            if (Config.INSTANCE.getLogLevel().compareTo(logLevel2) <= 0) {
                currentLogHandler2.d("[Purchases] - " + logLevel2.name(), "Local transaction metadata cache updated");
            }
        } catch (f e6) {
            LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Failed to serialize local transaction metadata", e6);
        }
    }

    public final synchronized void clearLocalTransactionMetadata(Set<String> purchaseTokens) {
        try {
            o.h(purchaseTokens, "purchaseTokens");
            if (purchaseTokens.isEmpty()) {
                return;
            }
            Set<String> set = purchaseTokens;
            ArrayList arrayList = new ArrayList(AbstractC3253s.G(set, 10));
            Iterator<T> it = set.iterator();
            while (it.hasNext()) {
                arrayList.add(getTokenHash((String) it.next()));
            }
            SharedPreferences.Editor editor = this.sharedPreferences.edit();
            o.g(editor, "editor");
            Iterator it2 = arrayList.iterator();
            int i6 = 0;
            while (it2.hasNext()) {
                String str = KEY_PREFIX + ((String) it2.next());
                if (this.sharedPreferences.contains(str)) {
                    editor.remove(str);
                    i6++;
                }
            }
            editor.apply();
            if (i6 > 0) {
                LogLevel logLevel = LogLevel.VERBOSE;
                LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
                if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                    currentLogHandler.v("[Purchases] - " + logLevel.name(), "Cleared local transaction metadata for " + i6 + " token(s)");
                }
            } else {
                LogLevel logLevel2 = LogLevel.DEBUG;
                LogHandler currentLogHandler2 = LogWrapperKt.getCurrentLogHandler();
                if (Config.INSTANCE.getLogLevel().compareTo(logLevel2) <= 0) {
                    currentLogHandler2.d("[Purchases] - " + logLevel2.name(), "No transaction metadata found to clear from local cache.");
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final List<LocalTransactionMetadata> getAllLocalTransactionMetadata() {
        Set<String> setKeySet = this.sharedPreferences.getAll().keySet();
        ArrayList<String> arrayList = new ArrayList();
        for (Object obj : setKeySet) {
            String it = (String) obj;
            o.g(it, "it");
            if (w.A0(it, KEY_PREFIX, false)) {
                arrayList.add(obj);
            }
        }
        ArrayList arrayList2 = new ArrayList();
        for (String str : arrayList) {
            String string = this.sharedPreferences.getString(str, null);
            if (string != null) {
                try {
                    arrayList2.add((LocalTransactionMetadata) this.json.b(LocalTransactionMetadata.Companion.serializer(), string));
                } catch (f e6) {
                    LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Failed to deserialize transaction metadata for key: " + str, e6);
                    SharedPreferences.Editor editor = this.sharedPreferences.edit();
                    o.g(editor, "editor");
                    editor.remove(str);
                    editor.apply();
                }
            }
        }
        return arrayList2;
    }

    public final LocalTransactionMetadata getLocalTransactionMetadata(String purchaseToken) {
        o.h(purchaseToken, "purchaseToken");
        String strN = a.n(KEY_PREFIX, getTokenHash(purchaseToken));
        String string = this.sharedPreferences.getString(strN, null);
        if (string == null) {
            return null;
        }
        try {
            return (LocalTransactionMetadata) this.json.b(LocalTransactionMetadata.Companion.serializer(), string);
        } catch (f e6) {
            LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Failed to deserialize local transaction metadata. Clearing cache.", e6);
            SharedPreferences.Editor editor = this.sharedPreferences.edit();
            o.g(editor, "editor");
            editor.remove(strN);
            editor.apply();
            return null;
        }
    }

    public /* synthetic */ LocalTransactionMetadataStore(Context context, String str, SharedPreferences sharedPreferences, AbstractC2458c abstractC2458c, int i6, h hVar) {
        this(context, str, (i6 & 4) != 0 ? Companion.initializeSharedPreferences(context, str) : sharedPreferences, (i6 & 8) != 0 ? JsonTools.INSTANCE.getJson() : abstractC2458c);
    }
}
