package com.revenuecat.purchases.common;

import F1.r;
import N4.a;
import N4.d;
import P4.i;
import T4.b;
import android.os.Handler;
import android.os.Looper;
import androidx.media3.common.j;
import com.revenuecat.purchases.PurchasesError;
import com.revenuecat.purchases.common.networking.HTTPResult;
import com.revenuecat.purchases.common.networking.NullPointerReadingErrorStreamException;
import com.revenuecat.purchases.common.verification.SignatureVerificationException;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import org.json.JSONException;

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

    @Deprecated
    public static final double INTEGRATION_TEST_DELAY_PERCENTAGE = 0.01d;
    private final ExecutorService executorService;
    private final Handler mainHandler;
    private final boolean runningIntegrationTests;

    public static abstract class AsyncCall implements Runnable {
        public abstract HTTPResult call();

        public void onCompletion(HTTPResult result) {
            o.h(result, "result");
        }

        public void onError(PurchasesError error) {
            o.h(error, "error");
        }

        @Override // java.lang.Runnable
        public void run() {
            try {
                onCompletion(call());
            } catch (NullPointerReadingErrorStreamException e6) {
                PurchasesError purchasesError = ErrorsKt.toPurchasesError(e6);
                LogUtilsKt.errorLog(purchasesError);
                onError(purchasesError);
            } catch (SignatureVerificationException e7) {
                PurchasesError purchasesError2 = ErrorsKt.toPurchasesError(e7);
                LogUtilsKt.errorLog(purchasesError2);
                onError(purchasesError2);
            } catch (IOException e8) {
                PurchasesError purchasesError3 = ErrorsKt.toPurchasesError(e8);
                LogUtilsKt.errorLog(purchasesError3);
                onError(purchasesError3);
            } catch (SecurityException e9) {
                PurchasesError purchasesError4 = ErrorsKt.toPurchasesError(e9);
                LogUtilsKt.errorLog(purchasesError4);
                onError(purchasesError4);
            } catch (JSONException e10) {
                PurchasesError purchasesError5 = ErrorsKt.toPurchasesError(e10);
                LogUtilsKt.errorLog(purchasesError5);
                onError(purchasesError5);
            }
        }
    }

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

        private Companion() {
        }
    }

    public Dispatcher(ExecutorService executorService, Handler handler, boolean z6) {
        o.h(executorService, "executorService");
        this.executorService = executorService;
        this.mainHandler = handler;
        this.runningIntegrationTests = z6;
    }

    public static /* synthetic */ void enqueue$default(Dispatcher dispatcher, Runnable runnable, Delay delay, int i6, Object obj) {
        if (obj != null) {
            throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: enqueue");
        }
        if ((i6 & 2) != 0) {
            delay = Delay.NONE;
        }
        dispatcher.enqueue(runnable, delay);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void enqueue$lambda$3$lambda$2(Runnable runnable, Dispatcher dispatcher) {
        try {
            runnable.run();
        } catch (Throwable th) {
            LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Exception running command: " + th, null);
            Handler handler = dispatcher.mainHandler;
            if (handler != null) {
                handler.post(new r(th, 14));
            }
        }
    }

    public void close() {
        synchronized (this.executorService) {
            this.executorService.shutdownNow();
        }
    }

    public void enqueue(Runnable command, Delay delay) {
        o.h(command, "command");
        o.h(delay, "delay");
        synchronized (this.executorService) {
            try {
                if (!this.executorService.isShutdown()) {
                    j jVar = new j(13, command, this);
                    if (delay == Delay.NONE || !(this.executorService instanceof ScheduledExecutorService)) {
                        this.executorService.submit(jVar);
                    } else {
                        i iVar = new i(b.d(delay.m486getMinDelayUwyO8pc()), b.d(delay.m485getMaxDelayUwyO8pc()));
                        a aVar = d.f5273a;
                        try {
                            long jY = L4.a.Y(iVar);
                            if (this.runningIntegrationTests) {
                                jY = (long) (jY * 0.01d);
                            }
                            ((ScheduledExecutorService) this.executorService).schedule(jVar, jY, TimeUnit.MILLISECONDS);
                        } catch (IllegalArgumentException e6) {
                            throw new NoSuchElementException(e6.getMessage());
                        }
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public boolean isClosed() {
        boolean zIsShutdown;
        synchronized (this.executorService) {
            zIsShutdown = this.executorService.isShutdown();
        }
        return zIsShutdown;
    }

    public /* synthetic */ Dispatcher(ExecutorService executorService, Handler handler, boolean z6, int i6, h hVar) {
        this(executorService, (i6 & 2) != 0 ? new Handler(Looper.getMainLooper()) : handler, (i6 & 4) != 0 ? false : z6);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void enqueue$lambda$3$lambda$2$lambda$1(Throwable th) throws Throwable {
        throw th;
    }
}
