package m4;

import a.AbstractC0872a;
import com.google.firebase.sessions.settings.RemoteSettings;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import k4.AbstractC2397j;
import k4.C2394g;
import k4.C2401n;
import k4.C2403p;
import k4.C2408v;
import k4.C2410x;
import k4.EnumC2404q;
import m1.AbstractC2464a;

/* JADX INFO: loaded from: classes3.dex */
public final class S0 extends k4.W implements k4.I {

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    public static final Logger f20758d0 = Logger.getLogger(S0.class.getName());
    public static final Pattern e0 = Pattern.compile("[a-zA-Z][a-zA-Z0-9+.-]*:/.*");
    public static final k4.r0 f0;
    public static final k4.r0 g0;
    public static final k4.r0 h0;

    /* JADX INFO: renamed from: i0, reason: collision with root package name */
    public static final Y0 f20759i0;

    /* JADX INFO: renamed from: j0, reason: collision with root package name */
    public static final D0 f20760j0;

    /* JADX INFO: renamed from: k0, reason: collision with root package name */
    public static final H f20761k0;

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public final HashSet f20762A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public LinkedHashSet f20763B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public final Object f20764C;

    /* JADX INFO: renamed from: D, reason: collision with root package name */
    public final HashSet f20765D;

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public final M f20766E;

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public final C0.J f20767F;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public final AtomicBoolean f20768G;

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    public boolean f20769H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    public boolean f20770I;

    /* JADX INFO: renamed from: J, reason: collision with root package name */
    public volatile boolean f20771J;

    /* JADX INFO: renamed from: K, reason: collision with root package name */
    public final CountDownLatch f20772K;

    /* JADX INFO: renamed from: L, reason: collision with root package name */
    public final i2 f20773L;

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    public final C0.J f20774M;

    /* JADX INFO: renamed from: N, reason: collision with root package name */
    public final C2530o f20775N;

    /* JADX INFO: renamed from: O, reason: collision with root package name */
    public final C2524m f20776O;

    /* JADX INFO: renamed from: P, reason: collision with root package name */
    public final k4.G f20777P;

    /* JADX INFO: renamed from: Q, reason: collision with root package name */
    public final P0 f20778Q;

    /* JADX INFO: renamed from: R, reason: collision with root package name */
    public Y0 f20779R;

    /* JADX INFO: renamed from: S, reason: collision with root package name */
    public boolean f20780S;

    /* JADX INFO: renamed from: T, reason: collision with root package name */
    public final boolean f20781T;

    /* JADX INFO: renamed from: U, reason: collision with root package name */
    public final C2500e f20782U;

    /* JADX INFO: renamed from: V, reason: collision with root package name */
    public final long f20783V;

    /* JADX INFO: renamed from: W, reason: collision with root package name */
    public final long f20784W;
    public final boolean X;
    public final C2401n Y;

    /* JADX INFO: renamed from: Z, reason: collision with root package name */
    public final C2522l0 f20785Z;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final k4.J f20786a;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    public final J3.v f20787a0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f20788b;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    public final A1 f20789b0;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final k4.n0 f20790c;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    public int f20791c0;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final k4.j0 f20792d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final k2 f20793e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final C2521l f20794f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Q0 f20795g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final Executor f20796h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final l2.c f20797i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final J0 f20798j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final J0 f20799k;
    public final i2 l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final k4.w0 f20800m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final C2410x f20801n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final C2403p f20802o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final C2498d0 f20803p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final long f20804q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final J3.v f20805r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final i2 f20806s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final P0 f20807t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final ArrayList f20808u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public U1 f20809v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public boolean f20810w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public K0 f20811x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public volatile k4.Q f20812y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public boolean f20813z;

    static {
        k4.r0 r0Var = k4.r0.f19946n;
        f0 = r0Var.g("Channel shutdownNow invoked");
        g0 = r0Var.g("Channel shutdown invoked");
        h0 = r0Var.g("Subchannel shutdown invoked");
        f20759i0 = new Y0(null, new HashMap(), new HashMap(), null, null, null);
        f20760j0 = new D0();
        f20761k0 = new H(1);
    }

    public S0(T0 t02, n4.g gVar, i2 i2Var, l2.c cVar, C2498d0 c2498d0, ArrayList arrayList) {
        int i6;
        i2 i2Var2 = i2.f21060b;
        k4.w0 w0Var = new k4.w0(new G0(this));
        this.f20800m = w0Var;
        J3.v vVar = new J3.v();
        vVar.f3543a = new ArrayList();
        vVar.f3544b = EnumC2404q.f19913d;
        this.f20805r = vVar;
        this.f20762A = new HashSet(16, 0.75f);
        this.f20764C = new Object();
        this.f20765D = new HashSet(1, 0.75f);
        this.f20767F = new C0.J(this);
        this.f20768G = new AtomicBoolean(false);
        this.f20772K = new CountDownLatch(1);
        this.f20791c0 = 1;
        this.f20779R = f20759i0;
        this.f20780S = false;
        this.f20782U = new C2500e(1);
        this.Y = C2408v.f19963d;
        l2.c cVar2 = new l2.c(this, 4);
        this.f20785Z = new C2522l0(this, 1);
        J3.v vVar2 = new J3.v();
        vVar2.f3543a = this;
        this.f20787a0 = vVar2;
        String str = t02.f20827f;
        A4.g.o(str, "target");
        this.f20788b = str;
        k4.J j4 = new k4.J("Channel", str, k4.J.f19806d.incrementAndGet());
        this.f20786a = j4;
        this.l = i2Var2;
        l2.c cVar3 = t02.f20822a;
        A4.g.o(cVar3, "executorPool");
        this.f20797i = cVar3;
        Executor executor = (Executor) f2.a((e2) cVar3.f20267b);
        A4.g.o(executor, "executor");
        this.f20796h = executor;
        l2.c cVar4 = t02.f20823b;
        A4.g.o(cVar4, "offloadExecutorPool");
        J0 j02 = new J0(cVar4);
        this.f20799k = j02;
        C2521l c2521l = new C2521l(gVar, j02);
        this.f20794f = c2521l;
        Q0 q02 = new Q0(gVar.f21653d);
        this.f20795g = q02;
        C2530o c2530o = new C2530o(j4, i2Var2.e(), Z0.o.o("Channel for '", str, "'"));
        this.f20775N = c2530o;
        C2524m c2524m = new C2524m(c2530o, i2Var2);
        this.f20776O = c2524m;
        C2549u1 c2549u1 = AbstractC2504f0.f21006m;
        boolean z6 = t02.f20835o;
        this.X = z6;
        k2 k2Var = new k2(t02.f20828g);
        this.f20793e = k2Var;
        k4.n0 n0Var = t02.f20825d;
        this.f20790c = n0Var;
        V1 v12 = new V1(z6, t02.f20832k, t02.l, k2Var);
        n4.h hVar = t02.f20844x.f21649a;
        int iC = AbstractC2464a.c(hVar.f21672g);
        if (iC == 0) {
            i6 = 443;
        } else {
            if (iC != 1) {
                throw new AssertionError(com.google.android.recaptcha.internal.a.z(hVar.f21672g).concat(" not handled"));
            }
            i6 = 80;
        }
        Integer numValueOf = Integer.valueOf(i6);
        c2549u1.getClass();
        k4.j0 j0Var = new k4.j0(numValueOf, c2549u1, w0Var, v12, q02, c2524m, j02);
        this.f20792d = j0Var;
        c2521l.f21089a.getClass();
        this.f20809v = D(str, n0Var, j0Var, Collections.singleton(InetSocketAddress.class));
        this.f20798j = new J0(cVar);
        M m6 = new M(executor, w0Var);
        this.f20766E = m6;
        m6.f(cVar2);
        this.f20806s = i2Var;
        boolean z7 = t02.f20837q;
        this.f20781T = z7;
        P0 p02 = new P0(this, this.f20809v.k());
        this.f20778Q = p02;
        Iterator it = arrayList.iterator();
        if (it.hasNext()) {
            if (it.next() != null) {
                throw new ClassCastException();
            }
            A4.g.o(null, "interceptor");
            throw null;
        }
        this.f20807t = p02;
        this.f20808u = new ArrayList(t02.f20826e);
        A4.g.o(c2498d0, "stopwatchSupplier");
        this.f20803p = c2498d0;
        long j6 = t02.f20831j;
        if (j6 == -1) {
            this.f20804q = j6;
        } else {
            A4.g.i(j6, j6 >= T0.f20815A, "invalid idleTimeoutMillis %s");
            this.f20804q = t02.f20831j;
        }
        this.f20789b0 = new A1(new E0(this, 5), w0Var, gVar.f21653d, new E3.p(0));
        C2410x c2410x = t02.f20829h;
        A4.g.o(c2410x, "decompressorRegistry");
        this.f20801n = c2410x;
        C2403p c2403p = t02.f20830i;
        A4.g.o(c2403p, "compressorRegistry");
        this.f20802o = c2403p;
        this.f20784W = t02.f20833m;
        this.f20783V = t02.f20834n;
        this.f20773L = new i2(13);
        this.f20774M = new C0.J(13);
        k4.G g6 = t02.f20836p;
        g6.getClass();
        this.f20777P = g6;
        if (z7) {
            return;
        }
        this.f20780S = true;
    }

    public static void A(S0 s02) {
        if (!s02.f20771J && s02.f20768G.get() && s02.f20762A.isEmpty() && s02.f20765D.isEmpty()) {
            s02.f20776O.f(2, "Terminated");
            l2.c cVar = s02.f20797i;
            f2.b((e2) cVar.f20267b, s02.f20796h);
            J0 j02 = s02.f20798j;
            synchronized (j02) {
                Executor executor = j02.f20654b;
                if (executor != null) {
                    f2.b((e2) j02.f20653a.f20267b, executor);
                    j02.f20654b = null;
                }
            }
            J0 j03 = s02.f20799k;
            synchronized (j03) {
                Executor executor2 = j03.f20654b;
                if (executor2 != null) {
                    f2.b((e2) j03.f20653a.f20267b, executor2);
                    j03.f20654b = null;
                }
            }
            s02.f20794f.close();
            s02.f20771J = true;
            s02.f20772K.countDown();
        }
    }

    public static U1 D(String str, k4.n0 n0Var, k4.j0 j0Var, Collection collection) {
        URI uri;
        k4.m0 m0Var;
        k4.j0 j0Var2;
        String str2;
        String str3;
        String str4;
        F3.T t6;
        F3.T t7;
        StringBuilder sb = new StringBuilder();
        U u6 = null;
        try {
            uri = new URI(str);
        } catch (URISyntaxException e6) {
            sb.append(e6.getMessage());
            uri = null;
        }
        if (uri == null) {
            m0Var = null;
        } else {
            String scheme = uri.getScheme();
            if (scheme == null) {
                n0Var.getClass();
                m0Var = null;
            } else {
                synchronized (n0Var) {
                    t7 = n0Var.f19904c;
                }
                m0Var = (k4.m0) ((F3.u0) t7).get(scheme.toLowerCase(Locale.US));
            }
        }
        if (m0Var == null && !e0.matcher(str).matches()) {
            try {
                synchronized (n0Var) {
                    str4 = n0Var.f19902a;
                }
                uri = new URI(str4, "", RemoteSettings.FORWARD_SLASH_STRING + str, null);
                String scheme2 = uri.getScheme();
                if (scheme2 == null) {
                    m0Var = null;
                } else {
                    synchronized (n0Var) {
                        t6 = n0Var.f19904c;
                    }
                    m0Var = (k4.m0) ((F3.u0) t6).get(scheme2.toLowerCase(Locale.US));
                }
            } catch (URISyntaxException e7) {
                throw new IllegalArgumentException(e7);
            }
        }
        if (m0Var == null) {
            if (sb.length() > 0) {
                str3 = " (" + ((Object) sb) + ")";
            } else {
                str3 = "";
            }
            throw new IllegalArgumentException(Z0.o.o("Could not find a NameResolverProvider for ", str, str3));
        }
        if (collection != null && !collection.containsAll(Collections.singleton(InetSocketAddress.class))) {
            throw new IllegalArgumentException(Z0.o.p("Address types of NameResolver '", uri.getScheme(), "' for '", str, "' not supported by transport"));
        }
        if ("dns".equals(uri.getScheme())) {
            String path = uri.getPath();
            A4.g.o(path, "targetPath");
            if (!path.startsWith(RemoteSettings.FORWARD_SLASH_STRING)) {
                throw new IllegalArgumentException(L4.a.X("the path component (%s) of the target (%s) must start with '/'", path, uri));
            }
            String strSubstring = path.substring(1);
            uri.getAuthority();
            j0Var2 = j0Var;
            u6 = new U(strSubstring, j0Var2, AbstractC2504f0.f21009p, new E3.p(0), V.f20881a);
        } else {
            j0Var2 = j0Var;
        }
        if (u6 != null) {
            i2 i2Var = new i2(7);
            Q0 q02 = j0Var2.f19888e;
            if (q02 == null) {
                throw new IllegalStateException("ScheduledExecutorService not set in Builder");
            }
            k4.w0 w0Var = j0Var2.f19886c;
            return new U1(u6, new C2512i(i2Var, q02, w0Var), w0Var);
        }
        if (sb.length() > 0) {
            str2 = " (" + ((Object) sb) + ")";
        } else {
            str2 = "";
        }
        throw new IllegalArgumentException(Z0.o.o("cannot create a NameResolver for ", str, str2));
    }

    public static void y(S0 s02) {
        s02.G(true);
        M m6 = s02.f20766E;
        m6.i(null);
        s02.f20776O.f(2, "Entering IDLE state");
        s02.f20805r.c(EnumC2404q.f19913d);
        Object[] objArr = {s02.f20764C, m6};
        C2522l0 c2522l0 = s02.f20785Z;
        c2522l0.getClass();
        for (int i6 = 0; i6 < 2; i6++) {
            if (((Set) c2522l0.f1203a).contains(objArr[i6])) {
                s02.C();
                return;
            }
        }
    }

    public static void z(S0 s02) {
        if (s02.f20769H) {
            for (C2548u0 c2548u0 : s02.f20762A) {
                c2548u0.getClass();
                k4.r0 r0Var = f0;
                RunnableC2528n0 runnableC2528n0 = new RunnableC2528n0(c2548u0, r0Var, 0);
                k4.w0 w0Var = c2548u0.f21182k;
                w0Var.execute(runnableC2528n0);
                w0Var.execute(new RunnableC2528n0(c2548u0, r0Var, 1));
            }
            Iterator it = s02.f20765D.iterator();
            if (it.hasNext()) {
                it.next().getClass();
                throw new ClassCastException();
            }
        }
    }

    public final void B(boolean z6) {
        ScheduledFuture scheduledFuture;
        A1 a12 = this.f20789b0;
        a12.f20571f = false;
        if (!z6 || (scheduledFuture = a12.f20572g) == null) {
            return;
        }
        scheduledFuture.cancel(false);
        a12.f20572g = null;
    }

    public final void C() {
        this.f20800m.d();
        if (this.f20768G.get() || this.f20813z) {
            return;
        }
        if (((Set) this.f20785Z.f1203a).isEmpty()) {
            E();
        } else {
            B(false);
        }
        if (this.f20811x != null) {
            return;
        }
        this.f20776O.f(2, "Exiting idle mode");
        K0 k02 = new K0(this);
        k2 k2Var = this.f20793e;
        k2Var.getClass();
        k02.f20657d = new C0.J(k2Var, k02);
        this.f20811x = k02;
        this.f20809v.p(new L0(this, k02, this.f20809v));
        this.f20810w = true;
    }

    public final void E() {
        long j4 = this.f20804q;
        if (j4 == -1) {
            return;
        }
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        A1 a12 = this.f20789b0;
        a12.getClass();
        long nanos = timeUnit.toNanos(j4);
        TimeUnit timeUnit2 = TimeUnit.NANOSECONDS;
        long jB = a12.f20569d.b() + nanos;
        a12.f20571f = true;
        if (jB - a12.f20570e < 0 || a12.f20572g == null) {
            ScheduledFuture scheduledFuture = a12.f20572g;
            if (scheduledFuture != null) {
                scheduledFuture.cancel(false);
            }
            a12.f20572g = a12.f20566a.schedule(new RunnableC2564z1(a12, 1), nanos, timeUnit2);
        }
        a12.f20570e = jB;
    }

    public final void F() {
        this.f20776O.f(1, "shutdown() called");
        if (this.f20768G.compareAndSet(false, true)) {
            E0 e02 = new E0(this, 3);
            k4.w0 w0Var = this.f20800m;
            w0Var.execute(e02);
            P0 p02 = this.f20778Q;
            p02.f20721d.f20800m.execute(new N0(p02, 0));
            w0Var.execute(new E0(this, 0));
        }
    }

    public final void G(boolean z6) {
        this.f20800m.d();
        if (z6) {
            A4.g.t(this.f20810w, "nameResolver is not started");
            A4.g.t(this.f20811x != null, "lbHelper is null");
        }
        U1 u12 = this.f20809v;
        if (u12 != null) {
            u12.o();
            this.f20810w = false;
            if (z6) {
                String str = this.f20788b;
                k4.n0 n0Var = this.f20790c;
                k4.j0 j0Var = this.f20792d;
                this.f20794f.f21089a.getClass();
                this.f20809v = D(str, n0Var, j0Var, Collections.singleton(InetSocketAddress.class));
            } else {
                this.f20809v = null;
            }
        }
        K0 k02 = this.f20811x;
        if (k02 != null) {
            C0.J j4 = k02.f20657d;
            ((k4.T) j4.f534c).f();
            j4.f534c = null;
            this.f20811x = null;
        }
        this.f20812y = null;
    }

    @Override // k4.I
    public final k4.J e() {
        return this.f20786a;
    }

    @Override // k4.AbstractC2384C
    public final AbstractC2397j l(k4.i0 i0Var, C2394g c2394g) {
        return this.f20807t.l(i0Var, c2394g);
    }

    @Override // k4.W
    public final boolean s(long j4) {
        return this.f20772K.await(j4, TimeUnit.SECONDS);
    }

    @Override // k4.W
    public final void t() {
        this.f20800m.execute(new E0(this, 1));
    }

    public final String toString() {
        E3.j jVarI0 = AbstractC0872a.i0(this);
        jVarI0.b("logId", this.f20786a.f19809c);
        jVarI0.a(this.f20788b, "target");
        return jVarI0.toString();
    }

    @Override // k4.W
    public final EnumC2404q u() {
        EnumC2404q enumC2404q = (EnumC2404q) this.f20805r.f3544b;
        if (enumC2404q == null) {
            throw new UnsupportedOperationException("Channel state API is not implemented");
        }
        if (enumC2404q == EnumC2404q.f19913d) {
            this.f20800m.execute(new E0(this, 2));
        }
        return enumC2404q;
    }

    @Override // k4.W
    public final void v(EnumC2404q enumC2404q, com.google.firebase.firestore.remote.g gVar) {
        this.f20800m.execute(new C1.o(this, gVar, enumC2404q, 8));
    }

    @Override // k4.W
    public final /* bridge */ /* synthetic */ k4.W w() {
        F();
        return this;
    }

    @Override // k4.W
    public final k4.W x() {
        this.f20776O.f(1, "shutdownNow() called");
        F();
        P0 p02 = this.f20778Q;
        p02.f20721d.f20800m.execute(new N0(p02, 1));
        this.f20800m.execute(new E0(this, 4));
        return this;
    }
}
