package okhttp3.internal.connection;

import java.net.UnknownServiceException;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.SSLSocket;
import kotlin.jvm.internal.o;
import okhttp3.CipherSuite;
import okhttp3.CipherSuite$Companion$ORDER_BY_NAME$1;
import okhttp3.ConnectionSpec;
import okhttp3.internal.Util;
import z4.C3296a;

/* JADX INFO: loaded from: classes3.dex */
public final class ConnectionSpecSelector {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final List f22286a;

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

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

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

    public ConnectionSpecSelector(List connectionSpecs) {
        o.h(connectionSpecs, "connectionSpecs");
        this.f22286a = connectionSpecs;
    }

    public final ConnectionSpec a(SSLSocket sSLSocket) throws UnknownServiceException {
        ConnectionSpec connectionSpec;
        int i6;
        boolean z6;
        String[] cipherSuitesIntersection;
        String[] tlsVersionsIntersection;
        int i7 = this.f22287b;
        List list = this.f22286a;
        int size = list.size();
        while (true) {
            if (i7 >= size) {
                connectionSpec = null;
                break;
            }
            connectionSpec = (ConnectionSpec) list.get(i7);
            if (connectionSpec.b(sSLSocket)) {
                this.f22287b = i7 + 1;
                break;
            }
            i7++;
        }
        if (connectionSpec == null) {
            StringBuilder sb = new StringBuilder("Unable to find acceptable protocols. isFallback=");
            sb.append(this.f22289d);
            sb.append(", modes=");
            sb.append(list);
            sb.append(", supported protocols=");
            String[] enabledProtocols = sSLSocket.getEnabledProtocols();
            o.e(enabledProtocols);
            String string = Arrays.toString(enabledProtocols);
            o.g(string, "toString(this)");
            sb.append(string);
            throw new UnknownServiceException(sb.toString());
        }
        int i8 = this.f22287b;
        int size2 = list.size();
        while (true) {
            i6 = 0;
            if (i8 >= size2) {
                z6 = false;
                break;
            }
            if (((ConnectionSpec) list.get(i8)).b(sSLSocket)) {
                z6 = true;
                break;
            }
            i8++;
        }
        this.f22288c = z6;
        boolean z7 = this.f22289d;
        String[] strArr = connectionSpec.f21980c;
        if (strArr != null) {
            String[] enabledCipherSuites = sSLSocket.getEnabledCipherSuites();
            o.g(enabledCipherSuites, "sslSocket.enabledCipherSuites");
            CipherSuite.f21956b.getClass();
            cipherSuitesIntersection = Util.o(enabledCipherSuites, strArr, CipherSuite.f21957c);
        } else {
            cipherSuitesIntersection = sSLSocket.getEnabledCipherSuites();
        }
        String[] strArr2 = connectionSpec.f21981d;
        if (strArr2 != null) {
            String[] enabledProtocols2 = sSLSocket.getEnabledProtocols();
            o.g(enabledProtocols2, "sslSocket.enabledProtocols");
            tlsVersionsIntersection = Util.o(enabledProtocols2, strArr2, C3296a.f26322b);
        } else {
            tlsVersionsIntersection = sSLSocket.getEnabledProtocols();
        }
        String[] supportedCipherSuites = sSLSocket.getSupportedCipherSuites();
        o.g(supportedCipherSuites, "supportedCipherSuites");
        CipherSuite.f21956b.getClass();
        CipherSuite$Companion$ORDER_BY_NAME$1 comparator = CipherSuite.f21957c;
        byte[] bArr = Util.f22179a;
        o.h(comparator, "comparator");
        int length = supportedCipherSuites.length;
        while (true) {
            if (i6 >= length) {
                i6 = -1;
                break;
            }
            if (comparator.compare(supportedCipherSuites[i6], "TLS_FALLBACK_SCSV") == 0) {
                break;
            }
            i6++;
        }
        if (z7 && i6 != -1) {
            o.g(cipherSuitesIntersection, "cipherSuitesIntersection");
            String str = supportedCipherSuites[i6];
            o.g(str, "supportedCipherSuites[indexOfFallbackScsv]");
            Object[] objArrCopyOf = Arrays.copyOf(cipherSuitesIntersection, cipherSuitesIntersection.length + 1);
            o.g(objArrCopyOf, "copyOf(this, newSize)");
            cipherSuitesIntersection = (String[]) objArrCopyOf;
            cipherSuitesIntersection[cipherSuitesIntersection.length - 1] = str;
        }
        ConnectionSpec.Builder builder = new ConnectionSpec.Builder();
        builder.f21982a = connectionSpec.f21978a;
        builder.f21983b = strArr;
        builder.f21984c = strArr2;
        builder.f21985d = connectionSpec.f21979b;
        o.g(cipherSuitesIntersection, "cipherSuitesIntersection");
        builder.b((String[]) Arrays.copyOf(cipherSuitesIntersection, cipherSuitesIntersection.length));
        o.g(tlsVersionsIntersection, "tlsVersionsIntersection");
        builder.d((String[]) Arrays.copyOf(tlsVersionsIntersection, tlsVersionsIntersection.length));
        ConnectionSpec connectionSpecA = builder.a();
        if (connectionSpecA.c() != null) {
            sSLSocket.setEnabledProtocols(connectionSpecA.f21981d);
        }
        if (connectionSpecA.a() != null) {
            sSLSocket.setEnabledCipherSuites(connectionSpecA.f21980c);
        }
        return connectionSpec;
    }
}
