package okhttp3.internal.publicsuffix;

import B6.k;
import B6.r;
import B6.t;
import I1.a;
import P6.g;
import W6.b;
import W6.c;
import W6.h;
import W6.j;
import X6.e;
import com.google.android.gms.internal.measurement.C1;
import d3.D;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.IDN;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import s7.l;
import s7.n;
import x7.B;
import x7.q;

/* JADX INFO: loaded from: classes.dex */
public final class PublicSuffixDatabase {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final byte[] f16000e = {42};

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final List f16001f = C1.C("*");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final PublicSuffixDatabase f16002g = new PublicSuffixDatabase();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final AtomicBoolean f16003a = new AtomicBoolean(false);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final CountDownLatch f16004b = new CountDownLatch(1);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public byte[] f16005c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public byte[] f16006d;

    public static List c(String str) {
        List listC0 = e.c0(str, new char[]{'.'});
        if (!g.a(k.n0(listC0), "")) {
            return listC0;
        }
        int size = listC0.size() - 1;
        return k.v0(size >= 0 ? size : 0, listC0);
    }

    public final String a(String str) {
        String strM;
        String strM2;
        String strM3;
        List listC0;
        String unicode = IDN.toUnicode(str);
        g.d(unicode, "unicodeDomain");
        List listC = c(unicode);
        if (this.f16003a.get() || !this.f16003a.compareAndSet(false, true)) {
            try {
                this.f16004b.await();
            } catch (InterruptedException unused) {
                Thread.currentThread().interrupt();
            }
        } else {
            boolean z8 = false;
            while (true) {
                try {
                    try {
                        b();
                        break;
                    } catch (InterruptedIOException unused2) {
                        Thread.interrupted();
                        z8 = true;
                    } catch (IOException e8) {
                        n nVar = n.f16942a;
                        n.f16942a.getClass();
                        n.i("Failed to read public suffix list", 5, e8);
                        if (z8) {
                        }
                    }
                } finally {
                    if (z8) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
        if (this.f16005c == null) {
            throw new IllegalStateException("Unable to load publicsuffixes.gz resource from the classpath.");
        }
        int size = listC.size();
        byte[][] bArr = new byte[size][];
        for (int i = 0; i < size; i++) {
            String str2 = (String) listC.get(i);
            Charset charset = StandardCharsets.UTF_8;
            g.d(charset, "UTF_8");
            byte[] bytes = str2.getBytes(charset);
            g.d(bytes, "this as java.lang.String).getBytes(charset)");
            bArr[i] = bytes;
        }
        int i5 = 0;
        while (true) {
            if (i5 >= size) {
                strM = null;
                break;
            }
            byte[] bArr2 = this.f16005c;
            if (bArr2 == null) {
                g.h("publicSuffixListBytes");
                throw null;
            }
            strM = D.m(bArr2, bArr, i5);
            if (strM != null) {
                break;
            }
            i5++;
        }
        if (size > 1) {
            byte[][] bArr3 = (byte[][]) bArr.clone();
            int length = bArr3.length - 1;
            for (int i8 = 0; i8 < length; i8++) {
                bArr3[i8] = f16000e;
                byte[] bArr4 = this.f16005c;
                if (bArr4 == null) {
                    g.h("publicSuffixListBytes");
                    throw null;
                }
                strM2 = D.m(bArr4, bArr3, i8);
                if (strM2 != null) {
                    break;
                }
            }
            strM2 = null;
        } else {
            strM2 = null;
        }
        if (strM2 != null) {
            int i9 = size - 1;
            for (int i10 = 0; i10 < i9; i10++) {
                byte[] bArr5 = this.f16006d;
                if (bArr5 == null) {
                    g.h("publicSuffixExceptionListBytes");
                    throw null;
                }
                strM3 = D.m(bArr5, bArr, i10);
                if (strM3 != null) {
                    break;
                }
            }
            strM3 = null;
        } else {
            strM3 = null;
        }
        if (strM3 != null) {
            listC0 = e.c0("!".concat(strM3), new char[]{'.'});
        } else if (strM == null && strM2 == null) {
            listC0 = f16001f;
        } else {
            List listC02 = t.f1032q;
            List listC03 = strM != null ? e.c0(strM, new char[]{'.'}) : listC02;
            if (strM2 != null) {
                listC02 = e.c0(strM2, new char[]{'.'});
            }
            listC0 = listC03.size() > listC02.size() ? listC03 : listC02;
        }
        if (listC.size() == listC0.size() && ((String) listC0.get(0)).charAt(0) != '!') {
            return null;
        }
        int size2 = ((String) listC0.get(0)).charAt(0) == '!' ? listC.size() - listC0.size() : listC.size() - (listC0.size() + 1);
        h rVar = new r(0, c(str));
        if (size2 < 0) {
            throw new IllegalArgumentException(a.k(size2, "Requested element count ", " is less than zero.").toString());
        }
        if (size2 != 0) {
            rVar = rVar instanceof c ? ((c) rVar).a(size2) : new b(rVar, size2);
        }
        return j.I(rVar, ".");
    }

    public final void b() {
        try {
            InputStream resourceAsStream = PublicSuffixDatabase.class.getResourceAsStream("publicsuffixes.gz");
            if (resourceAsStream != null) {
                B bC = l.c(new q(l.z(resourceAsStream)));
                try {
                    long jM = bC.m();
                    bC.O(jM);
                    byte[] bArrV = bC.f18725r.V(jM);
                    long jM2 = bC.m();
                    bC.O(jM2);
                    byte[] bArrV2 = bC.f18725r.V(jM2);
                    bC.close();
                    synchronized (this) {
                        this.f16005c = bArrV;
                        this.f16006d = bArrV2;
                    }
                } finally {
                }
            }
        } finally {
            this.f16004b.countDown();
        }
    }
}
