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

import A4.d;
import A4.l;
import B1.g;
import C4.e;
import C4.j;
import E3.v;
import J4.a;
import J4.c;
import V4.C;
import V4.E;
import Y4.H;
import Y4.J;
import Y4.O;
import Y4.a0;
import Y4.c0;
import a.AbstractC0872a;
import a0.I;
import android.app.Activity;
import android.os.LocaleList;
import androidx.compose.runtime.MutableState;
import androidx.compose.runtime.SnapshotStateKt__SnapshotStateKt;
import androidx.compose.runtime.State;
import androidx.compose.runtime.internal.StabilityInferred;
import androidx.lifecycle.X;
import androidx.lifecycle.b0;
import com.revenuecat.purchases.CustomerInfo;
import com.revenuecat.purchases.Offering;
import com.revenuecat.purchases.Offerings;
import com.revenuecat.purchases.Package;
import com.revenuecat.purchases.PresentedOfferingContext;
import com.revenuecat.purchases.PurchaseParams;
import com.revenuecat.purchases.PurchaseResult;
import com.revenuecat.purchases.PurchasesAreCompletedBy;
import com.revenuecat.purchases.PurchasesError;
import com.revenuecat.purchases.PurchasesErrorCode;
import com.revenuecat.purchases.PurchasesException;
import com.revenuecat.purchases.UiConfig;
import com.revenuecat.purchases.models.GoogleStoreProduct;
import com.revenuecat.purchases.models.StoreProduct;
import com.revenuecat.purchases.models.SubscriptionOption;
import com.revenuecat.purchases.paywalls.PaywallData;
import com.revenuecat.purchases.paywalls.components.common.ExitOffer;
import com.revenuecat.purchases.paywalls.components.common.ExitOffers;
import com.revenuecat.purchases.paywalls.components.common.PaywallComponentsData;
import com.revenuecat.purchases.paywalls.components.common.ProductChangeConfig;
import com.revenuecat.purchases.paywalls.events.ExitOfferType;
import com.revenuecat.purchases.paywalls.events.PaywallEvent;
import com.revenuecat.purchases.paywalls.events.PaywallEventType;
import com.revenuecat.purchases.ui.revenuecatui.CustomVariableValue;
import com.revenuecat.purchases.ui.revenuecatui.OfferingSelection;
import com.revenuecat.purchases.ui.revenuecatui.PaywallListener;
import com.revenuecat.purchases.ui.revenuecatui.PaywallMode;
import com.revenuecat.purchases.ui.revenuecatui.PaywallModeKt;
import com.revenuecat.purchases.ui.revenuecatui.PaywallOptions;
import com.revenuecat.purchases.ui.revenuecatui.PaywallPurchaseLogic;
import com.revenuecat.purchases.ui.revenuecatui.PaywallPurchaseLogicParams;
import com.revenuecat.purchases.ui.revenuecatui.ProductChange;
import com.revenuecat.purchases.ui.revenuecatui.PurchaseLogicResult;
import com.revenuecat.purchases.ui.revenuecatui.activity.PaywallResult;
import com.revenuecat.purchases.ui.revenuecatui.components.PaywallAction;
import com.revenuecat.purchases.ui.revenuecatui.data.PaywallState;
import com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel;
import com.revenuecat.purchases.ui.revenuecatui.data.PurchasesType;
import com.revenuecat.purchases.ui.revenuecatui.data.processed.TemplateConfiguration;
import com.revenuecat.purchases.ui.revenuecatui.data.processed.VariableDataProvider;
import com.revenuecat.purchases.ui.revenuecatui.errors.PaywallValidationError;
import com.revenuecat.purchases.ui.revenuecatui.extensions.PackageExtensionsKt;
import com.revenuecat.purchases.ui.revenuecatui.helpers.LocaleHelpersKt;
import com.revenuecat.purchases.ui.revenuecatui.helpers.Logger;
import com.revenuecat.purchases.ui.revenuecatui.helpers.NonEmptyList;
import com.revenuecat.purchases.ui.revenuecatui.helpers.OfferingToStateMapperKt;
import com.revenuecat.purchases.ui.revenuecatui.helpers.PaywallValidationResult;
import com.revenuecat.purchases.ui.revenuecatui.helpers.ResolvedOffer;
import com.revenuecat.purchases.ui.revenuecatui.helpers.ResourceProvider;
import com.revenuecat.purchases.ui.revenuecatui.strings.PaywallValidationErrorStrings;
import com.revenuecat.purchases.ui.revenuecatui.utils.Resumable;
import com.revenuecat.purchases.ui.revenuecatui.utils.URIExtensionsKt;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import kotlin.jvm.internal.p;
import m3.AbstractC2467b;
import w4.C3137A;
import w4.k;
import x4.AbstractC3229F;
import x4.AbstractC3251q;
import x4.z;

/* JADX INFO: loaded from: classes3.dex */
@StabilityInferred(parameters = 1)
public final class PaywallViewModelImpl extends b0 implements PaywallViewModel {
    public static final int $stable = 0;
    private final MutableState<PurchasesError> _actionError;
    private final MutableState<Boolean> _actionInProgress;
    private final H _colorScheme;
    private final H _lastLocaleList;
    private final MutableState<Offering> _preloadedExitOffering;
    private final MutableState<Boolean> _purchaseCompleted;
    private final H _state;
    private boolean isDarkMode;
    private PaywallOptions options;
    private PaywallEvent.Data paywallPresentationData;
    private final ProductChangeCalculator productChangeCalculator;
    private final PurchasesType purchases;
    private final ResourceProvider resourceProvider;
    private final c shouldDisplayBlock;
    private final VariableDataProvider variableDataProvider;

    public /* synthetic */ class WhenMappings {
        public static final /* synthetic */ int[] $EnumSwitchMapping$0;

        static {
            int[] iArr = new int[PurchasesAreCompletedBy.values().length];
            try {
                iArr[PurchasesAreCompletedBy.MY_APP.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                iArr[PurchasesAreCompletedBy.REVENUECAT.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            $EnumSwitchMapping$0 = iArr;
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$calculateState$2, reason: invalid class name */
    public static final class AnonymousClass2 extends p implements a {
        public static final AnonymousClass2 INSTANCE = new AnonymousClass2();

        public AnonymousClass2() {
            super(0);
        }

        @Override // J4.a
        public final Date invoke() {
            return new Date();
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$handlePackagePurchase$1, reason: invalid class name */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl", f = "PaywallViewModel.kt", l = {410, 426}, m = "handlePackagePurchase")
    public static final class AnonymousClass1 extends C4.c {
        Object L$0;
        int label;
        /* synthetic */ Object result;

        public AnonymousClass1(d dVar) {
            super(dVar);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return PaywallViewModelImpl.this.handlePackagePurchase(null, null, null, this);
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$handleRestorePurchases$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl", f = "PaywallViewModel.kt", l = {343, 344, 346, 377}, m = "handleRestorePurchases")
    public static final class C17151 extends C4.c {
        Object L$0;
        Object L$1;
        int label;
        /* synthetic */ Object result;

        public C17151(d dVar) {
            super(dVar);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return PaywallViewModelImpl.this.handleRestorePurchases(this);
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$performPurchase$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl", f = "PaywallViewModel.kt", l = {461, 476, 495, 498, 542}, m = "performPurchase")
    public static final class C17161 extends C4.c {
        Object L$0;
        Object L$1;
        Object L$2;
        Object L$3;
        Object L$4;
        int label;
        /* synthetic */ Object result;

        public C17161(d dVar) {
            super(dVar);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return PaywallViewModelImpl.this.performPurchase(null, null, null, null, this);
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$preloadExitOffering$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$preloadExitOffering$1", f = "PaywallViewModel.kt", l = {229}, m = "invokeSuspend")
    public static final class C17171 extends j implements J4.e {
        Object L$0;
        Object L$1;
        int label;

        public C17171(d dVar) {
            super(2, dVar);
        }

        @Override // C4.a
        public final d create(Object obj, d dVar) {
            return PaywallViewModelImpl.this.new C17171(dVar);
        }

        @Override // J4.e
        public final Object invoke(C c6, d dVar) {
            return ((C17171) create(c6, dVar)).invokeSuspend(C3137A.f25453a);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            Offering offering;
            String offeringId;
            MutableState mutableState;
            MutableState mutableState2;
            Offering.PaywallComponents paywallComponents;
            PaywallComponentsData data;
            ExitOffers exitOffers;
            ExitOffer dismiss;
            B4.a aVar = B4.a.f492a;
            int i6 = this.label;
            try {
            } catch (PurchasesException e6) {
                Logger.INSTANCE.e("Failed to preload exit offering", e6);
            }
            if (i6 == 0) {
                AbstractC2467b.l(obj);
                PaywallState paywallState = (PaywallState) ((c0) PaywallViewModelImpl.this._state).getValue();
                offering = null;
                Offering offering2 = paywallState instanceof PaywallState.Loaded.Legacy ? ((PaywallState.Loaded.Legacy) paywallState).getOffering() : paywallState instanceof PaywallState.Loaded.Components ? ((PaywallState.Loaded.Components) paywallState).getOffering() : null;
                offeringId = (offering2 == null || (paywallComponents = offering2.getPaywallComponents()) == null || (data = paywallComponents.getData()) == null || (exitOffers = data.getExitOffers()) == null || (dismiss = exitOffers.getDismiss()) == null) ? null : dismiss.getOfferingId();
                mutableState = PaywallViewModelImpl.this._preloadedExitOffering;
                if (offeringId != null) {
                    PurchasesType purchasesType = PaywallViewModelImpl.this.purchases;
                    this.L$0 = offeringId;
                    this.L$1 = mutableState;
                    this.label = 1;
                    Object objAwaitOfferings = purchasesType.awaitOfferings(this);
                    if (objAwaitOfferings == aVar) {
                        return aVar;
                    }
                    mutableState2 = mutableState;
                    obj = objAwaitOfferings;
                }
                mutableState.setValue(offering);
                return C3137A.f25453a;
            }
            if (i6 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            mutableState2 = (MutableState) this.L$1;
            offeringId = (String) this.L$0;
            AbstractC2467b.l(obj);
            offering = ((Offerings) obj).get(offeringId);
            if (offering == null) {
                Logger.INSTANCE.e("Exit offering with ID '" + offeringId + "' not found in available offerings. Exit offer will not be displayed.");
            }
            mutableState = mutableState2;
            mutableState.setValue(offering);
            return C3137A.f25453a;
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$purchaseSelectedPackage$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$purchaseSelectedPackage$1", f = "PaywallViewModel.kt", l = {289}, m = "invokeSuspend")
    public static final class C17181 extends j implements J4.e {
        final /* synthetic */ Activity $activity;
        int label;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C17181(Activity activity, d dVar) {
            super(2, dVar);
            this.$activity = activity;
        }

        @Override // C4.a
        public final d create(Object obj, d dVar) {
            return PaywallViewModelImpl.this.new C17181(this.$activity, dVar);
        }

        @Override // J4.e
        public final Object invoke(C c6, d dVar) {
            return ((C17181) create(c6, dVar)).invokeSuspend(C3137A.f25453a);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            B4.a aVar = B4.a.f492a;
            int i6 = this.label;
            if (i6 == 0) {
                AbstractC2467b.l(obj);
                PaywallViewModelImpl paywallViewModelImpl = PaywallViewModelImpl.this;
                Activity activity = this.$activity;
                this.label = 1;
                if (PaywallViewModel.DefaultImpls.handlePackagePurchase$default(paywallViewModelImpl, activity, null, null, this, 4, null) == aVar) {
                    return aVar;
                }
            } else {
                if (i6 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                AbstractC2467b.l(obj);
            }
            return C3137A.f25453a;
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$restorePurchases$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$restorePurchases$1", f = "PaywallViewModel.kt", l = {295}, m = "invokeSuspend")
    public static final class C17191 extends j implements J4.e {
        int label;

        public C17191(d dVar) {
            super(2, dVar);
        }

        @Override // C4.a
        public final d create(Object obj, d dVar) {
            return PaywallViewModelImpl.this.new C17191(dVar);
        }

        @Override // J4.e
        public final Object invoke(C c6, d dVar) {
            return ((C17191) create(c6, dVar)).invokeSuspend(C3137A.f25453a);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            B4.a aVar = B4.a.f492a;
            int i6 = this.label;
            if (i6 == 0) {
                AbstractC2467b.l(obj);
                PaywallViewModelImpl paywallViewModelImpl = PaywallViewModelImpl.this;
                this.label = 1;
                if (paywallViewModelImpl.handleRestorePurchases(this) == aVar) {
                    return aVar;
                }
            } else {
                if (i6 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                AbstractC2467b.l(obj);
            }
            return C3137A.f25453a;
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$updateState$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$updateState$1", f = "PaywallViewModel.kt", l = {580, 588}, m = "invokeSuspend")
    public static final class C17201 extends j implements J4.e {
        Object L$0;
        int label;

        public C17201(d dVar) {
            super(2, dVar);
        }

        @Override // C4.a
        public final d create(Object obj, d dVar) {
            return PaywallViewModelImpl.this.new C17201(dVar);
        }

        @Override // J4.e
        public final Object invoke(C c6, d dVar) {
            return ((C17201) create(c6, dVar)).invokeSuspend(C3137A.f25453a);
        }

        /* JADX WARN: Code restructure failed: missing block: B:39:0x008e, code lost:
        
            if (r7 == r0) goto L40;
         */
        /* JADX WARN: Removed duplicated region for block: B:27:0x0069 A[Catch: PurchasesException -> 0x0012, TryCatch #0 {PurchasesException -> 0x0012, blocks: (B:6:0x000d, B:41:0x0091, B:43:0x0099, B:44:0x00af, B:13:0x0021, B:25:0x0054, B:27:0x0069, B:31:0x0073, B:16:0x0028, B:18:0x0036, B:19:0x003d, B:21:0x0041, B:36:0x007e, B:38:0x0082, B:45:0x00e1, B:46:0x00e6), top: B:50:0x0007 }] */
        /* JADX WARN: Removed duplicated region for block: B:28:0x006e  */
        /* JADX WARN: Removed duplicated region for block: B:30:0x0071  */
        @Override // C4.a
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final Object invokeSuspend(Object obj) {
            OfferingSelection offeringSelection;
            Offering offeringType;
            PresentedOfferingContext presentedOfferingContext;
            B4.a aVar = B4.a.f492a;
            int i6 = this.label;
            try {
                if (i6 == 0) {
                    AbstractC2467b.l(obj);
                    OfferingSelection offeringSelection$revenuecatui_defaultsBc8Release = PaywallViewModelImpl.this.options.getOfferingSelection$revenuecatui_defaultsBc8Release();
                    if (!(offeringSelection$revenuecatui_defaultsBc8Release instanceof OfferingSelection.OfferingType)) {
                        if (offeringSelection$revenuecatui_defaultsBc8Release instanceof OfferingSelection.IdAndPresentedOfferingContext) {
                            PurchasesType purchasesType = PaywallViewModelImpl.this.purchases;
                            this.L$0 = offeringSelection$revenuecatui_defaultsBc8Release;
                            this.label = 1;
                            Object objAwaitOfferings = purchasesType.awaitOfferings(this);
                            if (objAwaitOfferings != aVar) {
                                offeringSelection = offeringSelection$revenuecatui_defaultsBc8Release;
                                obj = objAwaitOfferings;
                                Offerings offerings = (Offerings) obj;
                                presentedOfferingContext = ((OfferingSelection.IdAndPresentedOfferingContext) offeringSelection).getPresentedOfferingContext();
                                Offering offering = offerings.get(((OfferingSelection.IdAndPresentedOfferingContext) offeringSelection).getOfferingId());
                                if (offering != null) {
                                }
                                if (presentedOfferingContext != null) {
                                }
                            }
                        } else {
                            if (!(offeringSelection$revenuecatui_defaultsBc8Release instanceof OfferingSelection.None)) {
                                throw new v();
                            }
                            PurchasesType purchasesType2 = PaywallViewModelImpl.this.purchases;
                            this.label = 2;
                            obj = purchasesType2.awaitOfferings(this);
                        }
                        return aVar;
                    }
                    offeringType = ((OfferingSelection.OfferingType) offeringSelection$revenuecatui_defaultsBc8Release).getOfferingType();
                } else if (i6 == 1) {
                    offeringSelection = (OfferingSelection) this.L$0;
                    AbstractC2467b.l(obj);
                    Offerings offerings2 = (Offerings) obj;
                    presentedOfferingContext = ((OfferingSelection.IdAndPresentedOfferingContext) offeringSelection).getPresentedOfferingContext();
                    Offering offering2 = offerings2.get(((OfferingSelection.IdAndPresentedOfferingContext) offeringSelection).getOfferingId());
                    offeringType = offering2 != null ? offerings2.getCurrent() : offering2;
                    if (presentedOfferingContext != null) {
                        Offering offeringCopy = offeringType != null ? offeringType.copy(presentedOfferingContext) : null;
                        if (offeringCopy != null) {
                            offeringType = offeringCopy;
                        }
                    }
                } else {
                    if (i6 != 2) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    AbstractC2467b.l(obj);
                    offeringType = ((Offerings) obj).getCurrent();
                }
                if (offeringType == null) {
                    H h5 = PaywallViewModelImpl.this._state;
                    PaywallState.Error error = new PaywallState.Error("The RevenueCat dashboard does not have a current offering configured.");
                    c0 c0Var = (c0) h5;
                    c0Var.getClass();
                    c0Var.k(null, error);
                } else {
                    H h6 = PaywallViewModelImpl.this._state;
                    PaywallViewModelImpl paywallViewModelImpl = PaywallViewModelImpl.this;
                    ((c0) h6).j(paywallViewModelImpl.calculateState(offeringType, (I) ((c0) paywallViewModelImpl._colorScheme).getValue(), PaywallViewModelImpl.this.purchases.getStorefrontCountryCode(), PaywallViewModelImpl.this.options.getMode$revenuecatui_defaultsBc8Release()));
                }
            } catch (PurchasesException e6) {
                H h7 = PaywallViewModelImpl.this._state;
                PaywallState.Error error2 = new PaywallState.Error("Error " + e6.getCode().getCode() + ": " + e6.getCode().getDescription());
                c0 c0Var2 = (c0) h7;
                c0Var2.getClass();
                c0Var2.k(null, error2);
            }
            return C3137A.f25453a;
        }
    }

    /* JADX WARN: Illegal instructions before constructor call */
    /* JADX WARN: Multi-variable type inference failed */
    public /* synthetic */ PaywallViewModelImpl(ResourceProvider resourceProvider, PurchasesType purchasesType, PaywallOptions paywallOptions, I i6, boolean z6, c cVar, boolean z7, ProductChangeCalculator productChangeCalculator, int i7, h hVar) {
        PurchasesType purchasesImpl = (i7 & 2) != 0 ? new PurchasesImpl(null, 1, 0 == true ? 1 : 0) : purchasesType;
        this(resourceProvider, purchasesImpl, paywallOptions, i6, z6, cVar, (i7 & 64) != 0 ? false : z7, (i7 & 128) != 0 ? new ProductChangeCalculator(purchasesImpl) : productChangeCalculator);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final PaywallState calculateState(Offering offering, I i6, String str, PaywallMode paywallMode) {
        if (offering.getAvailablePackages().isEmpty()) {
            return new PaywallState.Error("No packages available");
        }
        PaywallValidationResult paywallValidationResultValidatedPaywall = OfferingToStateMapperKt.validatedPaywall(offering, i6, getResourceProvider());
        if ((paywallValidationResultValidatedPaywall instanceof PaywallValidationResult.Components) && !PaywallModeKt.isFullScreen(paywallMode)) {
            paywallValidationResultValidatedPaywall = OfferingToStateMapperKt.fallbackPaywall(offering, i6, getResourceProvider(), PaywallValidationError.InvalidModeForComponentsPaywall.INSTANCE);
        }
        NonEmptyList<PaywallValidationError> errors = paywallValidationResultValidatedPaywall.getErrors();
        if (errors != null) {
            Iterator<PaywallValidationError> it = errors.iterator();
            while (it.hasNext()) {
                Logger.INSTANCE.e(it.next().associatedErrorString(offering));
            }
            Logger.INSTANCE.e(PaywallValidationErrorStrings.DISPLAYING_DEFAULT);
        }
        if (paywallValidationResultValidatedPaywall instanceof PaywallValidationResult.Legacy) {
            PaywallValidationResult.Legacy legacy = (PaywallValidationResult.Legacy) paywallValidationResultValidatedPaywall;
            return OfferingToStateMapperKt.toLegacyPaywallState(offering, this.variableDataProvider, paywallMode, legacy.getDisplayablePaywall(), legacy.getTemplate(), this.options.getShouldDisplayDismissButton$revenuecatui_defaultsBc8Release(), str, legacy.getWarning());
        }
        if (paywallValidationResultValidatedPaywall instanceof PaywallValidationResult.Components) {
            return OfferingToStateMapperKt.toComponentsPaywallState(offering, (PaywallValidationResult.Components) paywallValidationResultValidatedPaywall, str, AnonymousClass2.INSTANCE, this.purchases, this.options.getCustomVariables(), extractDefaultCustomVariables(offering));
        }
        throw new v();
    }

    private final PaywallEvent.Data createEventData() {
        PaywallState paywallState = (PaywallState) getState().getValue();
        if (paywallState instanceof PaywallState.Loaded.Legacy) {
            return createEventData((PaywallState.Loaded.Legacy) paywallState);
        }
        if (paywallState instanceof PaywallState.Loaded.Components) {
            return createEventData((PaywallState.Loaded.Components) paywallState);
        }
        if (!(paywallState instanceof PaywallState.Error ? true : paywallState instanceof PaywallState.Loading)) {
            throw new v();
        }
        Logger.INSTANCE.e("Unexpected state trying to create event data: " + paywallState);
        return null;
    }

    private final Map<String, CustomVariableValue> extractDefaultCustomVariables(Offering offering) {
        UiConfig uiConfig;
        Map customVariables;
        Offering.PaywallComponents paywallComponents = offering.getPaywallComponents();
        if (paywallComponents == null || (uiConfig = paywallComponents.getUiConfig()) == null || (customVariables = uiConfig.getCustomVariables()) == null) {
            return z.f26066a;
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap(AbstractC3229F.u(customVariables.size()));
        for (Map.Entry entry : customVariables.entrySet()) {
            linkedHashMap.put(entry.getKey(), CustomVariableValue.Companion.from(((UiConfig.CustomVariableDefinition) entry.getValue()).getDefaultValue()));
        }
        return linkedHashMap;
    }

    private final void finishAction() {
        this._actionInProgress.setValue(Boolean.FALSE);
    }

    private final g getCurrentLocaleList() {
        String preferredUILocaleOverride = this.purchases.getPreferredUILocaleOverride();
        if (preferredUILocaleOverride == null) {
            return g.b();
        }
        try {
            Locale[] localeArr = {LocaleHelpersKt.createLocaleFromString(preferredUILocaleOverride)};
            g gVar = g.f447b;
            return new g(new B1.h(new LocaleList(localeArr)));
        } catch (IllegalArgumentException e6) {
            Logger.INSTANCE.e("Invalid preferred locale format: " + preferredUILocaleOverride + ". Using system default.", e6);
            return g.b();
        }
    }

    private final PaywallListener getListener() {
        return this.options.getListener();
    }

    private final PaywallMode getMode() {
        return this.options.getMode$revenuecatui_defaultsBc8Release();
    }

    private final PresentedOfferingContext getPresentedOfferingContext(Offering offering) {
        PresentedOfferingContext presentedOfferingContext;
        Package r02 = (Package) AbstractC3251q.g0(offering.getAvailablePackages());
        return (r02 == null || (presentedOfferingContext = r02.getPresentedOfferingContext()) == null) ? new PresentedOfferingContext(offering.getIdentifier()) : presentedOfferingContext;
    }

    private final PaywallPurchaseLogic getPurchaseLogic() {
        return this.options.getPurchaseLogic();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Code restructure failed: missing block: B:97:0x023e, code lost:
    
        if (r2 == r6) goto L98;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:100:0x0258 A[Catch: PurchasesException -> 0x004d, TryCatch #3 {PurchasesException -> 0x004d, blocks: (B:16:0x0048, B:80:0x01ce, B:82:0x01dd, B:83:0x01e8, B:23:0x0062, B:99:0x0241, B:26:0x0071, B:94:0x022b, B:96:0x0231, B:100:0x0258, B:102:0x025c, B:103:0x0260, B:105:0x0264, B:107:0x026c), top: B:127:0x0032 }] */
    /* JADX WARN: Removed duplicated region for block: B:113:0x0285  */
    /* JADX WARN: Removed duplicated region for block: B:116:0x0293  */
    /* JADX WARN: Removed duplicated region for block: B:63:0x013d  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x001d  */
    /* JADX WARN: Removed duplicated region for block: B:82:0x01dd A[Catch: PurchasesException -> 0x004d, TryCatch #3 {PurchasesException -> 0x004d, blocks: (B:16:0x0048, B:80:0x01ce, B:82:0x01dd, B:83:0x01e8, B:23:0x0062, B:99:0x0241, B:26:0x0071, B:94:0x022b, B:96:0x0231, B:100:0x0258, B:102:0x025c, B:103:0x0260, B:105:0x0264, B:107:0x026c), top: B:127:0x0032 }] */
    /* JADX WARN: Removed duplicated region for block: B:85:0x01f9 A[Catch: PurchasesException -> 0x0120, TRY_ENTER, TryCatch #0 {PurchasesException -> 0x0120, blocks: (B:55:0x011d, B:61:0x012c, B:64:0x013f, B:65:0x0158, B:67:0x015e, B:68:0x0161, B:70:0x0167, B:72:0x0170, B:75:0x0185, B:76:0x01b5, B:73:0x017e, B:85:0x01f9, B:88:0x0201, B:90:0x0210, B:109:0x0275, B:110:0x027c), top: B:122:0x011d }] */
    /* JADX WARN: Removed duplicated region for block: B:96:0x0231 A[Catch: PurchasesException -> 0x004d, TryCatch #3 {PurchasesException -> 0x004d, blocks: (B:16:0x0048, B:80:0x01ce, B:82:0x01dd, B:83:0x01e8, B:23:0x0062, B:99:0x0241, B:26:0x0071, B:94:0x022b, B:96:0x0231, B:100:0x0258, B:102:0x025c, B:103:0x0260, B:105:0x0264, B:107:0x026c), top: B:127:0x0032 }] */
    /* JADX WARN: Type inference failed for: r11v1, types: [com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl] */
    /* JADX WARN: Type inference failed for: r11v2 */
    /* JADX WARN: Type inference failed for: r11v20 */
    /* JADX WARN: Type inference failed for: r3v0, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r3v1, types: [com.revenuecat.purchases.Package] */
    /* JADX WARN: Type inference failed for: r3v18 */
    /* JADX WARN: Type inference failed for: r3v20 */
    /* JADX WARN: Type inference failed for: r3v3 */
    /* JADX WARN: Type inference failed for: r3v4 */
    /* JADX WARN: Type inference failed for: r4v0, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r4v1, types: [com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl] */
    /* JADX WARN: Type inference failed for: r4v2 */
    /* JADX WARN: Type inference failed for: r4v24 */
    /* JADX WARN: Type inference failed for: r4v25 */
    /* JADX WARN: Type inference failed for: r4v26 */
    /* JADX WARN: Type inference failed for: r4v27 */
    /* JADX WARN: Type inference failed for: r4v28 */
    /* JADX WARN: Type inference failed for: r4v29 */
    /* JADX WARN: Type inference failed for: r4v3 */
    /* JADX WARN: Type inference failed for: r4v30 */
    /* JADX WARN: Type inference failed for: r4v31 */
    /* JADX WARN: Type inference failed for: r4v32 */
    /* JADX WARN: Type inference failed for: r4v4 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object performPurchase(Activity activity, Package r19, ProductChangeConfig productChangeConfig, SubscriptionOption subscriptionOption, d dVar) {
        C17161 c17161;
        ?? r11;
        ?? r42;
        ProductChangeConfig productChangeConfig2;
        C3137A c3137a;
        Activity activity2;
        Package r10;
        SubscriptionOption subscriptionOption2;
        PaywallViewModelImpl paywallViewModelImpl;
        Activity activity3;
        ProductChangeInfo productChangeInfo;
        PaywallViewModelImpl paywallViewModelImpl2;
        PaywallViewModelImpl paywallViewModelImpl3;
        PaywallViewModelImpl paywallViewModelImpl4;
        int i6;
        Package r32;
        PaywallViewModelImpl paywallViewModelImpl5;
        PaywallViewModelImpl paywallViewModelImpl6;
        PurchaseLogicResult purchaseLogicResult;
        PaywallViewModelImpl paywallViewModelImpl7;
        PaywallListener listener;
        ?? r33 = "Unsupported purchase completion type: ";
        ?? r43 = "Performing product change from ";
        if (dVar instanceof C17161) {
            c17161 = (C17161) dVar;
            int i7 = c17161.label;
            if ((i7 & Integer.MIN_VALUE) != 0) {
                c17161.label = i7 - Integer.MIN_VALUE;
            } else {
                c17161 = new C17161(dVar);
            }
        }
        Object objCalculateProductChangeInfo = c17161.result;
        B4.a aVar = B4.a.f492a;
        int i8 = c17161.label;
        C3137A c3137a2 = C3137A.f25453a;
        try {
        } catch (PurchasesException e6) {
            e = e6;
        }
        if (i8 == 0) {
            AbstractC2467b.l(objCalculateProductChangeInfo);
            c17161.L$0 = this;
            c17161.L$1 = activity;
            c17161.L$2 = r19;
            productChangeConfig2 = productChangeConfig;
            c17161.L$3 = productChangeConfig2;
            c17161.L$4 = subscriptionOption;
            c17161.label = 1;
            final l lVar = new l(AbstractC0872a.V(c17161));
            PaywallListener listener2 = getListener();
            if (listener2 != null) {
                listener2.onPurchasePackageInitiated(r19, new Resumable() { // from class: com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModelImpl$performPurchase$shouldResume$1$1
                    @Override // com.revenuecat.purchases.ui.revenuecatui.utils.Resumable
                    public void invoke(boolean z6) {
                        Resumable.DefaultImpls.invoke(this, z6);
                    }

                    @Override // com.revenuecat.purchases.ui.revenuecatui.utils.Resumable
                    public final void resume(boolean z6) {
                        lVar.resumeWith(Boolean.valueOf(z6));
                    }
                });
                c3137a = c3137a2;
            } else {
                c3137a = null;
            }
            if (c3137a == null) {
                lVar.resumeWith(Boolean.TRUE);
            }
            Object objA = lVar.a();
            if (objA != aVar) {
                activity2 = activity;
                objCalculateProductChangeInfo = objA;
                r10 = r19;
                subscriptionOption2 = subscriptionOption;
                paywallViewModelImpl = this;
            }
            return aVar;
        }
        if (i8 == 1) {
            subscriptionOption2 = (SubscriptionOption) c17161.L$4;
            productChangeConfig2 = (ProductChangeConfig) c17161.L$3;
            r10 = (Package) c17161.L$2;
            activity2 = (Activity) c17161.L$1;
            paywallViewModelImpl = (PaywallViewModelImpl) c17161.L$0;
            AbstractC2467b.l(objCalculateProductChangeInfo);
        } else {
            if (i8 == 2) {
                subscriptionOption2 = (SubscriptionOption) c17161.L$3;
                Package r7 = (Package) c17161.L$2;
                Activity activity4 = (Activity) c17161.L$1;
                PaywallViewModelImpl paywallViewModelImpl8 = (PaywallViewModelImpl) c17161.L$0;
                try {
                    AbstractC2467b.l(objCalculateProductChangeInfo);
                    r10 = r7;
                    activity3 = activity4;
                    paywallViewModelImpl3 = paywallViewModelImpl8;
                    try {
                        productChangeInfo = (ProductChangeInfo) objCalculateProductChangeInfo;
                        paywallViewModelImpl2 = paywallViewModelImpl3;
                        i6 = WhenMappings.$EnumSwitchMapping$0[paywallViewModelImpl2.purchases.getPurchasesAreCompletedBy().ordinal()];
                    } catch (PurchasesException e7) {
                        e = e7;
                        r33 = r10;
                        paywallViewModelImpl4 = paywallViewModelImpl3;
                        r43 = paywallViewModelImpl4;
                        if (e.getCode() == PurchasesErrorCode.PurchaseCancelledError) {
                            r43.trackPaywallCancel();
                            PaywallListener listener3 = r43.getListener();
                            r42 = r43;
                            if (listener3 != null) {
                                listener3.onPurchaseCancelled();
                                r42 = r43;
                            }
                        } else {
                            r43.trackPaywallPurchaseError(r33, e.getError());
                            PaywallListener listener4 = r43.getListener();
                            if (listener4 != null) {
                                listener4.onPurchaseError(e.getError());
                            }
                            r43._actionError.setValue(e.getError());
                            r42 = r43;
                        }
                    }
                } catch (PurchasesException e8) {
                    e = e8;
                    r33 = r7;
                    paywallViewModelImpl4 = paywallViewModelImpl8;
                    r43 = paywallViewModelImpl4;
                    if (e.getCode() == PurchasesErrorCode.PurchaseCancelledError) {
                    }
                    r11 = r42;
                    r11.finishAction();
                    return c3137a2;
                }
                if (i6 == 1) {
                    PaywallPurchaseLogic purchaseLogic = paywallViewModelImpl2.getPurchaseLogic();
                    if (purchaseLogic == null) {
                        throw new IllegalStateException("myAppPurchaseLogic must not be null when purchases.purchasesAreCompletedBy is PurchasesAreCompletedBy.MY_APP");
                    }
                    PaywallPurchaseLogicParams paywallPurchaseLogicParams = new PaywallPurchaseLogicParams(r10, productChangeInfo != null ? new ProductChange(productChangeInfo.getOldProductId(), productChangeInfo.getReplacementMode()) : null, subscriptionOption2);
                    c17161.L$0 = paywallViewModelImpl2;
                    c17161.L$1 = r10;
                    c17161.L$2 = null;
                    c17161.L$3 = null;
                    c17161.L$4 = null;
                    c17161.label = 3;
                    objCalculateProductChangeInfo = purchaseLogic.performPurchase(activity3, paywallPurchaseLogicParams, c17161);
                    if (objCalculateProductChangeInfo != aVar) {
                        r32 = r10;
                        paywallViewModelImpl5 = paywallViewModelImpl2;
                        purchaseLogicResult = (PurchaseLogicResult) objCalculateProductChangeInfo;
                        if (purchaseLogicResult instanceof PurchaseLogicResult.Success) {
                        }
                    }
                } else {
                    if (i6 != 2) {
                        Logger.INSTANCE.e("Unsupported purchase completion type: " + paywallViewModelImpl2.purchases.getPurchasesAreCompletedBy());
                        r11 = paywallViewModelImpl2;
                        r11.finishAction();
                        return c3137a2;
                    }
                    PaywallListener listener5 = paywallViewModelImpl2.getListener();
                    if (listener5 != null) {
                        listener5.onPurchaseStarted(r10);
                    }
                    if (paywallViewModelImpl2.getPurchaseLogic() != null) {
                        Logger.INSTANCE.e("myAppPurchaseLogic expected to be null when purchases.purchasesAreCompletedBy is .REVENUECAT. \nmyAppPurchaseLogic.performPurchase will not be executed.");
                    }
                    PurchaseParams.Builder builderPresentedOfferingContext = subscriptionOption2 != null ? new PurchaseParams.Builder(activity3, subscriptionOption2).presentedOfferingContext(r10.getPresentedOfferingContext()) : new PurchaseParams.Builder(activity3, r10);
                    if (productChangeInfo != null) {
                        Logger.INSTANCE.d("Performing product change from " + productChangeInfo.getOldProductId() + " with mode " + productChangeInfo.getReplacementMode());
                        builderPresentedOfferingContext.oldProductId(productChangeInfo.getOldProductId()).googleReplacementMode(productChangeInfo.getReplacementMode());
                    }
                    PurchasesType purchasesType = paywallViewModelImpl2.purchases;
                    c17161.L$0 = paywallViewModelImpl2;
                    c17161.L$1 = r10;
                    c17161.L$2 = null;
                    c17161.L$3 = null;
                    c17161.L$4 = null;
                    c17161.label = 5;
                    objCalculateProductChangeInfo = purchasesType.awaitPurchase(builderPresentedOfferingContext, c17161);
                    if (objCalculateProductChangeInfo != aVar) {
                        paywallViewModelImpl6 = paywallViewModelImpl2;
                        PurchaseResult purchaseResult = (PurchaseResult) objCalculateProductChangeInfo;
                        paywallViewModelImpl6._purchaseCompleted.setValue(Boolean.TRUE);
                        listener = paywallViewModelImpl6.getListener();
                        if (listener != null) {
                        }
                        Logger.INSTANCE.d("Dismissing paywall after purchase");
                        paywallViewModelImpl6.options.getDismissRequest().invoke();
                        r42 = paywallViewModelImpl6;
                        r11 = r42;
                        r11.finishAction();
                        return c3137a2;
                    }
                }
                return aVar;
            }
            if (i8 != 3) {
                if (i8 == 4) {
                    PaywallViewModelImpl paywallViewModelImpl9 = (PaywallViewModelImpl) c17161.L$0;
                    AbstractC2467b.l(objCalculateProductChangeInfo);
                    paywallViewModelImpl7 = paywallViewModelImpl9;
                    paywallViewModelImpl7._purchaseCompleted.setValue(Boolean.TRUE);
                    Logger.INSTANCE.d("Dismissing paywall after purchase");
                    paywallViewModelImpl7.closePaywall(new PaywallResult.Purchased((CustomerInfo) objCalculateProductChangeInfo));
                    r42 = paywallViewModelImpl7;
                    r11 = r42;
                    r11.finishAction();
                    return c3137a2;
                }
                if (i8 != 5) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                PaywallViewModelImpl paywallViewModelImpl10 = (PaywallViewModelImpl) c17161.L$0;
                AbstractC2467b.l(objCalculateProductChangeInfo);
                paywallViewModelImpl6 = paywallViewModelImpl10;
                PurchaseResult purchaseResult2 = (PurchaseResult) objCalculateProductChangeInfo;
                paywallViewModelImpl6._purchaseCompleted.setValue(Boolean.TRUE);
                listener = paywallViewModelImpl6.getListener();
                if (listener != null) {
                    listener.onPurchaseCompleted(purchaseResult2.getCustomerInfo(), purchaseResult2.getStoreTransaction());
                }
                Logger.INSTANCE.d("Dismissing paywall after purchase");
                paywallViewModelImpl6.options.getDismissRequest().invoke();
                r42 = paywallViewModelImpl6;
                r11 = r42;
                r11.finishAction();
                return c3137a2;
            }
            r32 = (Package) c17161.L$1;
            PaywallViewModelImpl paywallViewModelImpl11 = (PaywallViewModelImpl) c17161.L$0;
            AbstractC2467b.l(objCalculateProductChangeInfo);
            paywallViewModelImpl5 = paywallViewModelImpl11;
            purchaseLogicResult = (PurchaseLogicResult) objCalculateProductChangeInfo;
            if (purchaseLogicResult instanceof PurchaseLogicResult.Success) {
                if (purchaseLogicResult instanceof PurchaseLogicResult.Cancellation) {
                    paywallViewModelImpl5.trackPaywallCancel();
                    r42 = paywallViewModelImpl5;
                } else {
                    r42 = paywallViewModelImpl5;
                    if (purchaseLogicResult instanceof PurchaseLogicResult.Error) {
                        PurchasesError errorDetails = ((PurchaseLogicResult.Error) purchaseLogicResult).getErrorDetails();
                        r42 = paywallViewModelImpl5;
                        if (errorDetails != null) {
                            paywallViewModelImpl5.trackPaywallPurchaseError(r32, errorDetails);
                            paywallViewModelImpl5._actionError.setValue(errorDetails);
                            r42 = paywallViewModelImpl5;
                        }
                    }
                }
                r11 = r42;
                r11.finishAction();
                return c3137a2;
            }
            PurchasesType purchasesType2 = paywallViewModelImpl5.purchases;
            c17161.L$0 = paywallViewModelImpl5;
            c17161.L$1 = r32;
            c17161.label = 4;
            objCalculateProductChangeInfo = purchasesType2.awaitSyncPurchases(c17161);
            paywallViewModelImpl7 = paywallViewModelImpl5;
        }
        if (!((Boolean) objCalculateProductChangeInfo).booleanValue()) {
            Logger.INSTANCE.d("Purchase cancelled listener.onPurchasePackageInitiated returned false");
            return c3137a2;
        }
        try {
            paywallViewModelImpl.trackPaywallPurchaseInitiated(r10);
            if (productChangeConfig2 == null) {
                activity3 = activity2;
                productChangeInfo = null;
                paywallViewModelImpl2 = paywallViewModelImpl;
                i6 = WhenMappings.$EnumSwitchMapping$0[paywallViewModelImpl2.purchases.getPurchasesAreCompletedBy().ordinal()];
                if (i6 == 1) {
                }
                return aVar;
            }
            ProductChangeCalculator productChangeCalculator = paywallViewModelImpl.productChangeCalculator;
            c17161.L$0 = paywallViewModelImpl;
            c17161.L$1 = activity2;
            c17161.L$2 = r10;
            c17161.L$3 = subscriptionOption2;
            c17161.L$4 = null;
            c17161.label = 2;
            objCalculateProductChangeInfo = productChangeCalculator.calculateProductChangeInfo(r10, productChangeConfig2, c17161);
            if (objCalculateProductChangeInfo != aVar) {
                activity3 = activity2;
                paywallViewModelImpl3 = paywallViewModelImpl;
                productChangeInfo = (ProductChangeInfo) objCalculateProductChangeInfo;
                paywallViewModelImpl2 = paywallViewModelImpl3;
                i6 = WhenMappings.$EnumSwitchMapping$0[paywallViewModelImpl2.purchases.getPurchasesAreCompletedBy().ordinal()];
                if (i6 == 1) {
                }
            }
            return aVar;
        } catch (PurchasesException e9) {
            e = e9;
            r43 = paywallViewModelImpl;
            r33 = r10;
            if (e.getCode() == PurchasesErrorCode.PurchaseCancelledError) {
            }
        }
        r11 = r42;
        r11.finishAction();
        return c3137a2;
    }

    public static /* synthetic */ Object performPurchase$default(PaywallViewModelImpl paywallViewModelImpl, Activity activity, Package r8, ProductChangeConfig productChangeConfig, SubscriptionOption subscriptionOption, d dVar, int i6, Object obj) {
        if ((i6 & 4) != 0) {
            productChangeConfig = null;
        }
        return paywallViewModelImpl.performPurchase(activity, r8, productChangeConfig, subscriptionOption, dVar);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final Object performPurchaseIfNecessary(Activity activity, PaywallState.Loaded.Components.SelectedPackageInfo selectedPackageInfo, ProductChangeConfig productChangeConfig, d dVar) {
        C3137A c3137a = C3137A.f25453a;
        if (selectedPackageInfo == null) {
            Logger.INSTANCE.w("Ignoring purchase request as no package is selected");
            return c3137a;
        }
        Package rcPackage = selectedPackageInfo.getRcPackage();
        ResolvedOffer resolvedOffer = selectedPackageInfo.getResolvedOffer();
        Object objPerformPurchase = performPurchase(activity, rcPackage, productChangeConfig, resolvedOffer != null ? resolvedOffer.getSubscriptionOption() : null, dVar);
        return objPerformPurchase == B4.a.f492a ? objPerformPurchase : c3137a;
    }

    private final LocaleList toFrameworkLocaleList(g gVar) {
        int size = gVar.f448a.f449a.size();
        Locale[] localeArr = new Locale[size];
        for (int i6 = 0; i6 < size; i6++) {
            Locale localeA = gVar.a(i6);
            o.e(localeA);
            localeArr[i6] = localeA;
        }
        return new LocaleList((Locale[]) Arrays.copyOf(localeArr, size));
    }

    private final void track(PaywallEventType paywallEventType) {
        PaywallEvent.Data data = this.paywallPresentationData;
        if (data == null) {
            Logger.INSTANCE.e("Paywall event data is null, not tracking event " + paywallEventType);
            return;
        }
        UUID uuidRandomUUID = UUID.randomUUID();
        o.g(uuidRandomUUID, "randomUUID()");
        this.purchases.track(new PaywallEvent(new PaywallEvent.CreationData(uuidRandomUUID, new Date()), data, paywallEventType));
    }

    private final void trackPaywallCancel() {
        track(PaywallEventType.CANCEL);
    }

    private final void trackPaywallClose() {
        if (this.paywallPresentationData != null) {
            track(PaywallEventType.CLOSE);
        }
    }

    private final void trackPaywallPurchaseError(Package r18, PurchasesError purchasesError) {
        PaywallEvent.Data data = this.paywallPresentationData;
        if (data == null) {
            Logger.INSTANCE.e("Paywall event data is null, not tracking purchase error event");
            return;
        }
        StoreProduct product = r18.getProduct();
        PaywallEvent.Data dataCopy$default = PaywallEvent.Data.copy$default(data, null, null, 0, null, null, null, false, null, null, r18.getIdentifier(), product instanceof GoogleStoreProduct ? ((GoogleStoreProduct) product).getProductId() : product.getId(), Integer.valueOf(purchasesError.getCode().getCode()), purchasesError.getMessage(), 511, null);
        UUID uuidRandomUUID = UUID.randomUUID();
        o.g(uuidRandomUUID, "randomUUID()");
        this.purchases.track(new PaywallEvent(new PaywallEvent.CreationData(uuidRandomUUID, new Date()), dataCopy$default, PaywallEventType.PURCHASE_ERROR));
    }

    private final void trackPaywallPurchaseInitiated(Package r18) {
        PaywallEvent.Data data = this.paywallPresentationData;
        if (data == null) {
            Logger.INSTANCE.e("Paywall event data is null, not tracking purchase initiated event");
            return;
        }
        StoreProduct product = r18.getProduct();
        PaywallEvent.Data dataCopy$default = PaywallEvent.Data.copy$default(data, null, null, 0, null, null, null, false, null, null, r18.getIdentifier(), product instanceof GoogleStoreProduct ? ((GoogleStoreProduct) product).getProductId() : product.getId(), null, null, 6655, null);
        UUID uuidRandomUUID = UUID.randomUUID();
        o.g(uuidRandomUUID, "randomUUID()");
        this.purchases.track(new PaywallEvent(new PaywallEvent.CreationData(uuidRandomUUID, new Date()), dataCopy$default, PaywallEventType.PURCHASE_INITIATED));
    }

    private final void updateState() {
        E.y(X.h(this), null, null, new C17201(null), 3);
    }

    private final void validateState() {
        if (this.purchases.getPurchasesAreCompletedBy() == PurchasesAreCompletedBy.MY_APP && this.options.getPurchaseLogic() == null) {
            H h5 = this._state;
            PaywallState.Error error = new PaywallState.Error("myAppPurchaseLogic is null, but is required when purchases.purchasesAreCompletedBy is .MY_APP. App purchases will not be successful.");
            c0 c0Var = (c0) h5;
            c0Var.getClass();
            c0Var.k(null, error);
        }
    }

    private final boolean verifyNoActionInProgressOrStartAction() {
        if (this._actionInProgress.getValue().booleanValue()) {
            Logger.INSTANCE.d("Ignoring purchase or restore because there already is an action in progress");
            return true;
        }
        this._actionInProgress.setValue(Boolean.TRUE);
        return false;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void clearActionError() {
        this._actionError.setValue(null);
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void closePaywall(PaywallResult paywallResult) {
        Logger.INSTANCE.d("Paywalls: Close paywall initiated");
        trackPaywallClose();
        Offering value = !this._purchaseCompleted.getValue().booleanValue() ? this._preloadedExitOffering.getValue() : null;
        if (value != null) {
            trackExitOffer(ExitOfferType.DISMISS, value.getIdentifier());
        }
        this.paywallPresentationData = null;
        J4.e dismissRequestWithExitOffering$revenuecatui_defaultsBc8Release = this.options.getDismissRequestWithExitOffering$revenuecatui_defaultsBc8Release();
        if (dismissRequestWithExitOffering$revenuecatui_defaultsBc8Release != null) {
            dismissRequestWithExitOffering$revenuecatui_defaultsBc8Release.invoke(value, paywallResult);
        } else {
            this.options.getDismissRequest().invoke();
        }
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public State<PurchasesError> getActionError() {
        return this._actionError;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public State<Boolean> getActionInProgress() {
        return this._actionInProgress;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public State<Offering> getPreloadedExitOffering() {
        return this._preloadedExitOffering;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public State<Boolean> getPurchaseCompleted() {
        return this._purchaseCompleted;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public ResourceProvider getResourceProvider() {
        return this.resourceProvider;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public a0 getState() {
        return new J(this._state);
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public String getWebCheckoutUrl(PaywallAction.External.LaunchWebCheckout launchWebCheckout) {
        k kVar;
        URL webCheckoutURL;
        String string;
        o.h(launchWebCheckout, "launchWebCheckout");
        String customUrl = launchWebCheckout.getCustomUrl();
        Object value = getState().getValue();
        PaywallState.Loaded.Components components = value instanceof PaywallState.Loaded.Components ? (PaywallState.Loaded.Components) value : null;
        if (components == null) {
            Logger.INSTANCE.e("Web checkout URL can only be constructed for loaded Components paywalls");
            return null;
        }
        PaywallAction.External.LaunchWebCheckout.PackageParamBehavior packageParamBehavior = launchWebCheckout.getPackageParamBehavior();
        if (packageParamBehavior instanceof PaywallAction.External.LaunchWebCheckout.PackageParamBehavior.Append) {
            PaywallAction.External.LaunchWebCheckout.PackageParamBehavior.Append append = (PaywallAction.External.LaunchWebCheckout.PackageParamBehavior.Append) packageParamBehavior;
            Package rcPackage = append.getRcPackage();
            if (rcPackage == null) {
                PaywallState.Loaded.Components.SelectedPackageInfo selectedPackageInfo = components.getSelectedPackageInfo();
                rcPackage = selectedPackageInfo != null ? selectedPackageInfo.getRcPackage() : null;
            }
            kVar = new k(rcPackage, append.getPackageParam());
        } else {
            if (!(packageParamBehavior instanceof PaywallAction.External.LaunchWebCheckout.PackageParamBehavior.DoNotAppend)) {
                throw new v();
            }
            kVar = new k(null, null);
        }
        Package r6 = (Package) kVar.f25469a;
        String str = (String) kVar.f25470b;
        if (customUrl == null) {
            return (r6 == null || (webCheckoutURL = r6.getWebCheckoutURL()) == null || (string = webCheckoutURL.toString()) == null) ? String.valueOf(components.getOffering().getWebCheckoutURL()) : string;
        }
        try {
            URI uri = new URI(customUrl);
            if (str != null && r6 != null) {
                uri = URIExtensionsKt.appendQueryParameter(uri, str, r6.getIdentifier());
            }
            return uri.toString();
        } catch (URISyntaxException e6) {
            Logger.INSTANCE.e("Invalid custom URI: ".concat(customUrl), e6);
            return null;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:23:0x0074, code lost:
    
        if (performPurchase$default(r1, r11, r12, null, null, r6, 4, null) == r0) goto L38;
     */
    /* JADX WARN: Code restructure failed: missing block: B:37:0x00b3, code lost:
    
        if (performPurchaseIfNecessary(r11, r3, r12, r6) == r0) goto L38;
     */
    /* JADX WARN: Code restructure failed: missing block: B:38:0x00b5, code lost:
    
        return r0;
     */
    /* JADX WARN: Removed duplicated region for block: B:8:0x0014  */
    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object handlePackagePurchase(Activity activity, Package r12, ResolvedOffer resolvedOffer, d dVar) {
        AnonymousClass1 anonymousClass1;
        PaywallViewModelImpl paywallViewModelImpl;
        PaywallComponentsData data;
        PaywallViewModelImpl paywallViewModelImpl2;
        if (dVar instanceof AnonymousClass1) {
            anonymousClass1 = (AnonymousClass1) dVar;
            int i6 = anonymousClass1.label;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                anonymousClass1.label = i6 - Integer.MIN_VALUE;
            } else {
                anonymousClass1 = new AnonymousClass1(dVar);
            }
        }
        AnonymousClass1 anonymousClass12 = anonymousClass1;
        Object obj = anonymousClass12.result;
        B4.a aVar = B4.a.f492a;
        int i7 = anonymousClass12.label;
        C3137A c3137a = C3137A.f25453a;
        if (i7 == 0) {
            AbstractC2467b.l(obj);
            if (verifyNoActionInProgressOrStartAction()) {
                return c3137a;
            }
            PaywallState paywallState = (PaywallState) ((c0) this._state).getValue();
            if (paywallState instanceof PaywallState.Loaded.Legacy) {
                Package rcPackage = ((PaywallState.Loaded.Legacy) paywallState).getSelectedPackage().getValue().getRcPackage();
                anonymousClass12.L$0 = this;
                anonymousClass12.label = 1;
                paywallViewModelImpl = this;
            } else {
                paywallViewModelImpl = this;
                if (paywallState instanceof PaywallState.Loaded.Components) {
                    PaywallState.Loaded.Components.SelectedPackageInfo selectedPackageInfo = r12 != null ? new PaywallState.Loaded.Components.SelectedPackageInfo(r12, resolvedOffer, r12.getIdentifier(), PackageExtensionsKt.calculateOfferEligibility(resolvedOffer, r12)) : ((PaywallState.Loaded.Components) paywallState).getSelectedPackageInfo();
                    Offering.PaywallComponents paywallComponents = ((PaywallState.Loaded.Components) paywallState).getOffering().getPaywallComponents();
                    ProductChangeConfig productChangeConfig = (paywallComponents == null || (data = paywallComponents.getData()) == null) ? null : data.getProductChangeConfig();
                    anonymousClass12.L$0 = paywallViewModelImpl;
                    anonymousClass12.label = 2;
                } else {
                    if (paywallState instanceof PaywallState.Error ? true : paywallState instanceof PaywallState.Loading) {
                        Logger.INSTANCE.e("Unexpected state trying to purchase package: " + paywallState);
                    }
                }
                paywallViewModelImpl2 = paywallViewModelImpl;
            }
        } else {
            if (i7 != 1 && i7 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            paywallViewModelImpl2 = (PaywallViewModelImpl) anonymousClass12.L$0;
            AbstractC2467b.l(obj);
        }
        paywallViewModelImpl2.finishAction();
        return c3137a;
    }

    /* JADX WARN: Code restructure failed: missing block: B:75:0x0148, code lost:
    
        if (r14 == r3) goto L76;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:56:0x00ed A[Catch: PurchasesException -> 0x003e, TryCatch #0 {PurchasesException -> 0x003e, blocks: (B:15:0x0039, B:54:0x00d4, B:56:0x00ed, B:57:0x00f0, B:59:0x00f4, B:61:0x0100, B:22:0x004d, B:77:0x014b, B:79:0x0151, B:81:0x015d, B:25:0x0056, B:72:0x0138, B:74:0x013e, B:82:0x0172, B:84:0x0176, B:86:0x017a, B:88:0x0182), top: B:98:0x002b }] */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00f4 A[Catch: PurchasesException -> 0x003e, TryCatch #0 {PurchasesException -> 0x003e, blocks: (B:15:0x0039, B:54:0x00d4, B:56:0x00ed, B:57:0x00f0, B:59:0x00f4, B:61:0x0100, B:22:0x004d, B:77:0x014b, B:79:0x0151, B:81:0x015d, B:25:0x0056, B:72:0x0138, B:74:0x013e, B:82:0x0172, B:84:0x0176, B:86:0x017a, B:88:0x0182), top: B:98:0x002b }] */
    /* JADX WARN: Removed duplicated region for block: B:71:0x0137  */
    /* JADX WARN: Removed duplicated region for block: B:74:0x013e A[Catch: PurchasesException -> 0x003e, TryCatch #0 {PurchasesException -> 0x003e, blocks: (B:15:0x0039, B:54:0x00d4, B:56:0x00ed, B:57:0x00f0, B:59:0x00f4, B:61:0x0100, B:22:0x004d, B:77:0x014b, B:79:0x0151, B:81:0x015d, B:25:0x0056, B:72:0x0138, B:74:0x013e, B:82:0x0172, B:84:0x0176, B:86:0x017a, B:88:0x0182), top: B:98:0x002b }] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /* JADX WARN: Removed duplicated region for block: B:82:0x0172 A[Catch: PurchasesException -> 0x003e, TryCatch #0 {PurchasesException -> 0x003e, blocks: (B:15:0x0039, B:54:0x00d4, B:56:0x00ed, B:57:0x00f0, B:59:0x00f4, B:61:0x0100, B:22:0x004d, B:77:0x014b, B:79:0x0151, B:81:0x015d, B:25:0x0056, B:72:0x0138, B:74:0x013e, B:82:0x0172, B:84:0x0176, B:86:0x017a, B:88:0x0182), top: B:98:0x002b }] */
    /* JADX WARN: Removed duplicated region for block: B:94:0x01a9  */
    /* JADX WARN: Type inference failed for: r0v0, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r0v12 */
    /* JADX WARN: Type inference failed for: r0v3 */
    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object handleRestorePurchases(d dVar) {
        C17151 c17151;
        PaywallListener listener;
        PaywallViewModelImpl paywallViewModelImpl;
        PaywallViewModelImpl paywallViewModelImpl2;
        J4.e eVar;
        PaywallViewModelImpl paywallViewModelImpl3;
        PaywallViewModelImpl paywallViewModelImpl4;
        PurchaseLogicResult purchaseLogicResult;
        PaywallViewModelImpl paywallViewModelImpl5;
        PaywallListener listener2;
        c cVar;
        PaywallViewModelImpl paywallViewModelImpl6 = "Unsupported purchase completion type: ";
        if (dVar instanceof C17151) {
            c17151 = (C17151) dVar;
            int i6 = c17151.label;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c17151.label = i6 - Integer.MIN_VALUE;
            } else {
                c17151 = new C17151(dVar);
            }
        }
        Object objAwaitCustomerInfo$default = c17151.result;
        B4.a aVar = B4.a.f492a;
        int i7 = c17151.label;
        C3137A c3137a = C3137A.f25453a;
        try {
            if (i7 == 0) {
                AbstractC2467b.l(objAwaitCustomerInfo$default);
                if (verifyNoActionInProgressOrStartAction()) {
                    return c3137a;
                }
                try {
                    PaywallPurchaseLogic purchaseLogic = getPurchaseLogic();
                    PaywallViewModelImpl$handleRestorePurchases$customRestoreHandler$1$1 paywallViewModelImpl$handleRestorePurchases$customRestoreHandler$1$1 = purchaseLogic != null ? new PaywallViewModelImpl$handleRestorePurchases$customRestoreHandler$1$1(purchaseLogic) : null;
                    int i8 = WhenMappings.$EnumSwitchMapping$0[this.purchases.getPurchasesAreCompletedBy().ordinal()];
                    if (i8 != 1) {
                        if (i8 != 2) {
                            Logger.INSTANCE.e("Unsupported purchase completion type: " + this.purchases.getPurchasesAreCompletedBy());
                            paywallViewModelImpl = this;
                        } else {
                            PaywallListener listener3 = getListener();
                            if (listener3 != null) {
                                listener3.onRestoreStarted();
                            }
                            if (paywallViewModelImpl$handleRestorePurchases$customRestoreHandler$1$1 != null) {
                                Logger.INSTANCE.w("myAppPurchaseLogic expected be null when purchases.purchasesAreCompletedBy is .REVENUECAT.\nmyAppPurchaseLogic.performRestore will not be executed.");
                            }
                            PurchasesType purchasesType = this.purchases;
                            c17151.L$0 = this;
                            c17151.label = 4;
                            objAwaitCustomerInfo$default = purchasesType.awaitRestore(c17151);
                            if (objAwaitCustomerInfo$default != aVar) {
                                paywallViewModelImpl3 = this;
                                CustomerInfo customerInfo = (CustomerInfo) objAwaitCustomerInfo$default;
                                Logger logger = Logger.INSTANCE;
                                logger.i("Restore purchases successful: " + customerInfo);
                                listener2 = paywallViewModelImpl3.getListener();
                                if (listener2 != null) {
                                }
                                cVar = paywallViewModelImpl3.shouldDisplayBlock;
                                paywallViewModelImpl = paywallViewModelImpl3;
                                if (cVar != null) {
                                }
                            }
                        }
                    } else {
                        if (paywallViewModelImpl$handleRestorePurchases$customRestoreHandler$1$1 == null) {
                            throw new IllegalStateException("myAppPurchaseLogic must not be null when purchases.purchasesAreCompletedBy is PurchasesAreCompletedBy.MY_APP");
                        }
                        PurchasesType purchasesType2 = this.purchases;
                        c17151.L$0 = this;
                        c17151.L$1 = paywallViewModelImpl$handleRestorePurchases$customRestoreHandler$1$1;
                        c17151.label = 1;
                        objAwaitCustomerInfo$default = PurchasesType.DefaultImpls.awaitCustomerInfo$default(purchasesType2, null, c17151, 1, null);
                        if (objAwaitCustomerInfo$default != aVar) {
                            paywallViewModelImpl2 = this;
                            eVar = paywallViewModelImpl$handleRestorePurchases$customRestoreHandler$1$1;
                            c17151.L$0 = paywallViewModelImpl2;
                            c17151.L$1 = null;
                            c17151.label = 2;
                            objAwaitCustomerInfo$default = eVar.invoke((CustomerInfo) objAwaitCustomerInfo$default, c17151);
                            if (objAwaitCustomerInfo$default != aVar) {
                            }
                        }
                    }
                    return aVar;
                } catch (PurchasesException e6) {
                    e = e6;
                    paywallViewModelImpl6 = this;
                    Logger.INSTANCE.e("Error restoring purchases: " + e);
                    listener = paywallViewModelImpl6.getListener();
                    if (listener != null) {
                        listener.onRestoreError(e.getError());
                    }
                    paywallViewModelImpl6._actionError.setValue(e.getError());
                    paywallViewModelImpl = paywallViewModelImpl6;
                }
            } else if (i7 == 1) {
                eVar = (J4.e) c17151.L$1;
                paywallViewModelImpl2 = (PaywallViewModelImpl) c17151.L$0;
                try {
                    AbstractC2467b.l(objAwaitCustomerInfo$default);
                    c17151.L$0 = paywallViewModelImpl2;
                    c17151.L$1 = null;
                    c17151.label = 2;
                    objAwaitCustomerInfo$default = eVar.invoke((CustomerInfo) objAwaitCustomerInfo$default, c17151);
                    if (objAwaitCustomerInfo$default != aVar) {
                        paywallViewModelImpl4 = paywallViewModelImpl2;
                        purchaseLogicResult = (PurchaseLogicResult) objAwaitCustomerInfo$default;
                        if (purchaseLogicResult instanceof PurchaseLogicResult.Success) {
                        }
                    }
                    return aVar;
                } catch (PurchasesException e7) {
                    e = e7;
                    paywallViewModelImpl6 = paywallViewModelImpl2;
                    Logger.INSTANCE.e("Error restoring purchases: " + e);
                    listener = paywallViewModelImpl6.getListener();
                    if (listener != null) {
                    }
                    paywallViewModelImpl6._actionError.setValue(e.getError());
                    paywallViewModelImpl = paywallViewModelImpl6;
                }
            } else if (i7 == 2) {
                PaywallViewModelImpl paywallViewModelImpl7 = (PaywallViewModelImpl) c17151.L$0;
                AbstractC2467b.l(objAwaitCustomerInfo$default);
                paywallViewModelImpl4 = paywallViewModelImpl7;
                purchaseLogicResult = (PurchaseLogicResult) objAwaitCustomerInfo$default;
                if (purchaseLogicResult instanceof PurchaseLogicResult.Success) {
                    paywallViewModelImpl = paywallViewModelImpl4;
                    if (!(purchaseLogicResult instanceof PurchaseLogicResult.Cancellation)) {
                        paywallViewModelImpl = paywallViewModelImpl4;
                        if (purchaseLogicResult instanceof PurchaseLogicResult.Error) {
                            PurchasesError errorDetails = ((PurchaseLogicResult.Error) purchaseLogicResult).getErrorDetails();
                            paywallViewModelImpl = paywallViewModelImpl4;
                            if (errorDetails != null) {
                                paywallViewModelImpl4._actionError.setValue(errorDetails);
                                paywallViewModelImpl = paywallViewModelImpl4;
                            }
                        }
                    }
                } else {
                    PurchasesType purchasesType3 = paywallViewModelImpl4.purchases;
                    c17151.L$0 = paywallViewModelImpl4;
                    c17151.label = 3;
                    objAwaitCustomerInfo$default = purchasesType3.awaitSyncPurchases(c17151);
                    paywallViewModelImpl5 = paywallViewModelImpl4;
                }
            } else if (i7 == 3) {
                PaywallViewModelImpl paywallViewModelImpl8 = (PaywallViewModelImpl) c17151.L$0;
                AbstractC2467b.l(objAwaitCustomerInfo$default);
                paywallViewModelImpl5 = paywallViewModelImpl8;
                CustomerInfo customerInfo2 = (CustomerInfo) objAwaitCustomerInfo$default;
                c cVar2 = paywallViewModelImpl5.shouldDisplayBlock;
                paywallViewModelImpl = paywallViewModelImpl5;
                if (cVar2 != null) {
                    paywallViewModelImpl = paywallViewModelImpl5;
                    if (!((Boolean) cVar2.invoke(customerInfo2)).booleanValue()) {
                        paywallViewModelImpl5._purchaseCompleted.setValue(Boolean.TRUE);
                        Logger.INSTANCE.d("Dismissing paywall after restore since display condition has not been met");
                        paywallViewModelImpl5.closePaywall(new PaywallResult.Restored(customerInfo2));
                        paywallViewModelImpl = paywallViewModelImpl5;
                    }
                }
            } else {
                if (i7 != 4) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                PaywallViewModelImpl paywallViewModelImpl9 = (PaywallViewModelImpl) c17151.L$0;
                AbstractC2467b.l(objAwaitCustomerInfo$default);
                paywallViewModelImpl3 = paywallViewModelImpl9;
                CustomerInfo customerInfo3 = (CustomerInfo) objAwaitCustomerInfo$default;
                Logger logger2 = Logger.INSTANCE;
                logger2.i("Restore purchases successful: " + customerInfo3);
                listener2 = paywallViewModelImpl3.getListener();
                if (listener2 != null) {
                    listener2.onRestoreCompleted(customerInfo3);
                }
                cVar = paywallViewModelImpl3.shouldDisplayBlock;
                paywallViewModelImpl = paywallViewModelImpl3;
                if (cVar != null) {
                    paywallViewModelImpl = paywallViewModelImpl3;
                    if (!((Boolean) cVar.invoke(customerInfo3)).booleanValue()) {
                        paywallViewModelImpl3._purchaseCompleted.setValue(Boolean.TRUE);
                        logger2.d("Dismissing paywall after restore since display condition has not been met");
                        paywallViewModelImpl3.options.getDismissRequest().invoke();
                        paywallViewModelImpl = paywallViewModelImpl3;
                    }
                }
            }
        } catch (PurchasesException e8) {
            e = e8;
        }
        paywallViewModelImpl.finishAction();
        return c3137a;
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void invalidateCustomerInfoCache() {
        this.purchases.invalidateVirtualCurrenciesCache();
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void preloadExitOffering() {
        E.y(X.h(this), null, null, new C17171(null), 3);
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void purchaseSelectedPackage(Activity activity) {
        if (activity == null) {
            Logger.INSTANCE.e("Activity is null, not initiating package purchase");
        } else {
            E.y(X.h(this), null, null, new C17181(activity, null), 3);
        }
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void refreshStateIfColorsChanged(I colorScheme, boolean z6) {
        o.h(colorScheme, "colorScheme");
        if (this.isDarkMode != z6) {
            this.isDarkMode = z6;
        }
        if (o.c(((c0) this._colorScheme).getValue(), colorScheme)) {
            return;
        }
        c0 c0Var = (c0) this._colorScheme;
        c0Var.getClass();
        c0Var.k(null, colorScheme);
        updateState();
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void refreshStateIfLocaleChanged() {
        g currentLocaleList = getCurrentLocaleList();
        if (o.c(((c0) this._lastLocaleList).getValue(), currentLocaleList)) {
            return;
        }
        ((c0) this._lastLocaleList).j(currentLocaleList);
        PaywallState paywallState = (PaywallState) ((c0) this._state).getValue();
        if (paywallState instanceof PaywallState.Loaded.Components) {
            PaywallState.Loaded.Components.update$default((PaywallState.Loaded.Components) paywallState, toFrameworkLocaleList(currentLocaleList), null, null, 6, null);
        } else {
            updateState();
        }
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void restorePurchases() {
        E.y(X.h(this), null, null, new C17191(null), 3);
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void selectPackage(TemplateConfiguration.PackageInfo packageToSelect) {
        o.h(packageToSelect, "packageToSelect");
        PaywallState paywallState = (PaywallState) ((c0) this._state).getValue();
        if (paywallState instanceof PaywallState.Loaded.Legacy) {
            ((PaywallState.Loaded.Legacy) paywallState).selectPackage(packageToSelect);
            return;
        }
        Logger.INSTANCE.e("Unexpected state trying to select package: " + paywallState);
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void trackExitOffer(ExitOfferType exitOfferType, String exitOfferingIdentifier) {
        o.h(exitOfferType, "exitOfferType");
        o.h(exitOfferingIdentifier, "exitOfferingIdentifier");
        PaywallEvent.Data data = this.paywallPresentationData;
        if (data == null) {
            Logger.INSTANCE.e("Paywall event data is null, not tracking exit offer event");
            return;
        }
        PaywallEvent.Data dataCopy$default = PaywallEvent.Data.copy$default(data, null, null, 0, null, null, null, false, exitOfferType, exitOfferingIdentifier, null, null, null, null, 7807, null);
        UUID uuidRandomUUID = UUID.randomUUID();
        o.g(uuidRandomUUID, "randomUUID()");
        this.purchases.track(new PaywallEvent(new PaywallEvent.CreationData(uuidRandomUUID, new Date()), dataCopy$default, PaywallEventType.EXIT_OFFER));
    }

    @Override // com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
    public void trackPaywallImpressionIfNeeded() {
        if (this.paywallPresentationData == null) {
            this.paywallPresentationData = createEventData();
            track(PaywallEventType.IMPRESSION);
        }
    }

    public final void updateOptions(PaywallOptions options) {
        o.h(options, "options");
        boolean z6 = this.options.hashCode() != options.hashCode();
        this.options = options;
        if (z6) {
            updateState();
        }
    }

    public PaywallViewModelImpl(ResourceProvider resourceProvider, PurchasesType purchases, PaywallOptions options, I colorScheme, boolean z6, c cVar, boolean z7, ProductChangeCalculator productChangeCalculator) {
        o.h(resourceProvider, "resourceProvider");
        o.h(purchases, "purchases");
        o.h(options, "options");
        o.h(colorScheme, "colorScheme");
        o.h(productChangeCalculator, "productChangeCalculator");
        this.resourceProvider = resourceProvider;
        this.purchases = purchases;
        this.options = options;
        this.isDarkMode = z6;
        this.shouldDisplayBlock = cVar;
        this.productChangeCalculator = productChangeCalculator;
        this.variableDataProvider = new VariableDataProvider(getResourceProvider(), z7);
        this._state = O.c(PaywallState.Loading.INSTANCE);
        Boolean bool = Boolean.FALSE;
        this._actionInProgress = SnapshotStateKt__SnapshotStateKt.mutableStateOf$default(bool, null, 2, null);
        this._actionError = SnapshotStateKt__SnapshotStateKt.mutableStateOf$default(null, null, 2, null);
        this._purchaseCompleted = SnapshotStateKt__SnapshotStateKt.mutableStateOf$default(bool, null, 2, null);
        this._preloadedExitOffering = SnapshotStateKt__SnapshotStateKt.mutableStateOf$default(null, null, 2, null);
        this._lastLocaleList = O.c(getCurrentLocaleList());
        this._colorScheme = O.c(colorScheme);
        updateState();
        validateState();
    }

    private final PaywallEvent.Data createEventData(PaywallState.Loaded.Legacy legacy) {
        PaywallComponentsData data;
        int revision;
        String str;
        PaywallComponentsData data2;
        String id;
        Offering offering = legacy.getOffering();
        PaywallData paywall = legacy.getOffering().getPaywall();
        String id2 = null;
        if (paywall != null) {
            revision = paywall.getRevision();
        } else {
            Offering.PaywallComponents paywallComponents = legacy.getOffering().getPaywallComponents();
            if (paywallComponents == null || (data = paywallComponents.getData()) == null) {
                Logger.INSTANCE.e("Null paywall revision trying to create event data");
                return null;
            }
            revision = data.getRevision();
        }
        int i6 = revision;
        PaywallData paywall2 = legacy.getOffering().getPaywall();
        if (paywall2 == null || (id = paywall2.getId()) == null) {
            Offering.PaywallComponents paywallComponents2 = legacy.getOffering().getPaywallComponents();
            if (paywallComponents2 != null && (data2 = paywallComponents2.getData()) != null) {
                id2 = data2.getId();
            }
            str = id2;
        } else {
            str = id;
        }
        Locale localeA = ((g) ((c0) this._lastLocaleList).getValue()).a(0);
        if (localeA == null) {
            localeA = Locale.getDefault();
        }
        PresentedOfferingContext presentedOfferingContext = getPresentedOfferingContext(offering);
        UUID uuidRandomUUID = UUID.randomUUID();
        o.g(uuidRandomUUID, "randomUUID()");
        String lowerCase = getMode().name().toLowerCase(Locale.ROOT);
        o.g(lowerCase, "toLowerCase(...)");
        String string = localeA.toString();
        o.g(string, "locale.toString()");
        return new PaywallEvent.Data(str, presentedOfferingContext, i6, uuidRandomUUID, lowerCase, string, this.isDarkMode, null, null, null, null, null, null, 8064, null);
    }

    private final PaywallEvent.Data createEventData(PaywallState.Loaded.Components components) {
        Offering offering = components.getOffering();
        Offering.PaywallComponents paywallComponents = components.getOffering().getPaywallComponents();
        if (paywallComponents == null) {
            Logger.INSTANCE.e("Null paywall revision trying to create event data");
            return null;
        }
        String id = paywallComponents.getData().getId();
        PresentedOfferingContext presentedOfferingContext = getPresentedOfferingContext(offering);
        int revision = paywallComponents.getData().getRevision();
        UUID uuidRandomUUID = UUID.randomUUID();
        o.g(uuidRandomUUID, "randomUUID()");
        String lowerCase = getMode().name().toLowerCase(Locale.ROOT);
        o.g(lowerCase, "toLowerCase(...)");
        return new PaywallEvent.Data(id, presentedOfferingContext, revision, uuidRandomUUID, lowerCase, components.getLocale().f13226a.toLanguageTag(), this.isDarkMode, null, null, null, null, null, null, 8064, null);
    }
}
