package defpackage;

import androidx.media3.container.NalUnitUtil;
import defpackage.r77;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.ContinuationImpl;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\b\u0016\u0018\u0000*\b\b\u0000\u0010\u0002*\u00020\u0001*\b\b\u0001\u0010\u0003*\u00020\u00012\u00020\u0001¨\u0006\u0004"}, d2 = {"Li77;", "", "TSubject", "TContext", "ktor-utils"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public class i77<TSubject, TContext> {
    public final ArrayList b;
    public int c;
    public boolean d;
    public q77 e;
    public final fx a = hx.a();

    @kp6
    private volatile /* synthetic */ Object interceptors$delegate = null;

    public i77(q77... q77VarArr) {
        this.b = y41.ar(Arrays.copyOf(q77VarArr, q77VarArr.length));
    }

    /* JADX WARN: Removed duplicated region for block: B:25:0x005a  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a(Object obj, Object obj2, ContinuationImpl continuationImpl) {
        int iAe;
        int iAe2;
        gj1 b = continuationImpl.getB();
        if (((List) this.interceptors$delegate) == null) {
            int i = this.c;
            if (i == 0) {
                this.interceptors$delegate = ik2.a;
                this.d = false;
                this.e = null;
            } else {
                ArrayList arrayList = this.b;
                if (i != 1 || (iAe2 = y41.ae(arrayList)) < 0) {
                    ArrayList arrayList2 = new ArrayList();
                    iAe = y41.ae(arrayList);
                    if (iAe >= 0) {
                        int i2 = 0;
                        while (true) {
                            Object obj3 = arrayList.get(i2);
                            m67 m67Var = obj3 instanceof m67 ? (m67) obj3 : null;
                            if (m67Var != null) {
                                List list = m67Var.c;
                                arrayList2.ensureCapacity(list.size() + arrayList2.size());
                                int size = list.size();
                                for (int i3 = 0; i3 < size; i3++) {
                                    arrayList2.add(list.get(i3));
                                }
                            }
                            if (i2 == iAe) {
                                break;
                            }
                            i2++;
                        }
                    }
                    this.interceptors$delegate = arrayList2;
                    this.d = false;
                    this.e = null;
                } else {
                    int i4 = 0;
                    while (true) {
                        Object obj4 = arrayList.get(i4);
                        m67 m67Var2 = obj4 instanceof m67 ? (m67) obj4 : null;
                        if (m67Var2 != null && !m67Var2.c.isEmpty()) {
                            List list2 = m67Var2.c;
                            m67Var2.d = true;
                            this.interceptors$delegate = list2;
                            this.d = false;
                            this.e = m67Var2.a;
                            break;
                        }
                        if (i4 == iAe2) {
                            break;
                        }
                        i4++;
                    }
                    ArrayList arrayList22 = new ArrayList();
                    iAe = y41.ae(arrayList);
                    if (iAe >= 0) {
                    }
                    this.interceptors$delegate = arrayList22;
                    this.d = false;
                    this.e = null;
                }
            }
        }
        this.d = true;
        List list3 = (List) this.interceptors$delegate;
        cr4.d(list3);
        boolean zD = getF();
        cr4.g(obj, "context");
        cr4.g(obj2, "subject");
        cr4.g(b, "coroutineContext");
        return ((l77.a || zD) ? new lw1(obj, list3, obj2, b) : new xe9(obj2, obj, list3)).c(obj2, continuationImpl);
    }

    public final m67 b(q77 q77Var) {
        ArrayList arrayList = this.b;
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            Object obj = arrayList.get(i);
            if (obj == q77Var) {
                m67 m67Var = new m67(q77Var, r77.c.a);
                arrayList.set(i, m67Var);
                return m67Var;
            }
            if (obj instanceof m67) {
                m67 m67Var2 = (m67) obj;
                if (m67Var2.a == q77Var) {
                    return m67Var2;
                }
            }
        }
        return null;
    }

    public final int c(q77 q77Var) {
        ArrayList arrayList = this.b;
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            Object obj = arrayList.get(i);
            if (obj == q77Var || ((obj instanceof m67) && ((m67) obj).a == q77Var)) {
                return i;
            }
        }
        return -1;
    }

    /* JADX INFO: renamed from: d */
    public boolean getF() {
        return false;
    }

    public final boolean e(q77 q77Var) {
        ArrayList arrayList = this.b;
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            Object obj = arrayList.get(i);
            if (obj == q77Var) {
                return true;
            }
            if ((obj instanceof m67) && ((m67) obj).a == q77Var) {
                return true;
            }
        }
        return false;
    }

    public final void f(q77 q77Var, q77 q77Var2) throws vr4 {
        r77 r77Var;
        q77 q77Var3;
        cr4.g(q77Var, "reference");
        if (e(q77Var2)) {
            return;
        }
        int iC = c(q77Var);
        if (iC == -1) {
            throw new vr4("Phase " + q77Var + " was not registered for this pipeline");
        }
        int i = iC + 1;
        ArrayList arrayList = this.b;
        int iAe = y41.ae(arrayList);
        if (i <= iAe) {
            while (true) {
                Object obj = arrayList.get(i);
                m67 m67Var = obj instanceof m67 ? (m67) obj : null;
                if (m67Var != null && (r77Var = m67Var.b) != null) {
                    r77.a aVar = r77Var instanceof r77.a ? (r77.a) r77Var : null;
                    if (aVar != null && (q77Var3 = aVar.a) != null && q77Var3.equals(q77Var)) {
                        iC = i;
                    }
                    if (i == iAe) {
                        break;
                    } else {
                        i++;
                    }
                } else {
                    break;
                }
            }
        }
        arrayList.add(iC + 1, new m67(q77Var2, new r77.a(q77Var)));
    }

    public final void g(q77 q77Var, ir3 ir3Var) {
        cr4.g(q77Var, "phase");
        m67 m67VarB = b(q77Var);
        if (m67VarB == null) {
            throw new vr4("Phase " + q77Var + " was not registered for this pipeline");
        }
        List list = (List) this.interceptors$delegate;
        if (!this.b.isEmpty() && list != null && !this.d && (list instanceof List) && (!(list instanceof f25) || (list instanceof j25))) {
            if (cr4.b(this.e, q77Var)) {
                list.add(ir3Var);
            } else if (q77Var.equals(y41.ak(this.b)) || c(q77Var) == y41.ae(this.b)) {
                m67 m67VarB2 = b(q77Var);
                cr4.d(m67VarB2);
                if (m67VarB2.d) {
                    m67VarB2.c = y41.bp(m67VarB2.c);
                    m67VarB2.d = false;
                }
                m67VarB2.c.add(ir3Var);
                list.add(ir3Var);
            }
            this.c++;
            return;
        }
        if (m67VarB.d) {
            m67VarB.c = y41.bp(m67VarB.c);
            m67VarB.d = false;
        }
        m67VarB.c.add(ir3Var);
        this.c++;
        this.interceptors$delegate = null;
        this.d = false;
        this.e = null;
    }
}
