package kotlinx.coroutines.internal;

import kotlin.jvm.functions.Function2;
import kotlinx.coroutines.p1;

/* JADX INFO: loaded from: classes.dex */
public abstract class v {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final la.e f29490a = new la.e("NO_THREAD_ELEMENTS");

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Function2 f29491b = new Function2() { // from class: kotlinx.coroutines.internal.ThreadContextKt$countAll$1
        @Override // kotlin.jvm.functions.Function2
        public final Object invoke(Object obj, Object obj2) {
            kotlin.coroutines.f fVar = (kotlin.coroutines.f) obj2;
            if (!(fVar instanceof p1)) {
                return obj;
            }
            Integer num = obj instanceof Integer ? (Integer) obj : null;
            int iIntValue = num != null ? num.intValue() : 1;
            return iIntValue == 0 ? fVar : Integer.valueOf(iIntValue + 1);
        }
    };

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Function2 f29492c = new Function2() { // from class: kotlinx.coroutines.internal.ThreadContextKt$findOne$1
        @Override // kotlin.jvm.functions.Function2
        public final Object invoke(Object obj, Object obj2) {
            p1 p1Var = (p1) obj;
            kotlin.coroutines.f fVar = (kotlin.coroutines.f) obj2;
            if (p1Var != null) {
                return p1Var;
            }
            if (fVar instanceof p1) {
                return (p1) fVar;
            }
            return null;
        }
    };

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Function2 f29493d = new Function2() { // from class: kotlinx.coroutines.internal.ThreadContextKt$updateState$1
        @Override // kotlin.jvm.functions.Function2
        public final Object invoke(Object obj, Object obj2) {
            z zVar = (z) obj;
            kotlin.coroutines.f fVar = (kotlin.coroutines.f) obj2;
            if (fVar instanceof p1) {
                p1 p1Var = (p1) fVar;
                Object objH = p1Var.H(zVar.f29500a);
                int i10 = zVar.f29503d;
                zVar.f29501b[i10] = objH;
                zVar.f29503d = i10 + 1;
                zVar.f29502c[i10] = p1Var;
            }
            return zVar;
        }
    };

    public static final void a(kotlin.coroutines.h hVar, Object obj) {
        if (obj == f29490a) {
            return;
        }
        if (!(obj instanceof z)) {
            Object objE = hVar.E(null, f29492c);
            nb.g.g(objE, "null cannot be cast to non-null type kotlinx.coroutines.ThreadContextElement<kotlin.Any?>");
            ((w) ((p1) objE)).a(obj);
            return;
        }
        z zVar = (z) obj;
        p1[] p1VarArr = zVar.f29502c;
        int length = p1VarArr.length - 1;
        if (length < 0) {
            return;
        }
        while (true) {
            int i10 = length - 1;
            p1 p1Var = p1VarArr[length];
            nb.g.f(p1Var);
            ((w) p1Var).a(zVar.f29501b[length]);
            if (i10 < 0) {
                return;
            } else {
                length = i10;
            }
        }
    }

    public static final Object b(kotlin.coroutines.h hVar) {
        Object objE = hVar.E(0, f29491b);
        nb.g.f(objE);
        return objE;
    }

    public static final Object c(kotlin.coroutines.h hVar, Object obj) {
        if (obj == null) {
            obj = b(hVar);
        }
        return obj == 0 ? f29490a : obj instanceof Integer ? hVar.E(new z(hVar, ((Number) obj).intValue()), f29493d) : ((p1) obj).H(hVar);
    }
}
