package zq;

import java.lang.ref.Reference;
import java.net.Socket;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.t;
import rm.n0;
import zq.h;

/* loaded from: classes3.dex */
public final class j {

    /* renamed from: f, reason: collision with root package name */
    public static final a f53697f = new a(null);

    /* renamed from: a, reason: collision with root package name */
    private final int f53698a;

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

    /* renamed from: c, reason: collision with root package name */
    private final yq.c f53700c;

    /* renamed from: d, reason: collision with root package name */
    private final b f53701d;

    /* renamed from: e, reason: collision with root package name */
    private final ConcurrentLinkedQueue f53702e;

    /* loaded from: classes3.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(kotlin.jvm.internal.k kVar) {
            this();
        }
    }

    /* loaded from: classes3.dex */
    public static final class b extends yq.a {
        b(String str) {
            super(str, false, 2, null);
        }

        @Override // yq.a
        public long f() {
            return j.this.b(System.nanoTime());
        }
    }

    public j(yq.d taskRunner, int i10, long j10, TimeUnit timeUnit) {
        t.g(taskRunner, "taskRunner");
        t.g(timeUnit, "timeUnit");
        this.f53698a = i10;
        this.f53699b = timeUnit.toNanos(j10);
        this.f53700c = taskRunner.i();
        this.f53701d = new b(vq.p.f40483f + " ConnectionPool");
        this.f53702e = new ConcurrentLinkedQueue();
        if (j10 > 0) {
            return;
        }
        throw new IllegalArgumentException(("keepAliveDuration <= 0: " + j10).toString());
    }

    private final int d(i iVar, long j10) {
        if (vq.p.f40482e && !Thread.holdsLock(iVar)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + iVar);
        }
        List i10 = iVar.i();
        int i11 = 0;
        while (i11 < i10.size()) {
            Reference reference = (Reference) i10.get(i11);
            if (reference.get() != null) {
                i11++;
            } else {
                t.e(reference, "null cannot be cast to non-null type okhttp3.internal.connection.RealCall.CallReference");
                dr.h.f16598a.g().l("A connection to " + iVar.u().a().l() + " was leaked. Did you forget to close a response body?", ((h.b) reference).a());
                i10.remove(i11);
                iVar.x(true);
                if (i10.isEmpty()) {
                    iVar.w(j10 - this.f53699b);
                    return 0;
                }
            }
        }
        return i10.size();
    }

    public final i a(boolean z10, uq.a address, h call, List list, boolean z11) {
        boolean z12;
        Socket z13;
        t.g(address, "address");
        t.g(call, "call");
        Iterator it = this.f53702e.iterator();
        while (it.hasNext()) {
            i connection = (i) it.next();
            t.f(connection, "connection");
            synchronized (connection) {
                z12 = false;
                if (z11) {
                    if (!connection.q()) {
                    }
                }
                if (connection.o(address, list)) {
                    call.c(connection);
                    z12 = true;
                }
            }
            if (z12) {
                if (connection.p(z10)) {
                    return connection;
                }
                synchronized (connection) {
                    connection.x(true);
                    z13 = call.z();
                }
                if (z13 != null) {
                    vq.p.g(z13);
                }
            }
        }
        return null;
    }

    public final long b(long j10) {
        Iterator it = this.f53702e.iterator();
        int i10 = 0;
        long j11 = Long.MIN_VALUE;
        i iVar = null;
        int i11 = 0;
        while (it.hasNext()) {
            i connection = (i) it.next();
            t.f(connection, "connection");
            synchronized (connection) {
                if (d(connection, j10) > 0) {
                    i11++;
                } else {
                    i10++;
                    long j12 = j10 - connection.j();
                    if (j12 > j11) {
                        iVar = connection;
                        j11 = j12;
                    }
                    n0 n0Var = n0.f34904a;
                }
            }
        }
        long j13 = this.f53699b;
        if (j11 < j13 && i10 <= this.f53698a) {
            if (i10 > 0) {
                return j13 - j11;
            }
            if (i11 > 0) {
                return j13;
            }
            return -1L;
        }
        t.d(iVar);
        synchronized (iVar) {
            if (!iVar.i().isEmpty()) {
                return 0L;
            }
            if (iVar.j() + j11 != j10) {
                return 0L;
            }
            iVar.x(true);
            this.f53702e.remove(iVar);
            vq.p.g(iVar.y());
            if (this.f53702e.isEmpty()) {
                this.f53700c.a();
            }
            return 0L;
        }
    }

    public final boolean c(i connection) {
        t.g(connection, "connection");
        if (vq.p.f40482e && !Thread.holdsLock(connection)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + connection);
        }
        if (!connection.k() && this.f53698a != 0) {
            yq.c.m(this.f53700c, this.f53701d, 0L, 2, null);
            return false;
        }
        connection.x(true);
        this.f53702e.remove(connection);
        if (this.f53702e.isEmpty()) {
            this.f53700c.a();
        }
        return true;
    }

    public final void e(i connection) {
        t.g(connection, "connection");
        if (!vq.p.f40482e || Thread.holdsLock(connection)) {
            this.f53702e.add(connection);
            yq.c.m(this.f53700c, this.f53701d, 0L, 2, null);
            return;
        }
        throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + connection);
    }
}
