package I2;

import J2.AbstractC0399a0;
import J2.AbstractC0403c0;
import J2.C0398a;
import J2.C0400b;
import J2.C0402c;
import J2.C0420l;
import J2.C0422m;
import J2.C0423n;
import J2.EnumC0404d;
import J2.InterfaceC0406e;
import J2.L0;
import a.AbstractC0872a;
import android.util.Log;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import k4.C2394g;

/* JADX INFO: renamed from: I2.i, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0325i {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f2623a = 0;

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

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

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

    public /* synthetic */ C0325i() {
    }

    public static int a(ArrayList arrayList, int i6, AbstractC0399a0 abstractC0399a0) {
        int i7 = 0;
        if (i6 < 0) {
            return 0;
        }
        Object obj = arrayList.get(i6);
        J2.Y y6 = abstractC0399a0.f3371b;
        if (obj != y6) {
            return -1;
        }
        Iterator it = y6.getChildren().iterator();
        while (it.hasNext()) {
            if (((AbstractC0403c0) it.next()) == abstractC0399a0) {
                return i7;
            }
            i7++;
        }
        return -1;
    }

    public static ArrayList c(C0402c c0402c) {
        ArrayList arrayList = new ArrayList();
        while (!c0402c.n()) {
            String strSubstring = null;
            if (!c0402c.n()) {
                int i6 = c0402c.f3282b;
                String str = (String) c0402c.f3284d;
                char cCharAt = str.charAt(i6);
                if ((cCharAt < 'A' || cCharAt > 'Z') && (cCharAt < 'a' || cCharAt > 'z')) {
                    c0402c.f3282b = i6;
                } else {
                    int iG = c0402c.g();
                    while (true) {
                        if ((iG < 65 || iG > 90) && (iG < 97 || iG > 122)) {
                            break;
                        }
                        iG = c0402c.g();
                    }
                    strSubstring = str.substring(i6, c0402c.f3282b);
                }
            }
            if (strSubstring == null) {
                break;
            }
            try {
                arrayList.add(EnumC0404d.valueOf(strSubstring));
            } catch (IllegalArgumentException unused) {
            }
            if (!c0402c.I()) {
                break;
            }
        }
        return arrayList;
    }

    public static boolean f(C0422m c0422m, int i6, ArrayList arrayList, int i7, AbstractC0399a0 abstractC0399a0) {
        C0423n c0423n = (C0423n) c0422m.f3405a.get(i6);
        if (!i(c0423n, abstractC0399a0)) {
            return false;
        }
        int i8 = c0423n.f3407a;
        if (i8 == 1) {
            if (i6 != 0) {
                while (i7 >= 0) {
                    if (!h(c0422m, i6 - 1, arrayList, i7)) {
                        i7--;
                    }
                }
                return false;
            }
            return true;
        }
        if (i8 == 2) {
            return h(c0422m, i6 - 1, arrayList, i7);
        }
        int iA = a(arrayList, i7, abstractC0399a0);
        if (iA <= 0) {
            return false;
        }
        return f(c0422m, i6 - 1, arrayList, i7, (AbstractC0399a0) abstractC0399a0.f3371b.getChildren().get(iA - 1));
    }

    public static boolean g(C0422m c0422m, AbstractC0399a0 abstractC0399a0) {
        ArrayList arrayList = new ArrayList();
        Object obj = abstractC0399a0.f3371b;
        while (true) {
            if (obj == null) {
                break;
            }
            arrayList.add(0, obj);
            obj = ((AbstractC0403c0) obj).f3371b;
        }
        int size = arrayList.size() - 1;
        ArrayList arrayList2 = c0422m.f3405a;
        if ((arrayList2 == null ? 0 : arrayList2.size()) == 1) {
            return i((C0423n) c0422m.f3405a.get(0), abstractC0399a0);
        }
        ArrayList arrayList3 = c0422m.f3405a;
        return f(c0422m, (arrayList3 != null ? arrayList3.size() : 0) - 1, arrayList, size, abstractC0399a0);
    }

    public static boolean h(C0422m c0422m, int i6, ArrayList arrayList, int i7) {
        C0423n c0423n = (C0423n) c0422m.f3405a.get(i6);
        AbstractC0399a0 abstractC0399a0 = (AbstractC0399a0) arrayList.get(i7);
        if (!i(c0423n, abstractC0399a0)) {
            return false;
        }
        int i8 = c0423n.f3407a;
        if (i8 == 1) {
            if (i6 != 0) {
                while (i7 > 0) {
                    i7--;
                    if (h(c0422m, i6 - 1, arrayList, i7)) {
                    }
                }
                return false;
            }
            return true;
        }
        if (i8 == 2) {
            return h(c0422m, i6 - 1, arrayList, i7 - 1);
        }
        int iA = a(arrayList, i7, abstractC0399a0);
        if (iA <= 0) {
            return false;
        }
        return f(c0422m, i6 - 1, arrayList, i7, (AbstractC0399a0) abstractC0399a0.f3371b.getChildren().get(iA - 1));
    }

    public static boolean i(C0423n c0423n, AbstractC0399a0 abstractC0399a0) {
        ArrayList arrayList;
        String str = c0423n.f3408b;
        if (str != null && !str.equals(abstractC0399a0.m().toLowerCase(Locale.US))) {
            return false;
        }
        ArrayList<C0400b> arrayList2 = c0423n.f3409c;
        if (arrayList2 != null) {
            for (C0400b c0400b : arrayList2) {
                String str2 = c0400b.f3363a;
                String str3 = c0400b.f3365c;
                if (str2.equals("id")) {
                    if (!str3.equals(abstractC0399a0.f3358c)) {
                        return false;
                    }
                } else if (!str2.equals("class") || (arrayList = abstractC0399a0.f3362g) == null || !arrayList.contains(str3)) {
                    return false;
                }
            }
        }
        ArrayList arrayList3 = c0423n.f3410d;
        if (arrayList3 == null) {
            return true;
        }
        Iterator it = arrayList3.iterator();
        while (it.hasNext()) {
            if (!((InterfaceC0406e) it.next()).a(abstractC0399a0)) {
                return false;
            }
        }
        return true;
    }

    public void b(C0334s c0334s, C0402c c0402c) throws C0398a {
        int iIntValue;
        int iK;
        String strM = c0402c.M();
        c0402c.J();
        if (strM == null) {
            throw new C0398a("Invalid '@' rule");
        }
        int i6 = 0;
        if (!this.f2625c && strM.equals("media")) {
            ArrayList<EnumC0404d> arrayListC = c(c0402c);
            if (!c0402c.k('{')) {
                throw new C0398a("Invalid @media rule: missing rule set");
            }
            c0402c.J();
            EnumC0404d enumC0404d = (EnumC0404d) this.f2626d;
            for (EnumC0404d enumC0404d2 : arrayListC) {
                if (enumC0404d2 == EnumC0404d.f3372a || enumC0404d2 == enumC0404d) {
                    this.f2625c = true;
                    c0334s.c(e(c0402c));
                    this.f2625c = false;
                    break;
                }
            }
            e(c0402c);
            if (!c0402c.n() && !c0402c.k('}')) {
                throw new C0398a("Invalid @media rule: expected '}' at end of rule set");
            }
        } else if (this.f2625c || !strM.equals("import")) {
            Log.w("CSSParser", "Ignoring @" + strM + " rule");
            while (!c0402c.n() && ((iIntValue = c0402c.y().intValue()) != 59 || i6 != 0)) {
                if (iIntValue != 123) {
                    if (iIntValue == 125 && i6 > 0 && i6 - 1 == 0) {
                        break;
                    }
                } else {
                    i6++;
                }
            }
        } else {
            String strL = null;
            if (!c0402c.n()) {
                int i7 = c0402c.f3282b;
                if (c0402c.l("url(")) {
                    c0402c.J();
                    String strL2 = c0402c.L();
                    if (strL2 == null) {
                        StringBuilder sb = new StringBuilder();
                        while (!c0402c.n()) {
                            int i8 = c0402c.f3282b;
                            String str = (String) c0402c.f3284d;
                            char cCharAt = str.charAt(i8);
                            if (cCharAt == '\'' || cCharAt == '\"' || cCharAt == '(' || cCharAt == ')' || J2.L.w(cCharAt) || Character.isISOControl((int) cCharAt)) {
                                break;
                            }
                            c0402c.f3282b++;
                            if (cCharAt == '\\') {
                                if (!c0402c.n()) {
                                    int i9 = c0402c.f3282b;
                                    c0402c.f3282b = i9 + 1;
                                    cCharAt = str.charAt(i9);
                                    if (cCharAt != '\n' && cCharAt != '\r' && cCharAt != '\f') {
                                        int iK2 = C0402c.K(cCharAt);
                                        if (iK2 != -1) {
                                            for (int i10 = 1; i10 <= 5 && !c0402c.n() && (iK = C0402c.K(str.charAt(c0402c.f3282b))) != -1; i10++) {
                                                c0402c.f3282b++;
                                                iK2 = (iK2 * 16) + iK;
                                            }
                                            sb.append((char) iK2);
                                        }
                                    }
                                }
                            }
                            sb.append(cCharAt);
                        }
                        strL2 = sb.length() == 0 ? null : sb.toString();
                    }
                    if (strL2 == null) {
                        c0402c.f3282b = i7;
                    } else {
                        c0402c.J();
                        if (c0402c.n() || c0402c.l(")")) {
                            strL = strL2;
                        } else {
                            c0402c.f3282b = i7;
                        }
                    }
                }
            }
            if (strL == null) {
                strL = c0402c.L();
            }
            if (strL == null) {
                throw new C0398a("Invalid @import rule: expected string or url()");
            }
            c0402c.J();
            c(c0402c);
            if (!c0402c.n() && !c0402c.k(';')) {
                throw new C0398a("Invalid @media rule: expected '}' at end of rule set");
            }
        }
        c0402c.J();
    }

    public boolean d(C0334s c0334s, C0402c c0402c) throws C0398a {
        ArrayList<C0422m> arrayListN = c0402c.N();
        if (arrayListN == null || arrayListN.isEmpty()) {
            return false;
        }
        if (!c0402c.k('{')) {
            throw new C0398a("Malformed rule block: expected '{'");
        }
        c0402c.J();
        J2.U u6 = new J2.U();
        do {
            String strM = c0402c.M();
            c0402c.J();
            if (!c0402c.k(':')) {
                throw new C0398a("Expected ':'");
            }
            c0402c.J();
            String strSubstring = null;
            if (!c0402c.n()) {
                int i6 = c0402c.f3282b;
                String str = (String) c0402c.f3284d;
                int iCharAt = str.charAt(i6);
                int i7 = i6;
                while (iCharAt != -1 && iCharAt != 59 && iCharAt != 125 && iCharAt != 33 && iCharAt != 10 && iCharAt != 13) {
                    if (!J2.L.w(iCharAt)) {
                        i7 = c0402c.f3282b + 1;
                    }
                    iCharAt = c0402c.g();
                }
                if (c0402c.f3282b > i6) {
                    strSubstring = str.substring(i6, i7);
                } else {
                    c0402c.f3282b = i6;
                }
            }
            if (strSubstring == null) {
                throw new C0398a("Expected property value");
            }
            c0402c.J();
            if (c0402c.k('!')) {
                c0402c.J();
                if (!c0402c.l("important")) {
                    throw new C0398a("Malformed rule set: found unexpected '!'");
                }
                c0402c.J();
            }
            c0402c.k(';');
            L0.D(u6, strM, strSubstring);
            c0402c.J();
            if (c0402c.n()) {
                break;
            }
        } while (!c0402c.k('}'));
        c0402c.J();
        for (C0422m c0422m : arrayListN) {
            C0420l c0420l = new C0420l();
            c0420l.f3401a = c0422m;
            c0420l.f3402b = u6;
            c0420l.f3403c = this.f2624b;
            c0334s.a(c0420l);
        }
        return true;
    }

    public C0334s e(C0402c c0402c) {
        C0334s c0334s = new C0334s(1, (byte) 0);
        while (!c0402c.n()) {
            try {
                if (!c0402c.l("<!--") && !c0402c.l("-->")) {
                    if (!c0402c.k('@')) {
                        if (!d(c0334s, c0402c)) {
                            break;
                        }
                    } else {
                        b(c0334s, c0402c);
                    }
                }
            } catch (C0398a e6) {
                Log.e("CSSParser", "CSS parser terminated early due to error: " + e6.getMessage());
            }
        }
        return c0334s;
    }

    public String toString() {
        switch (this.f2623a) {
            case 2:
                E3.j jVarI0 = AbstractC0872a.i0(this);
                jVarI0.a((C2394g) this.f2626d, "callOptions");
                jVarI0.d("previousAttempts", String.valueOf(this.f2624b));
                jVarI0.c("isTransparentRetry", this.f2625c);
                return jVarI0.toString();
            default:
                return super.toString();
        }
    }

    public C0325i(C2394g c2394g, int i6, boolean z6) {
        A4.g.o(c2394g, "callOptions");
        this.f2626d = c2394g;
        this.f2624b = i6;
        this.f2625c = z6;
    }

    public C0325i(int i6) {
        EnumC0404d enumC0404d = EnumC0404d.f3373b;
        this.f2625c = false;
        this.f2626d = enumC0404d;
        this.f2624b = i6;
    }
}
