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

import A4.d;
import B4.a;
import C4.c;
import C4.e;
import S4.p;
import androidx.compose.runtime.internal.StabilityInferred;
import com.revenuecat.purchases.CustomerInfo;
import com.revenuecat.purchases.Package;
import com.revenuecat.purchases.ProductType;
import com.revenuecat.purchases.PurchasesException;
import com.revenuecat.purchases.Store;
import com.revenuecat.purchases.SubscriptionInfo;
import com.revenuecat.purchases.common.Constants;
import com.revenuecat.purchases.models.GoogleReplacementMode;
import com.revenuecat.purchases.models.GoogleStoreProduct;
import com.revenuecat.purchases.models.Period;
import com.revenuecat.purchases.models.Price;
import com.revenuecat.purchases.models.StoreProduct;
import com.revenuecat.purchases.paywalls.components.common.ProductChangeConfig;
import com.revenuecat.purchases.ui.revenuecatui.data.PurchasesType;
import com.revenuecat.purchases.ui.revenuecatui.helpers.Logger;
import java.util.Iterator;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
import q.AbstractC2761L;
import w4.k;

/* JADX INFO: loaded from: classes3.dex */
@StabilityInferred(parameters = 0)
public final class ProductChangeCalculator {
    private static final int MONTHS_IN_HALF_YEAR = 6;
    private static final int MONTHS_IN_QUARTER = 3;
    private static final int MONTHS_IN_YEAR = 12;
    private static final long SANDBOX_HALF_YEAR_MINUTES = 15;
    private static final long SANDBOX_MONTHLY_MINUTES = 5;
    private static final long SANDBOX_QUARTER_MINUTES = 10;
    private static final long SANDBOX_YEARLY_MINUTES = 30;
    private final PurchasesType purchases;
    public static final Companion Companion = new Companion(null);
    public static final int $stable = 8;

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

        public final Long getNormalizedPrice$revenuecatui_defaultsBc8Release(StoreProduct storeProduct, boolean z6) {
            o.h(storeProduct, "<this>");
            Period period = storeProduct.getPeriod();
            if (period == null) {
                return null;
            }
            long amountMicros = storeProduct.getPrice().getAmountMicros();
            if (z6) {
                return Long.valueOf(amountMicros / getSandboxRenewalMinutes$revenuecatui_defaultsBc8Release(period));
            }
            Price pricePricePerMonth$default = StoreProduct.pricePerMonth$default(storeProduct, null, 1, null);
            if (pricePricePerMonth$default != null) {
                return Long.valueOf(pricePricePerMonth$default.getAmountMicros());
            }
            return null;
        }

        public final long getSandboxRenewalMinutes$revenuecatui_defaultsBc8Release(Period period) {
            o.h(period, "period");
            double valueInMonths = period.getValueInMonths();
            if (valueInMonths >= 12.0d) {
                return ProductChangeCalculator.SANDBOX_YEARLY_MINUTES;
            }
            if (valueInMonths >= 6.0d) {
                return ProductChangeCalculator.SANDBOX_HALF_YEAR_MINUTES;
            }
            if (valueInMonths >= 3.0d) {
                return 10L;
            }
            return ProductChangeCalculator.SANDBOX_MONTHLY_MINUTES;
        }

        public final k parseProductIdentifier$revenuecatui_defaultsBc8Release(String productIdentifier) {
            o.h(productIdentifier, "productIdentifier");
            String strK1 = p.k1(productIdentifier, Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR);
            String strG1 = p.g1(productIdentifier, Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, "");
            if (strG1.length() <= 0) {
                strG1 = null;
            }
            return new k(strK1, strG1);
        }

        public final k subscriptionIdentifiers$revenuecatui_defaultsBc8Release(StoreProduct storeProduct) {
            o.h(storeProduct, "<this>");
            if (!(storeProduct instanceof GoogleStoreProduct)) {
                return parseProductIdentifier$revenuecatui_defaultsBc8Release(storeProduct.getId());
            }
            GoogleStoreProduct googleStoreProduct = (GoogleStoreProduct) storeProduct;
            return new k(googleStoreProduct.getProductId(), googleStoreProduct.getBasePlanId());
        }

        private Companion() {
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.ProductChangeCalculator$calculateProductChange$1, reason: invalid class name */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.ProductChangeCalculator", f = "ProductChangeCalculator.kt", l = {72}, m = "calculateProductChange")
    public static final class AnonymousClass1 extends c {
        Object L$0;
        Object L$1;
        Object L$2;
        Object L$3;
        Object L$4;
        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 ProductChangeCalculator.this.calculateProductChange(null, null, null, this);
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.ui.revenuecatui.data.ProductChangeCalculator$calculateProductChangeInfo$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.ui.revenuecatui.data.ProductChangeCalculator", f = "ProductChangeCalculator.kt", l = {40, 45}, m = "calculateProductChangeInfo")
    public static final class C17211 extends c {
        Object L$0;
        Object L$1;
        Object L$2;
        int label;
        /* synthetic */ Object result;

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

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

    public ProductChangeCalculator(PurchasesType purchases) {
        o.h(purchases, "purchases");
        this.purchases = purchases;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object calculateProductChange(SubscriptionInfo subscriptionInfo, Package r13, ProductChangeConfig productChangeConfig, d dVar) {
        AnonymousClass1 anonymousClass1;
        String productIdentifier;
        String str;
        Object objAwaitGetProduct;
        GoogleReplacementMode downgradeReplacementMode;
        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);
            }
        }
        Object obj = anonymousClass1.result;
        a aVar = a.f492a;
        int i7 = anonymousClass1.label;
        if (i7 == 0) {
            AbstractC2467b.l(obj);
            productIdentifier = subscriptionInfo.getProductIdentifier();
            String productPlanIdentifier = subscriptionInfo.getProductPlanIdentifier();
            str = (String) Companion.subscriptionIdentifiers$revenuecatui_defaultsBc8Release(r13.getProduct()).f25469a;
            if (o.c(productIdentifier, str)) {
                Logger.INSTANCE.d("Same product (" + str + "), Google handles base plan change automatically");
                return null;
            }
            PurchasesType purchasesType = this.purchases;
            anonymousClass1.L$0 = subscriptionInfo;
            anonymousClass1.L$1 = r13;
            anonymousClass1.L$2 = productChangeConfig;
            anonymousClass1.L$3 = productIdentifier;
            anonymousClass1.L$4 = str;
            anonymousClass1.label = 1;
            objAwaitGetProduct = purchasesType.awaitGetProduct(productIdentifier, productPlanIdentifier, anonymousClass1);
            if (objAwaitGetProduct == aVar) {
                return aVar;
            }
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            String str2 = (String) anonymousClass1.L$4;
            String str3 = (String) anonymousClass1.L$3;
            productChangeConfig = (ProductChangeConfig) anonymousClass1.L$2;
            Package r12 = (Package) anonymousClass1.L$1;
            SubscriptionInfo subscriptionInfo2 = (SubscriptionInfo) anonymousClass1.L$0;
            AbstractC2467b.l(obj);
            str = str2;
            subscriptionInfo = subscriptionInfo2;
            objAwaitGetProduct = obj;
            productIdentifier = str3;
            r13 = r12;
        }
        StoreProduct storeProduct = (StoreProduct) objAwaitGetProduct;
        boolean zIsSandbox = subscriptionInfo.isSandbox();
        Long normalizedPrice$revenuecatui_defaultsBc8Release = storeProduct != null ? Companion.getNormalizedPrice$revenuecatui_defaultsBc8Release(storeProduct, zIsSandbox) : null;
        Long normalizedPrice$revenuecatui_defaultsBc8Release2 = Companion.getNormalizedPrice$revenuecatui_defaultsBc8Release(r13.getProduct(), zIsSandbox);
        if (normalizedPrice$revenuecatui_defaultsBc8Release == null || normalizedPrice$revenuecatui_defaultsBc8Release2 == null || normalizedPrice$revenuecatui_defaultsBc8Release2.longValue() <= normalizedPrice$revenuecatui_defaultsBc8Release.longValue()) {
            Logger logger = Logger.INSTANCE;
            StringBuilder sbI = AbstractC2761L.i("Detected downgrade: ", productIdentifier, " -> ", str, " (old: ");
            sbI.append(normalizedPrice$revenuecatui_defaultsBc8Release);
            sbI.append(", new: ");
            sbI.append(normalizedPrice$revenuecatui_defaultsBc8Release2);
            sbI.append(", sandbox: ");
            sbI.append(zIsSandbox);
            sbI.append(')');
            logger.d(sbI.toString());
            downgradeReplacementMode = productChangeConfig.getDowngradeReplacementMode();
        } else {
            Logger logger2 = Logger.INSTANCE;
            StringBuilder sbI2 = AbstractC2761L.i("Detected upgrade: ", productIdentifier, " -> ", str, " (old: ");
            sbI2.append(normalizedPrice$revenuecatui_defaultsBc8Release);
            sbI2.append(", new: ");
            sbI2.append(normalizedPrice$revenuecatui_defaultsBc8Release2);
            sbI2.append(", sandbox: ");
            sbI2.append(zIsSandbox);
            sbI2.append(')');
            logger2.d(sbI2.toString());
            downgradeReplacementMode = productChangeConfig.getUpgradeReplacementMode();
        }
        return new ProductChangeInfo(productIdentifier, downgradeReplacementMode);
    }

    /* JADX WARN: Code restructure failed: missing block: B:40:0x00a7, code lost:
    
        if (r11 == r1) goto L41;
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object calculateProductChangeInfo(Package r9, ProductChangeConfig productChangeConfig, d dVar) {
        C17211 c17211;
        ProductChangeCalculator productChangeCalculator;
        Object next;
        if (dVar instanceof C17211) {
            c17211 = (C17211) dVar;
            int i6 = c17211.label;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c17211.label = i6 - Integer.MIN_VALUE;
            } else {
                c17211 = new C17211(dVar);
            }
        }
        Object objAwaitCustomerInfo$default = c17211.result;
        a aVar = a.f492a;
        int i7 = c17211.label;
        try {
            if (i7 == 0) {
                AbstractC2467b.l(objAwaitCustomerInfo$default);
                if (r9.getProduct().getType() != ProductType.SUBS) {
                    return null;
                }
                PurchasesType purchasesType = this.purchases;
                c17211.L$0 = this;
                c17211.L$1 = r9;
                c17211.L$2 = productChangeConfig;
                c17211.label = 1;
                objAwaitCustomerInfo$default = PurchasesType.DefaultImpls.awaitCustomerInfo$default(purchasesType, null, c17211, 1, null);
                if (objAwaitCustomerInfo$default == aVar) {
                    return aVar;
                }
                productChangeCalculator = this;
            } else {
                if (i7 != 1) {
                    if (i7 != 2) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    AbstractC2467b.l(objAwaitCustomerInfo$default);
                    return (ProductChangeInfo) objAwaitCustomerInfo$default;
                }
                productChangeConfig = (ProductChangeConfig) c17211.L$2;
                r9 = (Package) c17211.L$1;
                productChangeCalculator = (ProductChangeCalculator) c17211.L$0;
                AbstractC2467b.l(objAwaitCustomerInfo$default);
            }
            Iterator<T> it = ((CustomerInfo) objAwaitCustomerInfo$default).getSubscriptionsByProductIdentifier().values().iterator();
            while (true) {
                if (!it.hasNext()) {
                    next = null;
                    break;
                }
                next = it.next();
                SubscriptionInfo subscriptionInfo = (SubscriptionInfo) next;
                if (subscriptionInfo.isActive() && subscriptionInfo.getStore() == Store.PLAY_STORE) {
                    break;
                }
            }
            SubscriptionInfo subscriptionInfo2 = (SubscriptionInfo) next;
            if (subscriptionInfo2 == null) {
                return null;
            }
            c17211.L$0 = null;
            c17211.L$1 = null;
            c17211.L$2 = null;
            c17211.label = 2;
            objAwaitCustomerInfo$default = productChangeCalculator.calculateProductChange(subscriptionInfo2, r9, productChangeConfig, c17211);
        } catch (PurchasesException e6) {
            Logger.INSTANCE.e("Error determining product change info: " + e6.getMessage());
            return null;
        }
    }
}
