package com.revenuecat.purchases;

import J4.e;
import J4.f;
import com.revenuecat.purchases.common.AppConfig;
import com.revenuecat.purchases.common.Backend;
import com.revenuecat.purchases.common.BillingAbstract;
import com.revenuecat.purchases.common.LogUtilsKt;
import com.revenuecat.purchases.common.PostReceiptErrorHandlingBehavior;
import com.revenuecat.purchases.common.ReceiptInfo;
import com.revenuecat.purchases.common.caching.DeviceCache;
import com.revenuecat.purchases.common.caching.LocalTransactionMetadata;
import com.revenuecat.purchases.common.caching.LocalTransactionMetadataStore;
import com.revenuecat.purchases.common.networking.PostReceiptProductInfo;
import com.revenuecat.purchases.common.networking.PostReceiptResponse;
import com.revenuecat.purchases.common.offlineentitlements.OfflineEntitlementsManager;
import com.revenuecat.purchases.models.PurchaseState;
import com.revenuecat.purchases.models.StoreProduct;
import com.revenuecat.purchases.models.StoreTransaction;
import com.revenuecat.purchases.models.SubscriptionOption;
import com.revenuecat.purchases.paywalls.PaywallPresentedCache;
import com.revenuecat.purchases.paywalls.events.PaywallEvent;
import com.revenuecat.purchases.paywalls.events.PaywallPostReceiptData;
import com.revenuecat.purchases.subscriberattributes.BackendHelpersKt;
import com.revenuecat.purchases.subscriberattributes.SubscriberAttribute;
import com.revenuecat.purchases.subscriberattributes.SubscriberAttributesManager;
import com.revenuecat.purchases.utils.Result;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import kotlin.jvm.internal.o;
import kotlin.jvm.internal.p;
import m1.g;
import org.json.JSONObject;
import q.AbstractC2761L;
import w4.C3137A;
import x4.AbstractC3251q;
import x4.AbstractC3252r;

/* JADX INFO: loaded from: classes3.dex */
public final class PostReceiptHelper {
    private final AppConfig appConfig;
    private final Backend backend;
    private final BillingAbstract billing;
    private final CustomerInfoUpdateHandler customerInfoUpdateHandler;
    private final DeviceCache deviceCache;
    private final LocalTransactionMetadataStore localTransactionMetadataStore;
    private final OfflineEntitlementsManager offlineEntitlementsManager;
    private final PaywallPresentedCache paywallPresentedCache;
    private final SubscriberAttributesManager subscriberAttributesManager;

    public static final class CachedDataToPost {
        private final boolean didCacheData;
        private final LocalTransactionMetadata localTransactionMetadata;
        private final PaywallEvent paywallEvent;

        public CachedDataToPost(LocalTransactionMetadata localTransactionMetadata, PaywallEvent paywallEvent, boolean z6) {
            this.localTransactionMetadata = localTransactionMetadata;
            this.paywallEvent = paywallEvent;
            this.didCacheData = z6;
        }

        public static /* synthetic */ CachedDataToPost copy$default(CachedDataToPost cachedDataToPost, LocalTransactionMetadata localTransactionMetadata, PaywallEvent paywallEvent, boolean z6, int i6, Object obj) {
            if ((i6 & 1) != 0) {
                localTransactionMetadata = cachedDataToPost.localTransactionMetadata;
            }
            if ((i6 & 2) != 0) {
                paywallEvent = cachedDataToPost.paywallEvent;
            }
            if ((i6 & 4) != 0) {
                z6 = cachedDataToPost.didCacheData;
            }
            return cachedDataToPost.copy(localTransactionMetadata, paywallEvent, z6);
        }

        public final LocalTransactionMetadata component1() {
            return this.localTransactionMetadata;
        }

        public final PaywallEvent component2() {
            return this.paywallEvent;
        }

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

        public final CachedDataToPost copy(LocalTransactionMetadata localTransactionMetadata, PaywallEvent paywallEvent, boolean z6) {
            return new CachedDataToPost(localTransactionMetadata, paywallEvent, z6);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof CachedDataToPost)) {
                return false;
            }
            CachedDataToPost cachedDataToPost = (CachedDataToPost) obj;
            return o.c(this.localTransactionMetadata, cachedDataToPost.localTransactionMetadata) && o.c(this.paywallEvent, cachedDataToPost.paywallEvent) && this.didCacheData == cachedDataToPost.didCacheData;
        }

        public final boolean getDidCacheData() {
            return this.didCacheData;
        }

        public final LocalTransactionMetadata getLocalTransactionMetadata() {
            return this.localTransactionMetadata;
        }

        public final PaywallEvent getPaywallEvent() {
            return this.paywallEvent;
        }

        public int hashCode() {
            LocalTransactionMetadata localTransactionMetadata = this.localTransactionMetadata;
            int iHashCode = (localTransactionMetadata == null ? 0 : localTransactionMetadata.hashCode()) * 31;
            PaywallEvent paywallEvent = this.paywallEvent;
            return Boolean.hashCode(this.didCacheData) + ((iHashCode + (paywallEvent != null ? paywallEvent.hashCode() : 0)) * 31);
        }

        public String toString() {
            StringBuilder sb = new StringBuilder("CachedDataToPost(localTransactionMetadata=");
            sb.append(this.localTransactionMetadata);
            sb.append(", paywallEvent=");
            sb.append(this.paywallEvent);
            sb.append(", didCacheData=");
            return AbstractC2761L.h(sb, this.didCacheData, ')');
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$calculateOfflineCustomerInfo$1, reason: invalid class name */
    public static final class AnonymousClass1 extends p implements J4.c {
        final /* synthetic */ J4.c $onSuccess;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass1(J4.c cVar) {
            super(1);
            this.$onSuccess = cVar;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((CustomerInfo) obj);
            return C3137A.f25453a;
        }

        public final void invoke(CustomerInfo customerInfo) {
            o.h(customerInfo, "customerInfo");
            PostReceiptHelper.this.customerInfoUpdateHandler.notifyListeners(customerInfo);
            this.$onSuccess.invoke(customerInfo);
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$calculateOfflineCustomerInfo$2, reason: invalid class name */
    public static final class AnonymousClass2 extends p implements J4.c {
        final /* synthetic */ J4.c $onError;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass2(J4.c cVar) {
            super(1);
            this.$onError = cVar;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((PurchasesError) obj);
            return C3137A.f25453a;
        }

        public final void invoke(PurchasesError error) {
            o.h(error, "error");
            this.$onError.invoke(error);
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$performPostReceipt$1, reason: invalid class name and case insensitive filesystem */
    public static final class C13621 extends p implements J4.c {
        final /* synthetic */ String $appUserID;
        final /* synthetic */ boolean $hasCachedTransactionMetadata;
        final /* synthetic */ PostReceiptInitiationSource $initiationSource;
        final /* synthetic */ boolean $isRestore;
        final /* synthetic */ f $onError;
        final /* synthetic */ J4.c $onSuccess;
        final /* synthetic */ PaywallPostReceiptData $paywallData;
        final /* synthetic */ String $purchaseToken;
        final /* synthetic */ PurchasesAreCompletedBy $purchasesAreCompletedBy;
        final /* synthetic */ ReceiptInfo $receiptInfo;

        /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$performPostReceipt$1$1, reason: invalid class name and collision with other inner class name */
        public static final class C00111 extends p implements J4.c {
            final /* synthetic */ String $appUserID;
            final /* synthetic */ boolean $hasCachedTransactionMetadata;
            final /* synthetic */ J4.c $onSuccess;
            final /* synthetic */ String $purchaseToken;
            final /* synthetic */ Map<String, SubscriberAttribute> $unsyncedSubscriberAttributesByKey;
            final /* synthetic */ PostReceiptHelper this$0;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            public C00111(boolean z6, PostReceiptHelper postReceiptHelper, String str, String str2, Map<String, SubscriberAttribute> map, J4.c cVar) {
                super(1);
                this.$hasCachedTransactionMetadata = z6;
                this.this$0 = postReceiptHelper;
                this.$purchaseToken = str;
                this.$appUserID = str2;
                this.$unsyncedSubscriberAttributesByKey = map;
                this.$onSuccess = cVar;
            }

            @Override // J4.c
            public /* bridge */ /* synthetic */ Object invoke(Object obj) {
                invoke((PostReceiptResponse) obj);
                return C3137A.f25453a;
            }

            public final void invoke(PostReceiptResponse postReceiptResponse) {
                o.h(postReceiptResponse, "postReceiptResponse");
                if (this.$hasCachedTransactionMetadata) {
                    this.this$0.localTransactionMetadataStore.clearLocalTransactionMetadata(g.o(this.$purchaseToken));
                }
                this.this$0.offlineEntitlementsManager.resetOfflineCustomerInfoCache();
                this.this$0.subscriberAttributesManager.markAsSynced(this.$appUserID, this.$unsyncedSubscriberAttributesByKey, BackendHelpersKt.getAttributeErrors(postReceiptResponse.getBody()));
                this.this$0.customerInfoUpdateHandler.cacheAndNotifyListeners(postReceiptResponse.getCustomerInfo());
                this.$onSuccess.invoke(postReceiptResponse);
            }
        }

        /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$performPostReceipt$1$2, reason: invalid class name */
        public static final class AnonymousClass2 extends p implements f {
            final /* synthetic */ String $appUserID;
            final /* synthetic */ boolean $hasCachedTransactionMetadata;
            final /* synthetic */ f $onError;
            final /* synthetic */ String $purchaseToken;
            final /* synthetic */ Map<String, SubscriberAttribute> $unsyncedSubscriberAttributesByKey;
            final /* synthetic */ PostReceiptHelper this$0;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            public AnonymousClass2(boolean z6, PostReceiptHelper postReceiptHelper, String str, String str2, Map<String, SubscriberAttribute> map, f fVar) {
                super(3);
                this.$hasCachedTransactionMetadata = z6;
                this.this$0 = postReceiptHelper;
                this.$purchaseToken = str;
                this.$appUserID = str2;
                this.$unsyncedSubscriberAttributesByKey = map;
                this.$onError = fVar;
            }

            @Override // J4.f
            public /* bridge */ /* synthetic */ Object invoke(Object obj, Object obj2, Object obj3) {
                invoke((PurchasesError) obj, (PostReceiptErrorHandlingBehavior) obj2, (JSONObject) obj3);
                return C3137A.f25453a;
            }

            public final void invoke(PurchasesError error, PostReceiptErrorHandlingBehavior errorHandlingBehavior, JSONObject jSONObject) {
                o.h(error, "error");
                o.h(errorHandlingBehavior, "errorHandlingBehavior");
                if (errorHandlingBehavior == PostReceiptErrorHandlingBehavior.SHOULD_BE_MARKED_SYNCED) {
                    if (this.$hasCachedTransactionMetadata) {
                        this.this$0.localTransactionMetadataStore.clearLocalTransactionMetadata(g.o(this.$purchaseToken));
                    }
                    this.this$0.subscriberAttributesManager.markAsSynced(this.$appUserID, this.$unsyncedSubscriberAttributesByKey, BackendHelpersKt.getAttributeErrors(jSONObject));
                }
                this.$onError.invoke(error, errorHandlingBehavior, jSONObject);
            }
        }

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C13621(String str, String str2, boolean z6, ReceiptInfo receiptInfo, PostReceiptInitiationSource postReceiptInitiationSource, PaywallPostReceiptData paywallPostReceiptData, PurchasesAreCompletedBy purchasesAreCompletedBy, boolean z7, J4.c cVar, f fVar) {
            super(1);
            this.$purchaseToken = str;
            this.$appUserID = str2;
            this.$isRestore = z6;
            this.$receiptInfo = receiptInfo;
            this.$initiationSource = postReceiptInitiationSource;
            this.$paywallData = paywallPostReceiptData;
            this.$purchasesAreCompletedBy = purchasesAreCompletedBy;
            this.$hasCachedTransactionMetadata = z7;
            this.$onSuccess = cVar;
            this.$onError = fVar;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((Map<String, SubscriberAttribute>) obj);
            return C3137A.f25453a;
        }

        public final void invoke(Map<String, SubscriberAttribute> unsyncedSubscriberAttributesByKey) {
            o.h(unsyncedSubscriberAttributesByKey, "unsyncedSubscriberAttributesByKey");
            PostReceiptHelper.this.backend.postReceiptData(this.$purchaseToken, this.$appUserID, this.$isRestore, PostReceiptHelper.this.getFinishTransactions(), BackendHelpersKt.toBackendMap(unsyncedSubscriberAttributesByKey), this.$receiptInfo, this.$initiationSource, this.$paywallData, this.$purchasesAreCompletedBy, new C00111(this.$hasCachedTransactionMetadata, PostReceiptHelper.this, this.$purchaseToken, this.$appUserID, unsyncedSubscriberAttributesByKey, this.$onSuccess), new AnonymousClass2(this.$hasCachedTransactionMetadata, PostReceiptHelper.this, this.$purchaseToken, this.$appUserID, unsyncedSubscriberAttributesByKey, this.$onError));
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$postTokenWithoutConsuming$1, reason: invalid class name and case insensitive filesystem */
    public static final class C13631 extends p implements J4.c {
        final /* synthetic */ Boolean $isAutoRenewing;
        final /* synthetic */ J4.c $onSuccess;
        final /* synthetic */ String $purchaseToken;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C13631(String str, Boolean bool, J4.c cVar) {
            super(1);
            this.$purchaseToken = str;
            this.$isAutoRenewing = bool;
            this.$onSuccess = cVar;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((PostReceiptResponse) obj);
            return C3137A.f25453a;
        }

        public final void invoke(PostReceiptResponse postReceiptResponse) {
            o.h(postReceiptResponse, "postReceiptResponse");
            PostReceiptHelper.this.deviceCache.addSuccessfullyPostedToken(this.$purchaseToken, this.$isAutoRenewing);
            this.$onSuccess.invoke(postReceiptResponse.getCustomerInfo());
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$postTokenWithoutConsuming$2, reason: invalid class name and case insensitive filesystem */
    public static final class C13642 extends p implements f {
        final /* synthetic */ String $appUserID;
        final /* synthetic */ Boolean $isAutoRenewing;
        final /* synthetic */ J4.c $onError;
        final /* synthetic */ J4.c $onSuccess;
        final /* synthetic */ String $purchaseToken;

        /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$postTokenWithoutConsuming$2$1, reason: invalid class name */
        public static final class AnonymousClass1 extends p implements J4.c {
            final /* synthetic */ J4.c $onSuccess;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            public AnonymousClass1(J4.c cVar) {
                super(1);
                this.$onSuccess = cVar;
            }

            @Override // J4.c
            public /* bridge */ /* synthetic */ Object invoke(Object obj) {
                invoke((CustomerInfo) obj);
                return C3137A.f25453a;
            }

            public final void invoke(CustomerInfo it) {
                o.h(it, "it");
                this.$onSuccess.invoke(it);
            }
        }

        /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$postTokenWithoutConsuming$2$2, reason: invalid class name and collision with other inner class name */
        public static final class C00122 extends p implements J4.a {
            final /* synthetic */ PurchasesError $backendError;
            final /* synthetic */ J4.c $onError;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            public C00122(J4.c cVar, PurchasesError purchasesError) {
                super(0);
                this.$onError = cVar;
                this.$backendError = purchasesError;
            }

            @Override // J4.a
            public /* bridge */ /* synthetic */ Object invoke() {
                m409invoke();
                return C3137A.f25453a;
            }

            /* JADX INFO: renamed from: invoke, reason: collision with other method in class */
            public final void m409invoke() {
                this.$onError.invoke(this.$backendError);
            }
        }

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C13642(String str, Boolean bool, String str2, J4.c cVar, J4.c cVar2) {
            super(3);
            this.$purchaseToken = str;
            this.$isAutoRenewing = bool;
            this.$appUserID = str2;
            this.$onSuccess = cVar;
            this.$onError = cVar2;
        }

        @Override // J4.f
        public /* bridge */ /* synthetic */ Object invoke(Object obj, Object obj2, Object obj3) {
            invoke((PurchasesError) obj, (PostReceiptErrorHandlingBehavior) obj2, (JSONObject) obj3);
            return C3137A.f25453a;
        }

        public final void invoke(PurchasesError backendError, PostReceiptErrorHandlingBehavior errorHandlingBehavior, JSONObject jSONObject) {
            o.h(backendError, "backendError");
            o.h(errorHandlingBehavior, "errorHandlingBehavior");
            if (errorHandlingBehavior == PostReceiptErrorHandlingBehavior.SHOULD_BE_MARKED_SYNCED) {
                PostReceiptHelper.this.deviceCache.addSuccessfullyPostedToken(this.$purchaseToken, this.$isAutoRenewing);
            }
            PostReceiptHelper.this.useOfflineEntitlementsCustomerInfoIfNeeded(errorHandlingBehavior, this.$appUserID, new AnonymousClass1(this.$onSuccess), new C00122(this.$onError, backendError));
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$postTransactionAndConsumeIfNeeded$1, reason: invalid class name and case insensitive filesystem */
    public static final class C13651 extends p implements J4.c {
        final /* synthetic */ PostReceiptInitiationSource $initiationSource;
        final /* synthetic */ e $onSuccess;
        final /* synthetic */ StoreTransaction $purchase;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C13651(StoreTransaction storeTransaction, PostReceiptInitiationSource postReceiptInitiationSource, e eVar) {
            super(1);
            this.$purchase = storeTransaction;
            this.$initiationSource = postReceiptInitiationSource;
            this.$onSuccess = eVar;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((PostReceiptResponse) obj);
            return C3137A.f25453a;
        }

        /* JADX WARN: Removed duplicated region for block: B:17:0x005d  */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final void invoke(PostReceiptResponse postReceiptResponse) {
            boolean zBooleanValue;
            PostReceiptProductInfo postReceiptProductInfo;
            Boolean shouldConsume;
            o.h(postReceiptResponse, "postReceiptResponse");
            Map<String, PostReceiptProductInfo> productInfoByProductId = postReceiptResponse.getProductInfoByProductId();
            if (productInfoByProductId != null) {
                StoreTransaction storeTransaction = this.$purchase;
                LinkedHashMap linkedHashMap = new LinkedHashMap();
                for (Map.Entry<String, PostReceiptProductInfo> entry : productInfoByProductId.entrySet()) {
                    if (storeTransaction.getProductIds().contains(entry.getKey())) {
                        linkedHashMap.put(entry.getKey(), entry.getValue());
                    }
                }
                Collection collectionValues = linkedHashMap.values();
                zBooleanValue = (collectionValues == null || (postReceiptProductInfo = (PostReceiptProductInfo) AbstractC3251q.f0(collectionValues)) == null || (shouldConsume = postReceiptProductInfo.getShouldConsume()) == null) ? false : shouldConsume.booleanValue();
            }
            PostReceiptHelper.this.billing.consumeAndSave(PostReceiptHelper.this.getFinishTransactions(), this.$purchase, zBooleanValue, this.$initiationSource);
            e eVar = this.$onSuccess;
            if (eVar != null) {
                eVar.invoke(this.$purchase, postReceiptResponse.getCustomerInfo());
            }
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$postTransactionAndConsumeIfNeeded$2, reason: invalid class name and case insensitive filesystem */
    public static final class C13662 extends p implements f {
        final /* synthetic */ String $appUserID;
        final /* synthetic */ PostReceiptInitiationSource $initiationSource;
        final /* synthetic */ e $onError;
        final /* synthetic */ e $onSuccess;
        final /* synthetic */ StoreTransaction $purchase;

        /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$postTransactionAndConsumeIfNeeded$2$1, reason: invalid class name */
        public static final class AnonymousClass1 extends p implements J4.c {
            final /* synthetic */ e $onSuccess;
            final /* synthetic */ StoreTransaction $purchase;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            public AnonymousClass1(e eVar, StoreTransaction storeTransaction) {
                super(1);
                this.$onSuccess = eVar;
                this.$purchase = storeTransaction;
            }

            @Override // J4.c
            public /* bridge */ /* synthetic */ Object invoke(Object obj) {
                invoke((CustomerInfo) obj);
                return C3137A.f25453a;
            }

            public final void invoke(CustomerInfo customerInfo) {
                o.h(customerInfo, "customerInfo");
                e eVar = this.$onSuccess;
                if (eVar != null) {
                    eVar.invoke(this.$purchase, customerInfo);
                }
            }
        }

        /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$postTransactionAndConsumeIfNeeded$2$2, reason: invalid class name and collision with other inner class name */
        public static final class C00132 extends p implements J4.a {
            final /* synthetic */ PurchasesError $backendError;
            final /* synthetic */ e $onError;
            final /* synthetic */ StoreTransaction $purchase;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            public C00132(e eVar, StoreTransaction storeTransaction, PurchasesError purchasesError) {
                super(0);
                this.$onError = eVar;
                this.$purchase = storeTransaction;
                this.$backendError = purchasesError;
            }

            @Override // J4.a
            public /* bridge */ /* synthetic */ Object invoke() {
                m410invoke();
                return C3137A.f25453a;
            }

            /* JADX INFO: renamed from: invoke, reason: collision with other method in class */
            public final void m410invoke() {
                e eVar = this.$onError;
                if (eVar != null) {
                    eVar.invoke(this.$purchase, this.$backendError);
                }
            }
        }

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C13662(StoreTransaction storeTransaction, PostReceiptInitiationSource postReceiptInitiationSource, String str, e eVar, e eVar2) {
            super(3);
            this.$purchase = storeTransaction;
            this.$initiationSource = postReceiptInitiationSource;
            this.$appUserID = str;
            this.$onSuccess = eVar;
            this.$onError = eVar2;
        }

        @Override // J4.f
        public /* bridge */ /* synthetic */ Object invoke(Object obj, Object obj2, Object obj3) {
            invoke((PurchasesError) obj, (PostReceiptErrorHandlingBehavior) obj2, (JSONObject) obj3);
            return C3137A.f25453a;
        }

        public final void invoke(PurchasesError backendError, PostReceiptErrorHandlingBehavior errorHandlingBehavior, JSONObject jSONObject) {
            o.h(backendError, "backendError");
            o.h(errorHandlingBehavior, "errorHandlingBehavior");
            if (errorHandlingBehavior == PostReceiptErrorHandlingBehavior.SHOULD_BE_MARKED_SYNCED) {
                PostReceiptHelper.this.billing.consumeAndSave(PostReceiptHelper.this.getFinishTransactions(), this.$purchase, false, this.$initiationSource);
            }
            PostReceiptHelper.this.useOfflineEntitlementsCustomerInfoIfNeeded(errorHandlingBehavior, this.$appUserID, new AnonymousClass1(this.$onSuccess, this.$purchase), new C00132(this.$onError, this.$purchase, backendError));
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.PostReceiptHelper$useOfflineEntitlementsCustomerInfoIfNeeded$1, reason: invalid class name and case insensitive filesystem */
    public static final class C13671 extends p implements J4.c {
        final /* synthetic */ J4.a $onError;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C13671(J4.a aVar) {
            super(1);
            this.$onError = aVar;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((PurchasesError) obj);
            return C3137A.f25453a;
        }

        public final void invoke(PurchasesError it) {
            o.h(it, "it");
            this.$onError.invoke();
        }
    }

    public PostReceiptHelper(AppConfig appConfig, Backend backend, BillingAbstract billing, CustomerInfoUpdateHandler customerInfoUpdateHandler, DeviceCache deviceCache, SubscriberAttributesManager subscriberAttributesManager, OfflineEntitlementsManager offlineEntitlementsManager, PaywallPresentedCache paywallPresentedCache, LocalTransactionMetadataStore localTransactionMetadataStore) {
        o.h(appConfig, "appConfig");
        o.h(backend, "backend");
        o.h(billing, "billing");
        o.h(customerInfoUpdateHandler, "customerInfoUpdateHandler");
        o.h(deviceCache, "deviceCache");
        o.h(subscriberAttributesManager, "subscriberAttributesManager");
        o.h(offlineEntitlementsManager, "offlineEntitlementsManager");
        o.h(paywallPresentedCache, "paywallPresentedCache");
        o.h(localTransactionMetadataStore, "localTransactionMetadataStore");
        this.appConfig = appConfig;
        this.backend = backend;
        this.billing = billing;
        this.customerInfoUpdateHandler = customerInfoUpdateHandler;
        this.deviceCache = deviceCache;
        this.subscriberAttributesManager = subscriberAttributesManager;
        this.offlineEntitlementsManager = offlineEntitlementsManager;
        this.paywallPresentedCache = paywallPresentedCache;
        this.localTransactionMetadataStore = localTransactionMetadataStore;
    }

    private final void calculateOfflineCustomerInfo(String str, J4.c cVar, J4.c cVar2) {
        this.offlineEntitlementsManager.calculateAndCacheOfflineCustomerInfo(str, new AnonymousClass1(cVar), new AnonymousClass2(cVar2));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void callTransactionMetadataCompletionFromResults(List<LocalTransactionMetadata> list, ConcurrentLinkedQueue<Result<CustomerInfo, PurchasesError>> concurrentLinkedQueue, J4.c cVar, J4.c cVar2) {
        if (list.size() == concurrentLinkedQueue.size()) {
            int i6 = 0;
            for (Object obj : concurrentLinkedQueue) {
                int i7 = i6 + 1;
                if (i6 < 0) {
                    AbstractC3252r.F();
                    throw null;
                }
                Result result = (Result) obj;
                if (result instanceof Result.Error) {
                    if (cVar != null) {
                        cVar.invoke(((Result.Error) result).getValue());
                        return;
                    }
                    return;
                } else {
                    if (i6 == concurrentLinkedQueue.size() - 1 && cVar2 != null) {
                        o.f(result, "null cannot be cast to non-null type com.revenuecat.purchases.utils.Result.Success<com.revenuecat.purchases.CustomerInfo>");
                        cVar2.invoke(((Result.Success) result).getValue());
                    }
                    i6 = i7;
                }
            }
        }
    }

    public static /* synthetic */ void callTransactionMetadataCompletionFromResults$default(PostReceiptHelper postReceiptHelper, List list, ConcurrentLinkedQueue concurrentLinkedQueue, J4.c cVar, J4.c cVar2, int i6, Object obj) {
        if ((i6 & 4) != 0) {
            cVar = null;
        }
        if ((i6 & 8) != 0) {
            cVar2 = null;
        }
        postReceiptHelper.callTransactionMetadataCompletionFromResults(list, concurrentLinkedQueue, cVar, cVar2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final boolean getFinishTransactions() {
        return this.appConfig.getFinishTransactions();
    }

    private final synchronized CachedDataToPost getOrPutDataToPost(LocalTransactionMetadataStore localTransactionMetadataStore, String str, ReceiptInfo receiptInfo, PostReceiptInitiationSource postReceiptInitiationSource) {
        LocalTransactionMetadata localTransactionMetadata;
        boolean z6;
        PaywallEvent andRemovePurchaseInitiatedEventIfNeeded;
        PaywallEvent.Data data;
        try {
            localTransactionMetadata = localTransactionMetadataStore.getLocalTransactionMetadata(str);
            z6 = localTransactionMetadata == null && postReceiptInitiationSource == PostReceiptInitiationSource.PURCHASE;
            andRemovePurchaseInitiatedEventIfNeeded = localTransactionMetadata == null ? this.paywallPresentedCache.getAndRemovePurchaseInitiatedEventIfNeeded(receiptInfo.getProductIDs(), receiptInfo.getPurchaseTime()) : null;
            if (z6) {
                PresentedOfferingContext presentedOfferingContext = (andRemovePurchaseInitiatedEventIfNeeded == null || (data = andRemovePurchaseInitiatedEventIfNeeded.getData()) == null) ? null : data.getPresentedOfferingContext();
                localTransactionMetadataStore.cacheLocalTransactionMetadata(str, new LocalTransactionMetadata(str, (receiptInfo.getPresentedOfferingContext() != null || presentedOfferingContext == null) ? receiptInfo : ReceiptInfo.copy$default(receiptInfo, null, null, presentedOfferingContext, null, null, null, null, null, null, null, false, null, null, 8187, null), andRemovePurchaseInitiatedEventIfNeeded != null ? andRemovePurchaseInitiatedEventIfNeeded.toPaywallPostReceiptData$purchases_defaultsBc8Release() : null, getPurchasesAreCompletedBy()));
            }
        } catch (Throwable th) {
            throw th;
        }
        return new CachedDataToPost(localTransactionMetadata, andRemovePurchaseInitiatedEventIfNeeded, z6);
    }

    private final PurchasesAreCompletedBy getPurchasesAreCompletedBy() {
        return this.appConfig.getPurchasesAreCompletedBy();
    }

    private final void performPostReceipt(String str, String str2, boolean z6, ReceiptInfo receiptInfo, PostReceiptInitiationSource postReceiptInitiationSource, PaywallPostReceiptData paywallPostReceiptData, PurchasesAreCompletedBy purchasesAreCompletedBy, boolean z7, J4.c cVar, f fVar) {
        this.subscriberAttributesManager.getUnsyncedSubscriberAttributes(str, new C13621(str2, str, z6, receiptInfo, postReceiptInitiationSource, paywallPostReceiptData, purchasesAreCompletedBy, z7, cVar, fVar));
    }

    /* JADX WARN: Multi-variable type inference failed */
    private final void postReceiptAndSubscriberAttributes(String str, String str2, boolean z6, ReceiptInfo receiptInfo, PostReceiptInitiationSource postReceiptInitiationSource, PurchaseState purchaseState, J4.c cVar, f fVar) {
        PaywallPostReceiptData paywallPostReceiptData;
        ReceiptInfo receiptInfo2;
        PurchasesAreCompletedBy purchasesAreCompletedBy;
        CachedDataToPost orPutDataToPost = getOrPutDataToPost(this.localTransactionMetadataStore, str2, receiptInfo, postReceiptInitiationSource);
        LocalTransactionMetadata localTransactionMetadataComponent1 = orPutDataToPost.component1();
        PaywallEvent paywallEventComponent2 = orPutDataToPost.component2();
        boolean zComponent3 = orPutDataToPost.component3();
        String str3 = null;
        Object[] objArr = 0;
        if (localTransactionMetadataComponent1 != null && (paywallPostReceiptData$purchases_defaultsBc8Release = localTransactionMetadataComponent1.getPaywallPostReceiptData()) != null) {
            paywallPostReceiptData = paywallPostReceiptData$purchases_defaultsBc8Release;
        } else if (paywallEventComponent2 != null) {
            PaywallPostReceiptData paywallPostReceiptData$purchases_defaultsBc8Release = paywallEventComponent2.toPaywallPostReceiptData$purchases_defaultsBc8Release();
            paywallPostReceiptData = paywallPostReceiptData$purchases_defaultsBc8Release;
        } else {
            paywallPostReceiptData = null;
        }
        if (localTransactionMetadataComponent1 == null || (receiptInfo2 = localTransactionMetadataComponent1.getReceiptInfo()) == null) {
            receiptInfo2 = receiptInfo;
        }
        if (localTransactionMetadataComponent1 == null || (purchasesAreCompletedBy = localTransactionMetadataComponent1.getPurchasesAreCompletedBy()) == null) {
            purchasesAreCompletedBy = getPurchasesAreCompletedBy();
        }
        if (purchaseState != PurchaseState.PENDING) {
            performPostReceipt(str, str2, z6, receiptInfo2, postReceiptInitiationSource, paywallPostReceiptData, purchasesAreCompletedBy, localTransactionMetadataComponent1 != null || zComponent3, cVar, fVar);
            return;
        }
        PurchasesError purchasesError = new PurchasesError(PurchasesErrorCode.PaymentPendingError, str3, 2, objArr == true ? 1 : 0);
        LogUtilsKt.errorLog(purchasesError);
        fVar.invoke(purchasesError, PostReceiptErrorHandlingBehavior.SHOULD_NOT_CONSUME, null);
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static /* synthetic */ void postTransactionAndConsumeIfNeeded$default(PostReceiptHelper postReceiptHelper, StoreTransaction storeTransaction, StoreProduct storeProduct, Map map, boolean z6, String str, PostReceiptInitiationSource postReceiptInitiationSource, boolean z7, e eVar, e eVar2, int i6, Object obj) {
        e eVar3;
        PostReceiptHelper postReceiptHelper2;
        StoreTransaction storeTransaction2;
        StoreProduct storeProduct2;
        Map map2;
        boolean z8;
        String str2;
        PostReceiptInitiationSource postReceiptInitiationSource2;
        boolean z9 = (i6 & 64) != 0 ? false : z7;
        e eVar4 = (i6 & 128) != 0 ? null : eVar;
        if ((i6 & 256) != 0) {
            eVar3 = null;
            storeTransaction2 = storeTransaction;
            storeProduct2 = storeProduct;
            map2 = map;
            z8 = z6;
            str2 = str;
            postReceiptInitiationSource2 = postReceiptInitiationSource;
            postReceiptHelper2 = postReceiptHelper;
        } else {
            eVar3 = eVar2;
            postReceiptHelper2 = postReceiptHelper;
            storeTransaction2 = storeTransaction;
            storeProduct2 = storeProduct;
            map2 = map;
            z8 = z6;
            str2 = str;
            postReceiptInitiationSource2 = postReceiptInitiationSource;
        }
        postReceiptHelper2.postTransactionAndConsumeIfNeeded(storeTransaction2, storeProduct2, map2, z8, str2, postReceiptInitiationSource2, z9, eVar4, eVar3);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void useOfflineEntitlementsCustomerInfoIfNeeded(PostReceiptErrorHandlingBehavior postReceiptErrorHandlingBehavior, String str, J4.c cVar, J4.a aVar) {
        if (this.offlineEntitlementsManager.shouldCalculateOfflineCustomerInfoInPostReceipt(postReceiptErrorHandlingBehavior == PostReceiptErrorHandlingBehavior.SHOULD_USE_OFFLINE_ENTITLEMENTS_AND_NOT_CONSUME)) {
            calculateOfflineCustomerInfo(str, cVar, new C13671(aVar));
        } else {
            aVar.invoke();
        }
    }

    public final void postRemainingCachedTransactionMetadata(String str, boolean z6, Set<String> pendingTransactionsTokens, J4.a onNoTransactionsToSync, J4.c cVar, J4.c cVar2) {
        String appUserID = str;
        o.h(appUserID, "appUserID");
        o.h(pendingTransactionsTokens, "pendingTransactionsTokens");
        o.h(onNoTransactionsToSync, "onNoTransactionsToSync");
        J4.c onError = cVar;
        o.h(onError, "onError");
        J4.c onSuccess = cVar2;
        o.h(onSuccess, "onSuccess");
        ConcurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue();
        PostReceiptHelper postReceiptHelper = this;
        List<LocalTransactionMetadata> allLocalTransactionMetadata = postReceiptHelper.localTransactionMetadataStore.getAllLocalTransactionMetadata();
        ArrayList<LocalTransactionMetadata> arrayList = new ArrayList();
        for (Object obj : allLocalTransactionMetadata) {
            if (!pendingTransactionsTokens.contains(((LocalTransactionMetadata) obj).getToken())) {
                arrayList.add(obj);
            }
        }
        if (arrayList.isEmpty()) {
            onNoTransactionsToSync.invoke();
            return;
        }
        for (LocalTransactionMetadata localTransactionMetadata : arrayList) {
            performPostReceipt(appUserID, localTransactionMetadata.getToken(), z6, localTransactionMetadata.getReceiptInfo(), PostReceiptInitiationSource.UNSYNCED_ACTIVE_PURCHASES, localTransactionMetadata.getPaywallPostReceiptData(), localTransactionMetadata.getPurchasesAreCompletedBy(), true, new PostReceiptHelper$postRemainingCachedTransactionMetadata$1$1(concurrentLinkedQueue, postReceiptHelper, arrayList, onError, onSuccess), new PostReceiptHelper$postRemainingCachedTransactionMetadata$1$2(concurrentLinkedQueue, this, arrayList, cVar, cVar2));
            postReceiptHelper = this;
            appUserID = str;
            onError = cVar;
            onSuccess = cVar2;
            concurrentLinkedQueue = concurrentLinkedQueue;
            arrayList = arrayList;
        }
    }

    public final void postTokenWithoutConsuming(String purchaseToken, ReceiptInfo receiptInfo, boolean z6, String appUserID, PostReceiptInitiationSource initiationSource, J4.c onSuccess, J4.c onError, Boolean bool) {
        o.h(purchaseToken, "purchaseToken");
        o.h(receiptInfo, "receiptInfo");
        o.h(appUserID, "appUserID");
        o.h(initiationSource, "initiationSource");
        o.h(onSuccess, "onSuccess");
        o.h(onError, "onError");
        postReceiptAndSubscriberAttributes(appUserID, purchaseToken, z6, receiptInfo, initiationSource, PurchaseState.UNSPECIFIED_STATE, new C13631(purchaseToken, bool, onSuccess), new C13642(purchaseToken, bool, appUserID, onSuccess, onError));
    }

    public final void postTransactionAndConsumeIfNeeded(StoreTransaction purchase, StoreProduct storeProduct, Map<String, ? extends SubscriptionOption> map, boolean z6, String appUserID, PostReceiptInitiationSource initiationSource, boolean z7, e eVar, e eVar2) {
        o.h(purchase, "purchase");
        o.h(appUserID, "appUserID");
        o.h(initiationSource, "initiationSource");
        postReceiptAndSubscriberAttributes(appUserID, purchase.getPurchaseToken(), z6, ReceiptInfo.Companion.from(purchase, storeProduct, map, z7), initiationSource, purchase.getPurchaseState(), new C13651(purchase, initiationSource, eVar), new C13662(purchase, initiationSource, appUserID, eVar, eVar2));
    }
}
