package or;

import com.google.firebase.perf.network.FirebasePerfOkHttpClient;
import java.io.IOException;
import java.util.Objects;
import kr.a0;
import uq.b0;
import uq.d0;
import uq.e;
import uq.e0;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes5.dex */
public final class n implements or.b {

    /* renamed from: p, reason: collision with root package name */
    private final s f31694p;

    /* renamed from: q, reason: collision with root package name */
    private final Object[] f31695q;

    /* renamed from: r, reason: collision with root package name */
    private final e.a f31696r;

    /* renamed from: s, reason: collision with root package name */
    private final f f31697s;

    /* renamed from: t, reason: collision with root package name */
    private volatile boolean f31698t;

    /* renamed from: u, reason: collision with root package name */
    private uq.e f31699u;

    /* renamed from: v, reason: collision with root package name */
    private Throwable f31700v;

    /* renamed from: w, reason: collision with root package name */
    private boolean f31701w;

    /* loaded from: classes5.dex */
    class a implements uq.f {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ d f31702p;

        a(d dVar) {
            this.f31702p = dVar;
        }

        private void a(Throwable th2) {
            try {
                this.f31702p.a(n.this, th2);
            } catch (Throwable th3) {
                y.s(th3);
                th3.printStackTrace();
            }
        }

        @Override // uq.f
        public void onFailure(uq.e eVar, IOException iOException) {
            a(iOException);
        }

        @Override // uq.f
        public void onResponse(uq.e eVar, d0 d0Var) {
            try {
                try {
                    this.f31702p.b(n.this, n.this.d(d0Var));
                } catch (Throwable th2) {
                    y.s(th2);
                    th2.printStackTrace();
                }
            } catch (Throwable th3) {
                y.s(th3);
                a(th3);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes5.dex */
    public static final class b extends e0 {

        /* renamed from: r, reason: collision with root package name */
        private final e0 f31704r;

        /* renamed from: s, reason: collision with root package name */
        private final kr.f f31705s;

        /* renamed from: t, reason: collision with root package name */
        IOException f31706t;

        /* loaded from: classes5.dex */
        class a extends kr.j {
            a(a0 a0Var) {
                super(a0Var);
            }

            @Override // kr.j, kr.a0
            public long J0(kr.d dVar, long j10) {
                try {
                    return super.J0(dVar, j10);
                } catch (IOException e10) {
                    b.this.f31706t = e10;
                    throw e10;
                }
            }
        }

        b(e0 e0Var) {
            this.f31704r = e0Var;
            this.f31705s = kr.o.b(new a(e0Var.h()));
        }

        @Override // uq.e0, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            this.f31704r.close();
        }

        @Override // uq.e0
        public long e() {
            return this.f31704r.e();
        }

        @Override // uq.e0
        public uq.x f() {
            return this.f31704r.f();
        }

        @Override // uq.e0
        public kr.f h() {
            return this.f31705s;
        }

        void m() {
            IOException iOException = this.f31706t;
            if (iOException != null) {
                throw iOException;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes5.dex */
    public static final class c extends e0 {

        /* renamed from: r, reason: collision with root package name */
        private final uq.x f31708r;

        /* renamed from: s, reason: collision with root package name */
        private final long f31709s;

        c(uq.x xVar, long j10) {
            this.f31708r = xVar;
            this.f31709s = j10;
        }

        @Override // uq.e0
        public long e() {
            return this.f31709s;
        }

        @Override // uq.e0
        public uq.x f() {
            return this.f31708r;
        }

        @Override // uq.e0
        public kr.f h() {
            throw new IllegalStateException("Cannot read raw response body of a converted body.");
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public n(s sVar, Object[] objArr, e.a aVar, f fVar) {
        this.f31694p = sVar;
        this.f31695q = objArr;
        this.f31696r = aVar;
        this.f31697s = fVar;
    }

    private uq.e b() {
        uq.e a10 = this.f31696r.a(this.f31694p.a(this.f31695q));
        if (a10 != null) {
            return a10;
        }
        throw new NullPointerException("Call.Factory returned null.");
    }

    private uq.e c() {
        uq.e eVar = this.f31699u;
        if (eVar != null) {
            return eVar;
        }
        Throwable th2 = this.f31700v;
        if (th2 != null) {
            if (th2 instanceof IOException) {
                throw ((IOException) th2);
            }
            if (th2 instanceof RuntimeException) {
                throw ((RuntimeException) th2);
            }
            throw ((Error) th2);
        }
        try {
            uq.e b10 = b();
            this.f31699u = b10;
            return b10;
        } catch (IOException | Error | RuntimeException e10) {
            y.s(e10);
            this.f31700v = e10;
            throw e10;
        }
    }

    @Override // or.b
    /* renamed from: a, reason: merged with bridge method [inline-methods] */
    public n clone() {
        return new n(this.f31694p, this.f31695q, this.f31696r, this.f31697s);
    }

    @Override // or.b
    public void cancel() {
        uq.e eVar;
        this.f31698t = true;
        synchronized (this) {
            eVar = this.f31699u;
        }
        if (eVar != null) {
            eVar.cancel();
        }
    }

    t d(d0 d0Var) {
        e0 b10 = d0Var.b();
        d0 c10 = d0Var.w().b(new c(b10.f(), b10.e())).c();
        int f10 = c10.f();
        if (f10 < 200 || f10 >= 300) {
            try {
                return t.c(y.a(b10), c10);
            } finally {
                b10.close();
            }
        }
        if (f10 == 204 || f10 == 205) {
            b10.close();
            return t.h(null, c10);
        }
        b bVar = new b(b10);
        try {
            return t.h(this.f31697s.convert(bVar), c10);
        } catch (RuntimeException e10) {
            bVar.m();
            throw e10;
        }
    }

    @Override // or.b
    public boolean e() {
        boolean z10 = true;
        if (this.f31698t) {
            return true;
        }
        synchronized (this) {
            uq.e eVar = this.f31699u;
            if (eVar == null || !eVar.e()) {
                z10 = false;
            }
        }
        return z10;
    }

    @Override // or.b
    public synchronized b0 g() {
        try {
        } catch (IOException e10) {
            throw new RuntimeException("Unable to create request.", e10);
        }
        return c().g();
    }

    @Override // or.b
    public void s0(d dVar) {
        uq.e eVar;
        Throwable th2;
        Objects.requireNonNull(dVar, "callback == null");
        synchronized (this) {
            if (this.f31701w) {
                throw new IllegalStateException("Already executed.");
            }
            this.f31701w = true;
            eVar = this.f31699u;
            th2 = this.f31700v;
            if (eVar == null && th2 == null) {
                try {
                    uq.e b10 = b();
                    this.f31699u = b10;
                    eVar = b10;
                } catch (Throwable th3) {
                    th2 = th3;
                    y.s(th2);
                    this.f31700v = th2;
                }
            }
        }
        if (th2 != null) {
            dVar.a(this, th2);
            return;
        }
        if (this.f31698t) {
            eVar.cancel();
        }
        FirebasePerfOkHttpClient.enqueue(eVar, new a(dVar));
    }
}
