package com.google.android.gms.internal.ads;

import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes2.dex */
public final class aw1 {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final aw1 f9301c;

    public aw1(ArrayList arrayList, j02 j02Var, aw1 aw1Var) {
        this.f9299a = arrayList;
        this.f9300b = j02Var;
        this.f9301c = aw1Var;
    }

    public static final aw1 a(b52 b52Var) throws GeneralSecurityException {
        rb h02Var;
        boolean z10;
        if (b52Var == null || b52Var.D() <= 0) {
            throw new GeneralSecurityException("empty keyset");
        }
        ArrayList arrayList = new ArrayList(b52Var.D());
        for (a52 a52Var : b52Var.C()) {
            int iD = a52Var.D();
            try {
                h12 h12VarG = g(a52Var);
                t02 t02Var = t02.f16664b;
                o12 o12Var = (o12) t02Var.f16665a.get();
                o12Var.getClass();
                rb h02Var2 = !o12Var.f14806b.containsKey(new m12(h12VarG.f11982b, h12.class)) ? new h02(h12VarG) : t02Var.e(h12VarG);
                z10 = false;
                h02Var = h02Var2;
            } catch (GeneralSecurityException e10) {
                if (((AtomicBoolean) lz1.f13915a.f12808b).get()) {
                    throw e10;
                }
                h02Var = new h02(g(a52Var));
                z10 = true;
            }
            if (((AtomicBoolean) lz1.f13915a.f12808b).get() && !h(a52Var.J())) {
                throw new GeneralSecurityException("Parsing of a single key failed (wrong status) and Tink is configured via validateKeysetsOnParsing to reject such keysets.");
            }
            arrayList.add(new yv1(h02Var, a52Var.J(), iD, iD == b52Var.B(), z10, xv1.f18594a));
        }
        return new aw1(Collections.unmodifiableList(arrayList), j02.f12676b);
    }

    public static final aw1 e(dw1 dw1Var) throws GeneralSecurityException {
        int i10;
        int i11;
        n5.s sVar = new n5.s(7);
        vv1 vv1Var = new vv1(dw1Var);
        w80 w80Var = w80.f17912d;
        vv1Var.f17784c = w80Var;
        char c6 = 1;
        vv1Var.f17782a = true;
        Iterator it = ((List) sVar.f30880b).iterator();
        while (true) {
            i10 = 0;
            if (!it.hasNext()) {
                break;
            }
            ((vv1) it.next()).f17782a = false;
        }
        ((List) sVar.f30880b).add(vv1Var);
        if (sVar.f30879a) {
            throw new GeneralSecurityException("KeysetHandle.Builder#build must only be called once");
        }
        sVar.f30879a = true;
        List<vv1> list = (List) sVar.f30880b;
        ArrayList<yv1> arrayList = new ArrayList(list.size());
        int i12 = 0;
        while (i12 < list.size() - 1) {
            int i13 = i12 + 1;
            if (((vv1) list.get(i12)).f17784c == w80Var && ((vv1) list.get(i13)).f17784c != w80Var) {
                throw new GeneralSecurityException("Entries with 'withRandomId()' may only be followed by other entries with 'withRandomId()'.");
            }
            i12 = i13;
        }
        HashSet hashSet = new HashSet();
        Integer num = null;
        for (vv1 vv1Var2 : list) {
            vv1Var2.getClass();
            w80 w80Var2 = vv1Var2.f17784c;
            if (w80Var2 == null) {
                throw new GeneralSecurityException("No ID was set (with withFixedId or withRandomId)");
            }
            int i14 = 3;
            if (w80Var2 == w80Var) {
                i11 = 0;
                while (true) {
                    if (i11 != 0 && !hashSet.contains(Integer.valueOf(i11))) {
                        break;
                    }
                    int i15 = q12.f15571a;
                    i11 = 0;
                    while (i11 == 0) {
                        byte[] bArrA = j12.a(4);
                        i11 = (bArrA[3] & 255) | ((bArrA[0] & 255) << 24) | ((bArrA[c6] & 255) << 16) | ((bArrA[2] & 255) << 8);
                    }
                }
            } else {
                i11 = 0;
            }
            Integer numValueOf = Integer.valueOf(i11);
            if (hashSet.contains(numValueOf)) {
                throw new GeneralSecurityException(com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.g(new StringBuilder(String.valueOf(i11).length() + 31), "Id ", i11, " is used twice in the keyset"));
            }
            hashSet.add(numValueOf);
            dw1 dw1Var2 = vv1Var2.f17783b;
            rb rbVarB = l02.f13453b.b(dw1Var2, c6 != dw1Var2.a() ? null : numValueOf);
            Object obj = uv1.f17399b;
            if (!obj.equals(obj)) {
                if (uv1.f17400c.equals(obj)) {
                    i14 = 4;
                } else {
                    if (!uv1.f17401d.equals(obj)) {
                        throw new IllegalStateException("Unknown key status");
                    }
                    i14 = 5;
                }
            }
            boolean z10 = vv1Var2.f17782a;
            yv1 yv1Var = new yv1(rbVarB, i14, i11, z10, false, xv1.f18594a);
            if (z10) {
                if (num != null) {
                    throw new GeneralSecurityException("Two primaries were set");
                }
                num = numValueOf;
            }
            arrayList.add(yv1Var);
            c6 = 1;
        }
        if (num == null) {
            throw new GeneralSecurityException("No primary was set");
        }
        j02 j02Var = (j02) sVar.f30881c;
        aw1 aw1Var = new aw1(arrayList, j02Var);
        if (j02Var.f12677a.isEmpty()) {
            return aw1Var;
        }
        zv1 zv1Var = new zv1(aw1Var, j02Var, i10);
        ArrayList arrayList2 = new ArrayList(arrayList.size());
        for (yv1 yv1Var2 : arrayList) {
            arrayList2.add(new yv1(yv1Var2.f18982a, yv1Var2.f18988g, yv1Var2.f18984c, yv1Var2.f18985d, yv1Var2.f18986e, zv1Var));
        }
        return new aw1(arrayList2, j02Var, aw1Var);
    }

    public static h12 g(a52 a52Var) {
        return h12.a(a52Var.C().B(), a52Var.C().C(), a52Var.C().D(), a52Var.E(), a52Var.E() == zzhqy.RAW ? null : Integer.valueOf(a52Var.D()));
    }

    public static boolean h(int i10) {
        int i11 = i10 - 2;
        return i11 == 1 || i11 == 2 || i11 == 3;
    }

    public final b52 b() {
        try {
            y42 y42VarH = b52.H();
            for (yv1 yv1Var : this.f9299a) {
                rb rbVarA = yv1Var.a();
                int i10 = yv1Var.f18988g;
                h12 h12Var = (h12) t02.f16664b.f(rbVarA);
                Integer numR = rbVarA.r();
                int i11 = yv1Var.f18984c;
                if (numR != null && numR.intValue() != i11) {
                    throw new GeneralSecurityException("Wrong ID set for key with ID requirement");
                }
                z42 z42VarF = a52.F();
                t42 t42VarE = u42.E();
                String str = h12Var.f11981a;
                t42VarE.c();
                ((u42) t42VarE.f8617b).G(str);
                zzian zzianVar = h12Var.f11983c;
                t42VarE.c();
                ((u42) t42VarE.f8617b).H(zzianVar);
                zzhqb zzhqbVar = h12Var.f11984d;
                t42VarE.c();
                ((u42) t42VarE.f8617b).I(zzhqbVar);
                z42VarF.c();
                ((a52) z42VarF.f8617b).G((u42) t42VarE.e());
                z42VarF.c();
                ((a52) z42VarF.f8617b).K(i10);
                z42VarF.c();
                ((a52) z42VarF.f8617b).H(i11);
                zzhqy zzhqyVar = h12Var.f11985e;
                z42VarF.c();
                ((a52) z42VarF.f8617b).I(zzhqyVar);
                a52 a52Var = (a52) z42VarF.e();
                y42VarH.c();
                ((b52) y42VarH.f8617b).J(a52Var);
                if (yv1Var.f18985d) {
                    y42VarH.c();
                    ((b52) y42VarH.f8617b).I(i11);
                }
            }
            return (b52) y42VarH.e();
        } catch (GeneralSecurityException e10) {
            throw new zzhlm(e10);
        }
    }

    public final yv1 c() {
        for (yv1 yv1Var : this.f9299a) {
            if (yv1Var != null && yv1Var.f18985d) {
                if (yv1Var.f18983b == uv1.f17399b) {
                    return yv1Var;
                }
                throw new IllegalStateException("Keyset has primary which isn't enabled");
            }
        }
        throw new IllegalStateException("Keyset has no valid primary");
    }

    public final yv1 d(int i10) {
        List list = this.f9299a;
        if (i10 < 0 || i10 >= list.size()) {
            int size = list.size();
            throw new IndexOutOfBoundsException(com.google.android.gms.measurement.internal.u1.m(new StringBuilder(String.valueOf(i10).length() + 34 + String.valueOf(size).length()), "Invalid index ", i10, " for keyset of size ", size));
        }
        yv1 yv1Var = (yv1) list.get(i10);
        if (!h(yv1Var.f18988g)) {
            throw new IllegalStateException(com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.g(new StringBuilder(String.valueOf(i10).length() + 42), "Keyset-Entry at position ", i10, " has wrong status"));
        }
        if (yv1Var.f18986e) {
            throw new IllegalStateException(com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.g(new StringBuilder(String.valueOf(i10).length() + 48), "Keyset-Entry at position ", i10, " didn't parse correctly"));
        }
        return (yv1) list.get(i10);
    }

    public final Object f(uz1 uz1Var, Class cls) throws GeneralSecurityException {
        if (!(uz1Var instanceof uz1)) {
            throw new GeneralSecurityException("Currently only subclasses of InternalConfiguration are accepted");
        }
        aw1 aw1Var = this.f9301c;
        b52 b52VarB = (aw1Var == null ? this : aw1Var).b();
        int i10 = gw1.f11901a;
        int iB = b52VarB.B();
        int i11 = 0;
        boolean z10 = true;
        int i12 = 0;
        boolean z11 = false;
        for (a52 a52Var : b52VarB.C()) {
            if (a52Var.J() == 3) {
                if (!a52Var.B()) {
                    throw new GeneralSecurityException(String.format("key %d has no key data", Integer.valueOf(a52Var.D())));
                }
                if (a52Var.E() == zzhqy.UNKNOWN_PREFIX) {
                    throw new GeneralSecurityException(String.format("key %d has unknown prefix", Integer.valueOf(a52Var.D())));
                }
                if (a52Var.J() == 2) {
                    throw new GeneralSecurityException(String.format("key %d has unknown status", Integer.valueOf(a52Var.D())));
                }
                if (a52Var.D() == iB) {
                    if (z11) {
                        throw new GeneralSecurityException("keyset contains multiple primary keys");
                    }
                    z11 = true;
                }
                z10 &= a52Var.C().D() == zzhqb.ASYMMETRIC_PUBLIC;
                i12++;
            }
        }
        if (i12 == 0) {
            throw new GeneralSecurityException("keyset must contain at least one ENABLED key");
        }
        if (!z11 && !z10) {
            throw new GeneralSecurityException("keyset doesn't contain a valid primary key");
        }
        while (true) {
            List list = this.f9299a;
            if (i11 >= list.size()) {
                if (aw1Var == null) {
                    aw1Var = this;
                }
                return uz1Var.a(aw1Var, this.f9300b, cls);
            }
            if (((yv1) list.get(i11)).f18986e || !h(((yv1) list.get(i11)).f18988g)) {
                break;
            }
            i11++;
        }
        String strB = b52VarB.E(i11).C().B();
        StringBuilder sb = new StringBuilder(String.valueOf(strB).length() + String.valueOf(i11).length() + 44 + 32);
        sb.append("Key parsing of key with index ");
        sb.append(i11);
        sb.append(" and type_url ");
        sb.append(strB);
        sb.append(" failed, unable to get primitive");
        throw new GeneralSecurityException(sb.toString());
    }

    public final String toString() {
        b52 b52VarB = b();
        int i10 = gw1.f11901a;
        c52 c52VarB = f52.B();
        int iB = b52VarB.B();
        c52VarB.c();
        ((f52) c52VarB.f8617b).C(iB);
        for (a52 a52Var : b52VarB.C()) {
            d52 d52VarB = e52.B();
            String strB = a52Var.C().B();
            d52VarB.c();
            ((e52) d52VarB.f8617b).C(strB);
            int iJ = a52Var.J();
            d52VarB.c();
            ((e52) d52VarB.f8617b).F(iJ);
            zzhqy zzhqyVarE = a52Var.E();
            d52VarB.c();
            ((e52) d52VarB.f8617b).E(zzhqyVarE);
            int iD = a52Var.D();
            d52VarB.c();
            ((e52) d52VarB.f8617b).D(iD);
            e52 e52Var = (e52) d52VarB.e();
            c52VarB.c();
            ((f52) c52VarB.f8617b).D(e52Var);
        }
        return ((f52) c52VarB.e()).toString();
    }

    public aw1(List list, j02 j02Var) throws GeneralSecurityException {
        this.f9299a = list;
        this.f9300b = j02Var;
        if (((AtomicBoolean) lz1.f13915a.f12808b).get()) {
            HashSet hashSet = new HashSet();
            Iterator it = list.iterator();
            boolean z10 = false;
            while (it.hasNext()) {
                yv1 yv1Var = (yv1) it.next();
                boolean zContains = hashSet.contains(Integer.valueOf(yv1Var.f18984c));
                int i10 = yv1Var.f18984c;
                if (zContains) {
                    throw new GeneralSecurityException(com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.g(new StringBuilder(String.valueOf(i10).length() + 121), "KeyID ", i10, " is duplicated in the keyset, and Tink is configured to reject such keysets with the flag validateKeysetsOnParsing."));
                }
                hashSet.add(Integer.valueOf(i10));
                z10 |= yv1Var.f18985d;
            }
            if (!z10) {
                throw new GeneralSecurityException("Primary key id not found in keyset, and Tink is configured to reject such keysets with the flag validateKeysetsOnParsing.");
            }
        }
        this.f9301c = null;
    }
}
