package com.revenuecat.purchases.google.history;

import A4.d;
import A4.j;
import C4.c;
import C4.e;
import V4.C0772m;
import V4.C0781t;
import V4.E;
import V4.InterfaceC0780s;
import V4.u0;
import a.AbstractC0872a;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import com.android.vending.billing.IInAppBillingService;
import com.revenuecat.purchases.LogHandler;
import com.revenuecat.purchases.LogLevel;
import com.revenuecat.purchases.ProductType;
import com.revenuecat.purchases.common.Config;
import com.revenuecat.purchases.common.LogWrapperKt;
import com.revenuecat.purchases.google.ErrorsKt;
import com.revenuecat.purchases.google.history.PurchaseData;
import e5.a;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
import w4.C3137A;
import w4.k;
import x4.AbstractC3251q;
import x4.AbstractC3253s;
import x4.y;

/* JADX INFO: loaded from: classes3.dex */
public final class PurchaseHistoryManager {
    public static final Companion Companion = new Companion(null);
    private static final int MAX_PAGINATION_PAGES = 50;
    private IInAppBillingService billingService;
    private InterfaceC0780s connectDeferred;
    private final Context context;
    private final a operationsMutex;
    private final Map<String, InterfaceC0780s> queryDeferreds;
    private ServiceConnection serviceConnection;

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

        private Companion() {
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.google.history.PurchaseHistoryManager$connect$1, reason: invalid class name */
    @e(c = "com.revenuecat.purchases.google.history.PurchaseHistoryManager", f = "PurchaseHistoryManager.kt", l = {347, 374, 379, 347, 347}, m = "connect")
    public static final class AnonymousClass1 extends c {
        int I$0;
        Object L$0;
        Object L$1;
        Object L$2;
        Object L$3;
        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 PurchaseHistoryManager.this.connect(this);
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.google.history.PurchaseHistoryManager$disconnect$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.google.history.PurchaseHistoryManager", f = "PurchaseHistoryManager.kt", l = {346}, m = "disconnect")
    public static final class C14361 extends c {
        Object L$0;
        Object L$1;
        int label;
        /* synthetic */ Object result;

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

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

    /* JADX INFO: renamed from: com.revenuecat.purchases.google.history.PurchaseHistoryManager$queryAllPurchaseHistory$1, reason: invalid class name and case insensitive filesystem */
    @e(c = "com.revenuecat.purchases.google.history.PurchaseHistoryManager", f = "PurchaseHistoryManager.kt", l = {355, 382, 355, 355}, m = "queryAllPurchaseHistory")
    public static final class C14371 extends c {
        int I$0;
        Object L$0;
        Object L$1;
        Object L$2;
        Object L$3;
        int label;
        /* synthetic */ Object result;

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

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

    public PurchaseHistoryManager(Context context) {
        o.h(context, "context");
        this.context = context;
        this.operationsMutex = e5.d.a();
        this.queryDeferreds = new LinkedHashMap();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void cleanup() {
        j jVar = this.connectDeferred;
        if (jVar != null) {
            ((u0) jVar).cancel(null);
        }
        Iterator<Map.Entry<String, InterfaceC0780s>> it = this.queryDeferreds.entrySet().iterator();
        while (it.hasNext()) {
            it.next().getValue().cancel(null);
        }
        ServiceConnection serviceConnection = this.serviceConnection;
        if (serviceConnection != null) {
            try {
                this.context.unbindService(serviceConnection);
                LogLevel logLevel = LogLevel.DEBUG;
                LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
                if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                    currentLogHandler.d("[Purchases] - " + logLevel.name(), "AIDL Billing service disconnected");
                }
            } catch (Throwable th) {
                LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Error disconnecting from AIDL Billing service", th);
            }
        }
        this.billingService = null;
        this.serviceConnection = null;
        this.connectDeferred = null;
        this.queryDeferreds.clear();
    }

    private final <T> Object getOrExecute(J4.a aVar, J4.c cVar, String str, boolean z6, J4.c cVar2, d dVar) throws Throwable {
        k kVar;
        e5.c cVar3 = (e5.c) this.operationsMutex;
        cVar3.e(dVar);
        try {
            j jVar = (InterfaceC0780s) aVar.invoke();
            if (jVar != null) {
                if (((u0) jVar).E()) {
                    LogLevel logLevel = LogLevel.DEBUG;
                    LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
                    if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                        currentLogHandler.d("[Purchases] - " + logLevel.name(), str + " (already completed)");
                    }
                } else {
                    LogLevel logLevel2 = LogLevel.DEBUG;
                    LogHandler currentLogHandler2 = LogWrapperKt.getCurrentLogHandler();
                    if (Config.INSTANCE.getLogLevel().compareTo(logLevel2) <= 0) {
                        currentLogHandler2.d("[Purchases] - " + logLevel2.name(), str);
                    }
                }
                kVar = new k(jVar, Boolean.FALSE);
            } else {
                C0781t c0781tB = E.b();
                cVar.invoke(c0781tB);
                kVar = new k(c0781tB, Boolean.TRUE);
            }
            cVar3.g(null);
            j jVar2 = (InterfaceC0780s) kVar.f25469a;
            try {
                if (!((Boolean) kVar.f25470b).booleanValue()) {
                    Object objM = ((C0781t) jVar2).m(dVar);
                    B4.a aVar2 = B4.a.f492a;
                    return objM;
                }
                try {
                    try {
                        Object objInvoke = cVar2.invoke(dVar);
                        ((C0781t) jVar2).G(objInvoke);
                        if (!z6) {
                            return objInvoke;
                        }
                        ((e5.c) this.operationsMutex).e(dVar);
                        try {
                            cVar.invoke(null);
                            return objInvoke;
                        } finally {
                        }
                    } catch (CancellationException e6) {
                        ((u0) jVar2).cancel(null);
                        throw e6;
                    }
                } catch (Throwable th) {
                    ((C0781t) jVar2).U(th);
                    throw th;
                }
            } catch (Throwable th2) {
                if (z6) {
                    ((e5.c) this.operationsMutex).e(dVar);
                    try {
                        cVar.invoke(null);
                    } finally {
                    }
                }
                throw th2;
            }
        } catch (Throwable th3) {
            throw th3;
        }
    }

    public static Object getOrExecute$default(PurchaseHistoryManager purchaseHistoryManager, J4.a aVar, J4.c cVar, String str, boolean z6, J4.c cVar2, d dVar, int i6, Object obj) throws Throwable {
        k kVar;
        if ((i6 & 8) != 0) {
            z6 = true;
        }
        e5.c cVar3 = (e5.c) purchaseHistoryManager.operationsMutex;
        cVar3.e(dVar);
        try {
            j jVar = (InterfaceC0780s) aVar.invoke();
            if (jVar != null) {
                if (((u0) jVar).E()) {
                    LogLevel logLevel = LogLevel.DEBUG;
                    LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
                    if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                        currentLogHandler.d("[Purchases] - " + logLevel.name(), str + " (already completed)");
                    }
                } else {
                    LogLevel logLevel2 = LogLevel.DEBUG;
                    LogHandler currentLogHandler2 = LogWrapperKt.getCurrentLogHandler();
                    if (Config.INSTANCE.getLogLevel().compareTo(logLevel2) <= 0) {
                        currentLogHandler2.d("[Purchases] - " + logLevel2.name(), str);
                    }
                }
                kVar = new k(jVar, Boolean.FALSE);
            } else {
                C0781t c0781tB = E.b();
                cVar.invoke(c0781tB);
                kVar = new k(c0781tB, Boolean.TRUE);
            }
            cVar3.g(null);
            j jVar2 = (InterfaceC0780s) kVar.f25469a;
            try {
                if (!((Boolean) kVar.f25470b).booleanValue()) {
                    Object objM = ((C0781t) jVar2).m(dVar);
                    B4.a aVar2 = B4.a.f492a;
                    return objM;
                }
                try {
                    Object objInvoke = cVar2.invoke(dVar);
                    ((C0781t) jVar2).G(objInvoke);
                    if (!z6) {
                        return objInvoke;
                    }
                    ((e5.c) purchaseHistoryManager.operationsMutex).e(dVar);
                    try {
                        cVar.invoke(null);
                        return objInvoke;
                    } finally {
                    }
                } catch (CancellationException e6) {
                    ((u0) jVar2).cancel(null);
                    throw e6;
                } catch (Throwable th) {
                    ((C0781t) jVar2).U(th);
                    throw th;
                }
            } catch (Throwable th2) {
                if (z6) {
                    ((e5.c) purchaseHistoryManager.operationsMutex).e(dVar);
                    try {
                        cVar.invoke(null);
                    } finally {
                    }
                }
                throw th2;
            }
        } catch (Throwable th3) {
            throw th3;
        }
    }

    private final PurchaseHistoryResult parseResponse(Bundle bundle) {
        PurchaseHistoryRecord purchaseHistoryRecord;
        int i6 = bundle.getInt(BillingConstants.RESPONSE_CODE, -1);
        if (i6 != 0) {
            LogLevel logLevel = LogLevel.WARN;
            LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
            if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                currentLogHandler.w(com.google.android.recaptcha.internal.a.j(logLevel, new StringBuilder("[Purchases] - ")), "Purchase history query returned non-OK response: " + ErrorsKt.getBillingResponseCodeName(i6));
            }
            return new PurchaseHistoryResult(i6, y.f26065a, null);
        }
        ArrayList<String> stringArrayList = bundle.getStringArrayList(BillingConstants.INAPP_PURCHASE_DATA_LIST);
        if (stringArrayList == null) {
            stringArrayList = new ArrayList<>();
        }
        ArrayList<String> stringArrayList2 = bundle.getStringArrayList(BillingConstants.INAPP_DATA_SIGNATURE_LIST);
        if (stringArrayList2 == null) {
            stringArrayList2 = new ArrayList<>();
        }
        String string = bundle.getString(BillingConstants.INAPP_CONTINUATION_TOKEN);
        ArrayList<k> arrayListJ0 = AbstractC3251q.J0(stringArrayList, stringArrayList2);
        ArrayList arrayList = new ArrayList();
        for (k kVar : arrayListJ0) {
            String purchaseJson = (String) kVar.f25469a;
            String signature = (String) kVar.f25470b;
            PurchaseData.Companion companion = PurchaseData.Companion;
            o.g(purchaseJson, "purchaseJson");
            PurchaseData purchaseDataFromJson = companion.fromJson(purchaseJson);
            if (purchaseDataFromJson != null) {
                o.g(signature, "signature");
                purchaseHistoryRecord = new PurchaseHistoryRecord(purchaseDataFromJson, signature, purchaseJson);
            } else {
                LogLevel logLevel2 = LogLevel.WARN;
                LogHandler currentLogHandler2 = LogWrapperKt.getCurrentLogHandler();
                if (Config.INSTANCE.getLogLevel().compareTo(logLevel2) <= 0) {
                    currentLogHandler2.w(com.google.android.recaptcha.internal.a.j(logLevel2, new StringBuilder("[Purchases] - ")), "Failed to parse purchase data: ".concat(purchaseJson));
                }
                purchaseHistoryRecord = null;
            }
            if (purchaseHistoryRecord != null) {
                arrayList.add(purchaseHistoryRecord);
            }
        }
        LogLevel logLevel3 = LogLevel.DEBUG;
        LogHandler currentLogHandler3 = LogWrapperKt.getCurrentLogHandler();
        if (Config.INSTANCE.getLogLevel().compareTo(logLevel3) <= 0) {
            currentLogHandler3.d(com.google.android.recaptcha.internal.a.j(logLevel3, new StringBuilder("[Purchases] - ")), "Parsed " + arrayList.size() + " purchase history records from AIDL.");
        }
        return new PurchaseHistoryResult(i6, arrayList, string);
    }

    public static /* synthetic */ Object queryAllPurchaseHistory$default(PurchaseHistoryManager purchaseHistoryManager, String str, d dVar, int i6, Object obj) {
        if ((i6 & 1) != 0) {
            str = "inapp";
        }
        return purchaseHistoryManager.queryAllPurchaseHistory(str, dVar);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final PurchaseHistoryResult queryPurchaseHistory(String str, String str2) {
        IInAppBillingService iInAppBillingService = this.billingService;
        y yVar = y.f26065a;
        if (iInAppBillingService == null) {
            return new PurchaseHistoryResult(2, yVar, null);
        }
        try {
            Bundle bundle = new Bundle();
            LogLevel logLevel = LogLevel.DEBUG;
            LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
            if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                currentLogHandler.d("[Purchases] - " + logLevel.name(), "Calling getPurchaseHistory via AIDL with API version 7, type=" + str);
            }
            IInAppBillingService iInAppBillingService2 = this.billingService;
            o.e(iInAppBillingService2);
            Bundle response = iInAppBillingService2.getPurchaseHistory(7, this.context.getPackageName(), str, str2, bundle);
            o.g(response, "response");
            return parseResponse(response);
        } catch (Throwable th) {
            LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Error querying purchase history via AIDL", th);
            return new PurchaseHistoryResult(6, yVar, null);
        }
    }

    public static /* synthetic */ PurchaseHistoryResult queryPurchaseHistory$default(PurchaseHistoryManager purchaseHistoryManager, String str, String str2, int i6, Object obj) {
        if ((i6 & 1) != 0) {
            str = "inapp";
        }
        if ((i6 & 2) != 0) {
            str2 = null;
        }
        return purchaseHistoryManager.queryPurchaseHistory(str, str2);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:67:0x01e5  */
    /* JADX WARN: Removed duplicated region for block: B:76:0x0210 A[ORIG_RETURN, RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0019  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object connect(d dVar) throws Throwable {
        AnonymousClass1 anonymousClass1;
        Throwable th;
        PurchaseHistoryManager purchaseHistoryManager;
        a aVar;
        String str;
        int i6;
        PurchaseHistoryManager purchaseHistoryManager2;
        final PurchaseHistoryManager purchaseHistoryManager3;
        a aVar2;
        k kVar;
        Object obj;
        int i7;
        PurchaseHistoryManager purchaseHistoryManager4;
        Object obj2;
        PurchaseHistoryManager purchaseHistoryManager5;
        a aVar3;
        if (dVar instanceof AnonymousClass1) {
            anonymousClass1 = (AnonymousClass1) dVar;
            int i8 = anonymousClass1.label;
            if ((i8 & Integer.MIN_VALUE) != 0) {
                anonymousClass1.label = i8 - Integer.MIN_VALUE;
            } else {
                anonymousClass1 = new AnonymousClass1(dVar);
            }
        }
        Object objR = anonymousClass1.result;
        B4.a aVar4 = B4.a.f492a;
        int i9 = anonymousClass1.label;
        try {
            try {
            } catch (Throwable th2) {
                if ("[Purchases] - ERROR" == 0) {
                    throw th2;
                }
                a aVar5 = 3.operationsMutex;
                anonymousClass1.L$0 = 2;
                anonymousClass1.L$1 = th2;
                anonymousClass1.L$2 = aVar5;
                anonymousClass1.L$3 = null;
                anonymousClass1.label = 5;
                e5.c cVar = (e5.c) aVar5;
                if (cVar.e(anonymousClass1) != aVar4) {
                    th = th2;
                    purchaseHistoryManager = 2;
                    aVar = cVar;
                }
                return aVar4;
            }
            if (i9 == 0) {
                AbstractC2467b.l(objR);
                a aVar6 = this.operationsMutex;
                anonymousClass1.L$0 = this;
                anonymousClass1.L$1 = this;
                str = "Connection already in progress or completed, hooking into existing operation";
                anonymousClass1.L$2 = "Connection already in progress or completed, hooking into existing operation";
                anonymousClass1.L$3 = aVar6;
                i6 = 0;
                anonymousClass1.I$0 = 0;
                anonymousClass1.label = 1;
                e5.c cVar2 = (e5.c) aVar6;
                if (cVar2.e(anonymousClass1) != aVar4) {
                    purchaseHistoryManager2 = this;
                    purchaseHistoryManager3 = purchaseHistoryManager2;
                    aVar2 = cVar2;
                }
                return aVar4;
            }
            if (i9 != 1) {
                if (i9 == 2) {
                    AbstractC2467b.l(objR);
                    return objR;
                }
                if (i9 != 3) {
                    if (i9 == 4) {
                        a aVar7 = (a) anonymousClass1.L$2;
                        obj2 = anonymousClass1.L$1;
                        purchaseHistoryManager5 = (PurchaseHistoryManager) anonymousClass1.L$0;
                        AbstractC2467b.l(objR);
                        aVar3 = aVar7;
                        try {
                            purchaseHistoryManager5.connectDeferred = null;
                            ((e5.c) aVar3).g(null);
                            return obj2;
                        } finally {
                        }
                    }
                    if (i9 != 5) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    a aVar8 = (a) anonymousClass1.L$2;
                    th = (Throwable) anonymousClass1.L$1;
                    PurchaseHistoryManager purchaseHistoryManager6 = (PurchaseHistoryManager) anonymousClass1.L$0;
                    AbstractC2467b.l(objR);
                    aVar = aVar8;
                    purchaseHistoryManager = purchaseHistoryManager6;
                    try {
                        purchaseHistoryManager.connectDeferred = null;
                        ((e5.c) aVar).g(null);
                        throw th;
                    } finally {
                    }
                }
                i7 = anonymousClass1.I$0;
                obj = (InterfaceC0780s) anonymousClass1.L$2;
                purchaseHistoryManager4 = (PurchaseHistoryManager) anonymousClass1.L$1;
                PurchaseHistoryManager purchaseHistoryManager7 = (PurchaseHistoryManager) anonymousClass1.L$0;
                try {
                    AbstractC2467b.l(objR);
                    purchaseHistoryManager3 = purchaseHistoryManager7;
                    try {
                        ((C0781t) obj).G(objR);
                        if (i7 != 0) {
                            return objR;
                        }
                        a aVar9 = purchaseHistoryManager4.operationsMutex;
                        anonymousClass1.L$0 = purchaseHistoryManager3;
                        anonymousClass1.L$1 = objR;
                        anonymousClass1.L$2 = aVar9;
                        anonymousClass1.L$3 = null;
                        anonymousClass1.label = 4;
                        e5.c cVar3 = (e5.c) aVar9;
                        if (cVar3.e(anonymousClass1) != aVar4) {
                            obj2 = objR;
                            purchaseHistoryManager5 = purchaseHistoryManager3;
                            aVar3 = cVar3;
                            purchaseHistoryManager5.connectDeferred = null;
                            ((e5.c) aVar3).g(null);
                            return obj2;
                        }
                        return aVar4;
                    } catch (CancellationException e6) {
                        e = e6;
                        ((u0) obj).cancel(null);
                        throw e;
                    } catch (Throwable th3) {
                        th = th3;
                        ((C0781t) obj).U(th);
                        throw th;
                    }
                } catch (CancellationException e7) {
                    e = e7;
                    ((u0) obj).cancel(null);
                    throw e;
                } catch (Throwable th4) {
                    th = th4;
                    ((C0781t) obj).U(th);
                    throw th;
                }
            }
            i6 = anonymousClass1.I$0;
            a aVar10 = (a) anonymousClass1.L$3;
            str = (String) anonymousClass1.L$2;
            purchaseHistoryManager2 = (PurchaseHistoryManager) anonymousClass1.L$1;
            purchaseHistoryManager3 = (PurchaseHistoryManager) anonymousClass1.L$0;
            AbstractC2467b.l(objR);
            aVar2 = aVar10;
            j jVar = purchaseHistoryManager3.connectDeferred;
            if (jVar != null) {
                if (((u0) jVar).E()) {
                    LogLevel logLevel = LogLevel.DEBUG;
                    LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
                    if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                        currentLogHandler.d("[Purchases] - " + logLevel.name(), str + " (already completed)");
                    }
                } else {
                    LogLevel logLevel2 = LogLevel.DEBUG;
                    LogHandler currentLogHandler2 = LogWrapperKt.getCurrentLogHandler();
                    if (Config.INSTANCE.getLogLevel().compareTo(logLevel2) <= 0) {
                        currentLogHandler2.d("[Purchases] - " + logLevel2.name(), str);
                    }
                }
                kVar = new k(jVar, Boolean.FALSE);
            } else {
                C0781t c0781tB = E.b();
                purchaseHistoryManager3.connectDeferred = c0781tB;
                kVar = new k(c0781tB, Boolean.TRUE);
            }
            ((e5.c) aVar2).g(null);
            InterfaceC0780s interfaceC0780s = (InterfaceC0780s) kVar.f25469a;
            if (((Boolean) kVar.f25470b).booleanValue()) {
                try {
                    anonymousClass1.L$0 = purchaseHistoryManager3;
                    anonymousClass1.L$1 = purchaseHistoryManager2;
                    anonymousClass1.L$2 = interfaceC0780s;
                    anonymousClass1.L$3 = anonymousClass1;
                    anonymousClass1.I$0 = i6;
                    anonymousClass1.label = 3;
                    final C0772m c0772m = new C0772m(1, AbstractC0872a.V(anonymousClass1));
                    c0772m.s();
                    ServiceConnection serviceConnection = new ServiceConnection() { // from class: com.revenuecat.purchases.google.history.PurchaseHistoryManager$connect$4$1$connection$1
                        @Override // android.content.ServiceConnection
                        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                            LogLevel logLevel3 = LogLevel.DEBUG;
                            LogHandler currentLogHandler3 = LogWrapperKt.getCurrentLogHandler();
                            Config config = Config.INSTANCE;
                            if (config.getLogLevel().compareTo(logLevel3) <= 0) {
                                com.google.android.recaptcha.internal.a.u(logLevel3, new StringBuilder("[Purchases] - "), currentLogHandler3, "AIDL Billing service connected");
                            }
                            if (c0772m.isActive()) {
                                purchaseHistoryManager3.billingService = IInAppBillingService.Stub.asInterface(iBinder);
                                purchaseHistoryManager3.serviceConnection = this;
                                c0772m.resumeWith(Boolean.TRUE);
                                return;
                            }
                            LogHandler currentLogHandler4 = LogWrapperKt.getCurrentLogHandler();
                            if (config.getLogLevel().compareTo(logLevel3) <= 0) {
                                com.google.android.recaptcha.internal.a.u(logLevel3, new StringBuilder("[Purchases] - "), currentLogHandler4, "AIDL Billing service connected after cancellation, cleaning up");
                            }
                            purchaseHistoryManager3.cleanup();
                        }

                        @Override // android.content.ServiceConnection
                        public void onServiceDisconnected(ComponentName componentName) {
                            LogLevel logLevel3 = LogLevel.DEBUG;
                            LogHandler currentLogHandler3 = LogWrapperKt.getCurrentLogHandler();
                            if (Config.INSTANCE.getLogLevel().compareTo(logLevel3) <= 0) {
                                com.google.android.recaptcha.internal.a.u(logLevel3, new StringBuilder("[Purchases] - "), currentLogHandler3, "AIDL Billing service disconnected");
                            }
                            purchaseHistoryManager3.cleanup();
                        }
                    };
                    c0772m.u(new PurchaseHistoryManager$connect$4$1$1(purchaseHistoryManager3));
                    try {
                        Intent intent = new Intent(BillingConstants.BILLING_SERVICE_ACTION);
                        intent.setPackage("com.android.vending");
                        if (!purchaseHistoryManager3.context.bindService(intent, serviceConnection, 1)) {
                            LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Failed to bind to AIDL billing service", null);
                            c0772m.resumeWith(AbstractC2467b.d(new Exception("Failed to bind to Google Play billing service")));
                        }
                    } catch (Throwable th5) {
                        LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Error binding to AIDL billing service", th5);
                        c0772m.resumeWith(AbstractC2467b.d(th5));
                    }
                    objR = c0772m.r();
                    B4.a aVar11 = B4.a.f492a;
                    if (objR != aVar4) {
                        i7 = i6;
                        obj = interfaceC0780s;
                        purchaseHistoryManager4 = purchaseHistoryManager2;
                        ((C0781t) obj).G(objR);
                        if (i7 != 0) {
                        }
                    }
                } catch (CancellationException e8) {
                    e = e8;
                    obj = interfaceC0780s;
                    ((u0) obj).cancel(null);
                    throw e;
                } catch (Throwable th6) {
                    th = th6;
                    obj = interfaceC0780s;
                    ((C0781t) obj).U(th);
                    throw th;
                }
            } else {
                anonymousClass1.L$0 = null;
                anonymousClass1.L$1 = null;
                anonymousClass1.L$2 = null;
                anonymousClass1.L$3 = null;
                anonymousClass1.label = 2;
                Object objM = ((C0781t) interfaceC0780s).m(anonymousClass1);
                if (objM != aVar4) {
                    return objM;
                }
            }
            return aVar4;
        } catch (Throwable th7) {
            throw th7;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object disconnect(d dVar) {
        C14361 c14361;
        PurchaseHistoryManager purchaseHistoryManager;
        a aVar;
        if (dVar instanceof C14361) {
            c14361 = (C14361) dVar;
            int i6 = c14361.label;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c14361.label = i6 - Integer.MIN_VALUE;
            } else {
                c14361 = new C14361(dVar);
            }
        }
        Object obj = c14361.result;
        B4.a aVar2 = B4.a.f492a;
        int i7 = c14361.label;
        if (i7 == 0) {
            AbstractC2467b.l(obj);
            a aVar3 = this.operationsMutex;
            c14361.L$0 = this;
            c14361.L$1 = aVar3;
            c14361.label = 1;
            e5.c cVar = (e5.c) aVar3;
            if (cVar.e(c14361) == aVar2) {
                return aVar2;
            }
            purchaseHistoryManager = this;
            aVar = cVar;
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            aVar = (a) c14361.L$1;
            purchaseHistoryManager = (PurchaseHistoryManager) c14361.L$0;
            AbstractC2467b.l(obj);
        }
        try {
            purchaseHistoryManager.cleanup();
            ((e5.c) aVar).g(null);
            return C3137A.f25453a;
        } catch (Throwable th) {
            ((e5.c) aVar).g(null);
            throw th;
        }
    }

    /* JADX WARN: Can't wrap try/catch for region: R(9:(1:(1:(1:(1:(2:14|15)(5:16|114|100|101|121))(6:17|18|110|82|83|84))(2:19|20))(1:21))(3:22|(1:25)|98)|109|26|27|(3:29|(2:31|(1:33))(2:36|(1:38))|39)(1:40)|41|108|(2:43|(0)(1:46))(10:112|47|(2:48|(3:117|50|(1:52)(0))(2:57|(2:118|59)(3:60|(1:62)|(1:67)(1:119))))|68|(1:70)(1:71)|72|(2:75|73)|120|76|(2:78|(6:81|18|110|82|83|84))(1:88))|98) */
    /* JADX WARN: Code restructure failed: missing block: B:105:0x02ef, code lost:
    
        throw r0;
     */
    /* JADX WARN: Code restructure failed: missing block: B:91:0x02b2, code lost:
    
        r0 = move-exception;
     */
    /* JADX WARN: Code restructure failed: missing block: B:95:0x02bb, code lost:
    
        if (r7 != 0) goto L96;
     */
    /* JADX WARN: Code restructure failed: missing block: B:96:0x02bd, code lost:
    
        r2 = r12.operationsMutex;
        r3.L$0 = r12;
        r3.L$1 = r11;
        r3.L$2 = r0;
        r3.L$3 = r2;
        r3.label = 4;
        r2 = (e5.c) r2;
     */
    /* JADX WARN: Code restructure failed: missing block: B:97:0x02d2, code lost:
    
        if (r2.e(r3) != r4) goto L99;
     */
    /* JADX WARN: Code restructure failed: missing block: B:99:0x02d5, code lost:
    
        r4 = r0;
        r5 = r11;
        r3 = r12;
        r2 = r2;
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0019  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object queryAllPurchaseHistory(String str, d dVar) throws Throwable {
        C14371 c14371;
        String str2;
        String str3;
        PurchaseHistoryManager purchaseHistoryManager;
        a aVar;
        k kVar;
        List list;
        String str4;
        PurchaseHistoryManager purchaseHistoryManager2;
        a aVar2;
        a aVar3;
        if (dVar instanceof C14371) {
            c14371 = (C14371) dVar;
            int i6 = c14371.label;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c14371.label = i6 - Integer.MIN_VALUE;
            } else {
                c14371 = new C14371(dVar);
            }
        }
        Object obj = c14371.result;
        B4.a aVar4 = B4.a.f492a;
        int i7 = c14371.label;
        Throwable th = null;
        int i8 = 1;
        try {
            if (i7 == 0) {
                AbstractC2467b.l(obj);
                str2 = "Query for type " + str + " already in progress, hooking into existing operation";
                a aVar5 = this.operationsMutex;
                c14371.L$0 = this;
                c14371.L$1 = str;
                c14371.L$2 = str2;
                c14371.L$3 = aVar5;
                c14371.I$0 = 1;
                c14371.label = 1;
                e5.c cVar = (e5.c) aVar5;
                if (cVar.e(c14371) != aVar4) {
                    str3 = str;
                    purchaseHistoryManager = this;
                    aVar = cVar;
                }
                return aVar4;
            }
            if (i7 != 1) {
                if (i7 == 2) {
                    AbstractC2467b.l(obj);
                    return obj;
                }
                if (i7 == 3) {
                    a aVar6 = (a) c14371.L$3;
                    list = (List) c14371.L$2;
                    str4 = (String) c14371.L$1;
                    purchaseHistoryManager2 = (PurchaseHistoryManager) c14371.L$0;
                    AbstractC2467b.l(obj);
                    aVar2 = aVar6;
                    aVar3 = aVar2;
                    try {
                        purchaseHistoryManager2.queryDeferreds.remove(str4);
                        ((e5.c) aVar3).g(null);
                        return list;
                    } catch (Throwable th2) {
                        ((e5.c) aVar3).g(null);
                        throw th2;
                    }
                }
                if (i7 != 4) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                a aVar7 = (a) c14371.L$3;
                Throwable th3 = (Throwable) c14371.L$2;
                String str5 = (String) c14371.L$1;
                PurchaseHistoryManager purchaseHistoryManager3 = (PurchaseHistoryManager) c14371.L$0;
                AbstractC2467b.l(obj);
                a aVar8 = aVar7;
                try {
                    purchaseHistoryManager3.queryDeferreds.remove(str5);
                    ((e5.c) aVar8).g(null);
                    throw th3;
                } catch (Throwable th4) {
                    ((e5.c) aVar8).g(null);
                    throw th4;
                }
            }
            i8 = c14371.I$0;
            a aVar9 = (a) c14371.L$3;
            str2 = (String) c14371.L$2;
            str3 = (String) c14371.L$1;
            purchaseHistoryManager = (PurchaseHistoryManager) c14371.L$0;
            AbstractC2467b.l(obj);
            aVar = aVar9;
            j jVar = (InterfaceC0780s) purchaseHistoryManager.queryDeferreds.get(str3);
            if (jVar != null) {
                if (((u0) jVar).E()) {
                    LogLevel logLevel = LogLevel.DEBUG;
                    LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
                    if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                        currentLogHandler.d("[Purchases] - " + logLevel.name(), str2 + " (already completed)");
                    }
                } else {
                    LogLevel logLevel2 = LogLevel.DEBUG;
                    LogHandler currentLogHandler2 = LogWrapperKt.getCurrentLogHandler();
                    if (Config.INSTANCE.getLogLevel().compareTo(logLevel2) <= 0) {
                        currentLogHandler2.d("[Purchases] - " + logLevel2.name(), str2);
                    }
                }
                kVar = new k(jVar, Boolean.FALSE);
            } else {
                C0781t c0781tB = E.b();
                purchaseHistoryManager.queryDeferreds.put(str3, c0781tB);
                kVar = new k(c0781tB, Boolean.TRUE);
            }
            ((e5.c) aVar).g(null);
            j jVar2 = (InterfaceC0780s) kVar.f25469a;
            if (((Boolean) kVar.f25470b).booleanValue()) {
                try {
                    try {
                        ArrayList arrayList = new ArrayList();
                        int i9 = 0;
                        String str6 = null;
                        while (true) {
                            if (i9 < 50) {
                                PurchaseHistoryResult purchaseHistoryResultQueryPurchaseHistory = purchaseHistoryManager.queryPurchaseHistory(str3, str6);
                                if (!purchaseHistoryResultQueryPurchaseHistory.isSuccess()) {
                                    LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Error querying purchase history through AIDL: " + purchaseHistoryResultQueryPurchaseHistory.getResponseCodeString(), th);
                                    break;
                                }
                                arrayList.addAll(purchaseHistoryResultQueryPurchaseHistory.getRecords());
                                String continuationToken = purchaseHistoryResultQueryPurchaseHistory.getContinuationToken();
                                i9++;
                                LogLevel logLevel3 = LogLevel.DEBUG;
                                LogHandler currentLogHandler3 = LogWrapperKt.getCurrentLogHandler();
                                if (Config.INSTANCE.getLogLevel().compareTo(logLevel3) <= 0) {
                                    currentLogHandler3.d("[Purchases] - " + logLevel3.name(), "Retrieved " + purchaseHistoryResultQueryPurchaseHistory.getRecords().size() + " records from AIDL queryPurchaseHistory (page " + i9 + ')');
                                }
                                if (continuationToken == null || !E.u(c14371.getContext())) {
                                    break;
                                }
                                str6 = continuationToken;
                                th = null;
                            } else {
                                LogLevel logLevel4 = LogLevel.WARN;
                                LogHandler currentLogHandler4 = LogWrapperKt.getCurrentLogHandler();
                                if (Config.INSTANCE.getLogLevel().compareTo(logLevel4) <= 0) {
                                    currentLogHandler4.w("[Purchases] - " + logLevel4.name(), "Reached maximum pagination limit for AIDL purchase history (50 pages). Will stop querying further pages.");
                                }
                            }
                        }
                        ProductType productType = o.c(str3, "subs") ? ProductType.SUBS : ProductType.INAPP;
                        ArrayList arrayList2 = new ArrayList(AbstractC3253s.G(arrayList, 10));
                        Iterator it = arrayList.iterator();
                        while (it.hasNext()) {
                            arrayList2.add(((PurchaseHistoryRecord) it.next()).toStoreTransaction(productType));
                        }
                        ((C0781t) jVar2).G(arrayList2);
                        if (i8 == 0) {
                            return arrayList2;
                        }
                        a aVar10 = purchaseHistoryManager.operationsMutex;
                        c14371.L$0 = purchaseHistoryManager;
                        c14371.L$1 = str3;
                        c14371.L$2 = arrayList2;
                        c14371.L$3 = aVar10;
                        c14371.label = 3;
                        e5.c cVar2 = (e5.c) aVar10;
                        if (cVar2.e(c14371) != aVar4) {
                            list = arrayList2;
                            str4 = str3;
                            purchaseHistoryManager2 = purchaseHistoryManager;
                            aVar2 = cVar2;
                            aVar3 = aVar2;
                            purchaseHistoryManager2.queryDeferreds.remove(str4);
                            ((e5.c) aVar3).g(null);
                            return list;
                        }
                    } catch (Throwable th5) {
                        ((C0781t) jVar2).U(th5);
                        throw th5;
                    }
                } catch (CancellationException e6) {
                    ((u0) jVar2).cancel(null);
                    throw e6;
                }
            } else {
                c14371.L$0 = null;
                c14371.L$1 = null;
                c14371.L$2 = null;
                c14371.L$3 = null;
                c14371.label = 2;
                Object objM = ((C0781t) jVar2).m(c14371);
                if (objM != aVar4) {
                    return objM;
                }
            }
            return aVar4;
        } finally {
            ((e5.c) aVar).g(null);
        }
    }
}
