package com.google.android.material.shape;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.util.AttributeSet;
import android.util.StateSet;
import android.util.Xml;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

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

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int[][] f23112c = new int[10][];

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public d[] f23113d = new d[10];

    public static l0 b(Context context, TypedArray typedArray, int i10, a aVar) {
        int next;
        int resourceId = typedArray.getResourceId(i10, 0);
        if (resourceId == 0) {
            return c(s.e(typedArray, i10, aVar));
        }
        if (!context.getResources().getResourceTypeName(resourceId).equals("xml")) {
            return c(s.e(typedArray, i10, aVar));
        }
        try {
            XmlResourceParser xml = context.getResources().getXml(resourceId);
            try {
                l0 l0Var = new l0();
                AttributeSet attributeSetAsAttributeSet = Xml.asAttributeSet(xml);
                do {
                    next = xml.next();
                    if (next == 2) {
                        break;
                    }
                } while (next != 1);
                if (next != 2) {
                    throw new XmlPullParserException("No start tag found");
                }
                if (xml.getName().equals("selector")) {
                    l0Var.loadCornerSizesFromItems(context, xml, attributeSetAsAttributeSet, context.getTheme());
                }
                xml.close();
                return l0Var;
            } catch (Throwable th) {
                if (xml != null) {
                    try {
                        xml.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                }
                throw th;
            }
        } catch (Resources.NotFoundException | IOException | XmlPullParserException unused) {
            return c(aVar);
        }
    }

    public static l0 c(d dVar) {
        l0 l0Var = new l0();
        l0Var.a(StateSet.WILD_CARD, dVar);
        return l0Var;
    }

    private void loadCornerSizesFromItems(@NonNull Context context, @NonNull XmlPullParser xmlPullParser, @NonNull AttributeSet attributeSet, @Nullable Resources.Theme theme) throws XmlPullParserException, IOException {
        int depth = xmlPullParser.getDepth() + 1;
        while (true) {
            int next = xmlPullParser.next();
            if (next == 1) {
                return;
            }
            int depth2 = xmlPullParser.getDepth();
            if (depth2 < depth && next == 3) {
                return;
            }
            if (next == 2 && depth2 <= depth && xmlPullParser.getName().equals("item")) {
                TypedArray typedArrayObtainAttributes = theme == null ? context.getResources().obtainAttributes(attributeSet, x8.m.ShapeAppearance) : theme.obtainStyledAttributes(attributeSet, x8.m.ShapeAppearance, 0, 0);
                d dVarE = s.e(typedArrayObtainAttributes, x8.m.ShapeAppearance_cornerSize, new a(0.0f));
                typedArrayObtainAttributes.recycle();
                int attributeCount = attributeSet.getAttributeCount();
                int[] iArr = new int[attributeCount];
                int i10 = 0;
                for (int i11 = 0; i11 < attributeCount; i11++) {
                    int attributeNameResource = attributeSet.getAttributeNameResource(i11);
                    if (attributeNameResource != x8.c.cornerSize) {
                        int i12 = i10 + 1;
                        if (!attributeSet.getAttributeBooleanValue(i11, false)) {
                            attributeNameResource = -attributeNameResource;
                        }
                        iArr[i10] = attributeNameResource;
                        i10 = i12;
                    }
                }
                a(StateSet.trimStateSet(iArr, i10), dVarE);
            }
        }
    }

    public final void a(int[] iArr, d dVar) {
        int i10 = this.f23110a;
        if (i10 == 0 || iArr.length == 0) {
            this.f23111b = dVar;
        }
        int[][] iArr2 = this.f23112c;
        if (i10 >= iArr2.length) {
            int i11 = i10 + 10;
            int[][] iArr3 = new int[i11][];
            System.arraycopy(iArr2, 0, iArr3, 0, i10);
            this.f23112c = iArr3;
            d[] dVarArr = new d[i11];
            System.arraycopy(this.f23113d, 0, dVarArr, 0, i10);
            this.f23113d = dVarArr;
        }
        int[][] iArr4 = this.f23112c;
        int i12 = this.f23110a;
        iArr4[i12] = iArr;
        this.f23113d[i12] = dVar;
        this.f23110a = i12 + 1;
    }

    public final d d(int[] iArr) {
        int i10;
        int[][] iArr2 = this.f23112c;
        int i11 = 0;
        int i12 = 0;
        while (true) {
            i10 = -1;
            if (i12 >= this.f23110a) {
                i12 = -1;
                break;
            }
            if (StateSet.stateSetMatches(iArr2[i12], iArr)) {
                break;
            }
            i12++;
        }
        if (i12 < 0) {
            int[] iArr3 = StateSet.WILD_CARD;
            int[][] iArr4 = this.f23112c;
            while (true) {
                if (i11 >= this.f23110a) {
                    break;
                }
                if (StateSet.stateSetMatches(iArr4[i11], iArr3)) {
                    i10 = i11;
                    break;
                }
                i11++;
            }
            i12 = i10;
        }
        return i12 < 0 ? this.f23111b : this.f23113d[i12];
    }
}
