package okhttp3.internal.publicsuffix;

import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.IDN;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import o.AbstractC0126Ew;
import o.AbstractC0940cn0;
import o.AbstractC1904ms0;
import o.AbstractC3117zi;
import o.BP;
import o.C0123Et;
import o.C0164Gi;
import o.C0332Mv;
import o.C1822m00;
import o.C2794wE;
import o.F3;
import o.F60;
import o.InterfaceC0149Ft;
import o.ZW;
import o.Za0;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class PublicSuffixDatabase {
    public static final byte[] e = {42};
    public static final List f = Collections.singletonList("*");
    public static final PublicSuffixDatabase g = new PublicSuffixDatabase();
    public final AtomicBoolean a = new AtomicBoolean(false);
    public final CountDownLatch b = new CountDownLatch(1);
    public byte[] c;
    public byte[] d;

    public static List c(String str) {
        List listK0 = Za0.k0(str, new char[]{'.'});
        if (!F3.c(AbstractC3117zi.j0(listK0), "")) {
            return listK0;
        }
        int size = listK0.size() - 1;
        return AbstractC3117zi.q0(size >= 0 ? size : 0, listK0);
    }

    public final String a(String str) throws InterruptedException {
        String strH;
        String strH2;
        String strH3;
        int size;
        int size2;
        List listC = c(IDN.toUnicode(str));
        List listK0 = C0332Mv.l;
        int i = 0;
        if (this.a.get() || !this.a.compareAndSet(false, true)) {
            try {
                this.b.await();
            } catch (InterruptedException unused) {
                Thread.currentThread().interrupt();
            }
        } else {
            boolean z = false;
            while (true) {
                try {
                    try {
                        b();
                        break;
                    } catch (InterruptedIOException unused2) {
                        Thread.interrupted();
                        z = true;
                    } catch (IOException e2) {
                        ZW zw = ZW.a;
                        ZW.a.getClass();
                        ZW.i("Failed to read public suffix list", 5, e2);
                        if (z) {
                        }
                    }
                } finally {
                    if (z) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
        if (this.c == null) {
            throw new IllegalStateException("Unable to load publicsuffixes.gz resource from the classpath.");
        }
        int size3 = listC.size();
        byte[][] bArr = new byte[size3][];
        for (int i2 = 0; i2 < size3; i2++) {
            bArr[i2] = ((String) listC.get(i2)).getBytes(StandardCharsets.UTF_8);
        }
        int i3 = 0;
        while (true) {
            if (i3 >= size3) {
                strH = null;
                break;
            }
            byte[] bArr2 = this.c;
            if (bArr2 == null) {
                bArr2 = null;
            }
            strH = BP.h(bArr2, bArr, i3);
            if (strH != null) {
                break;
            }
            i3++;
        }
        if (size3 > 1) {
            byte[][] bArr3 = (byte[][]) bArr.clone();
            int length = bArr3.length - 1;
            for (int i4 = 0; i4 < length; i4++) {
                bArr3[i4] = e;
                byte[] bArr4 = this.c;
                if (bArr4 == null) {
                    bArr4 = null;
                }
                strH2 = BP.h(bArr4, bArr3, i4);
                if (strH2 != null) {
                    break;
                }
            }
            strH2 = null;
        } else {
            strH2 = null;
        }
        if (strH2 != null) {
            int i5 = size3 - 1;
            for (int i6 = 0; i6 < i5; i6++) {
                byte[] bArr5 = this.d;
                if (bArr5 == null) {
                    bArr5 = null;
                }
                strH3 = BP.h(bArr5, bArr, i6);
                if (strH3 != null) {
                    break;
                }
            }
            strH3 = null;
        } else {
            strH3 = null;
        }
        if (strH3 != null) {
            listK0 = Za0.k0("!".concat(strH3), new char[]{'.'});
        } else if (strH == null && strH2 == null) {
            listK0 = f;
        } else {
            List listK02 = strH != null ? Za0.k0(strH, new char[]{'.'}) : listK0;
            if (strH2 != null) {
                listK0 = Za0.k0(strH2, new char[]{'.'});
            }
            if (listK02.size() > listK0.size()) {
                listK0 = listK02;
            }
        }
        if (listC.size() == listK0.size() && ((String) listK0.get(0)).charAt(0) != '!') {
            return null;
        }
        if (((String) listK0.get(0)).charAt(0) == '!') {
            size = listC.size();
            size2 = listK0.size();
        } else {
            size = listC.size();
            size2 = listK0.size() + 1;
        }
        int i7 = size - size2;
        F60 c0164Gi = new C0164Gi(0, c(str));
        if (i7 < 0) {
            throw new IllegalArgumentException(AbstractC0126Ew.k(i7, "Requested element count ", " is less than zero.").toString());
        }
        if (i7 != 0) {
            c0164Gi = c0164Gi instanceof InterfaceC0149Ft ? ((InterfaceC0149Ft) c0164Gi).a(i7) : new C0123Et(c0164Gi, i7);
        }
        StringBuilder sb = new StringBuilder();
        sb.append((CharSequence) "");
        for (Object obj : c0164Gi) {
            i++;
            if (i > 1) {
                sb.append((CharSequence) ".");
            }
            AbstractC1904ms0.b(sb, obj, null);
        }
        sb.append((CharSequence) "");
        return sb.toString();
    }

    public final void b() {
        try {
            InputStream resourceAsStream = PublicSuffixDatabase.class.getResourceAsStream("publicsuffixes.gz");
            if (resourceAsStream != null) {
                C1822m00 c1822m00 = new C1822m00(new C2794wE(AbstractC0940cn0.I(resourceAsStream)));
                try {
                    long j = c1822m00.readInt();
                    c1822m00.I(j);
                    byte[] bArrQ = c1822m00.m.q(j);
                    long j2 = c1822m00.readInt();
                    c1822m00.I(j2);
                    byte[] bArrQ2 = c1822m00.m.q(j2);
                    c1822m00.close();
                    synchronized (this) {
                        this.c = bArrQ;
                        this.d = bArrQ2;
                    }
                } finally {
                }
            }
        } finally {
            this.b.countDown();
        }
    }
}
