package s.coroutines.internal;

import java.util.Objects;
import kotlin.Metadata;
import kotlin.coroutines.CoroutineContext;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Lambda;
import kotlin.jvm.internal.o;
import s.coroutines.ThreadContextElement;

/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u00000\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\u001a\u001a\u0010\n\u001a\u00020\u000b2\u0006\u0010\f\u001a\u00020\r2\b\u0010\u000e\u001a\u0004\u0018\u00010\u0004H\u0000\u001a\u0010\u0010\u000f\u001a\u00020\u00042\u0006\u0010\f\u001a\u00020\rH\u0000\u001a\u001c\u0010\u0010\u001a\u0004\u0018\u00010\u00042\u0006\u0010\f\u001a\u00020\r2\b\u0010\u0011\u001a\u0004\u0018\u00010\u0004H\u0000\"\u0010\u0010\u0000\u001a\u00020\u00018\u0000X\u0081\u0004¢\u0006\u0002\n\u0000\"$\u0010\u0002\u001a\u0018\u0012\u0006\u0012\u0004\u0018\u00010\u0004\u0012\u0004\u0012\u00020\u0005\u0012\u0006\u0012\u0004\u0018\u00010\u00040\u0003X\u0082\u0004¢\u0006\u0002\n\u0000\",\u0010\u0006\u001a \u0012\n\u0012\b\u0012\u0002\b\u0003\u0018\u00010\u0007\u0012\u0004\u0012\u00020\u0005\u0012\n\u0012\b\u0012\u0002\b\u0003\u0018\u00010\u00070\u0003X\u0082\u0004¢\u0006\u0002\n\u0000\" \u0010\b\u001a\u0014\u0012\u0004\u0012\u00020\t\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\t0\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u0012"}, d2 = {"NO_THREAD_ELEMENTS", "Lkotlinx/coroutines/internal/Symbol;", "countAll", "Lkotlin/Function2;", "", "Lkotlin/coroutines/CoroutineContext$Element;", "findOne", "Lkotlinx/coroutines/ThreadContextElement;", "updateState", "Lkotlinx/coroutines/internal/ThreadState;", "restoreThreadContext", "", "context", "Lkotlin/coroutines/CoroutineContext;", "oldState", "threadContextElements", "updateThreadContext", "countOrElement", "kotlinx-coroutines-core"}, k = 2, mv = {1, 6, 0}, xi = 48)
public final class u {
    public static final Symbol a = new Symbol("NO_THREAD_ELEMENTS");

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Function2<Object, CoroutineContext.a, Object> f9357b = a.f9360f;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Function2<ThreadContextElement<?>, CoroutineContext.a, ThreadContextElement<?>> f9358c = b.f9361f;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Function2<ThreadState, CoroutineContext.a, ThreadState> f9359d = c.f9362f;

    @Metadata(d1 = {"\u0000\u0010\n\u0000\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\u0010\u0000\u001a\u0004\u0018\u00010\u00012\b\u0010\u0002\u001a\u0004\u0018\u00010\u00012\u0006\u0010\u0003\u001a\u00020\u0004H\n¢\u0006\u0002\b\u0005"}, d2 = {"<no name provided>", "", "countOrElement", "element", "Lkotlin/coroutines/CoroutineContext$Element;", "invoke"}, k = 3, mv = {1, 6, 0}, xi = 48)
    public static final class a extends Lambda implements Function2<Object, CoroutineContext.a, Object> {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public static final a f9360f = new a();

        public a() {
            super(2);
        }

        @Override // kotlin.jvm.functions.Function2
        public Object invoke(Object obj, CoroutineContext.a aVar) {
            CoroutineContext.a aVar2 = aVar;
            if (!(aVar2 instanceof ThreadContextElement)) {
                return obj;
            }
            Integer num = obj instanceof Integer ? (Integer) obj : null;
            int iIntValue = num != null ? num.intValue() : 1;
            return iIntValue == 0 ? aVar2 : Integer.valueOf(iIntValue + 1);
        }
    }

    @Metadata(d1 = {"\u0000\u0010\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\u0010\u0000\u001a\b\u0012\u0002\b\u0003\u0018\u00010\u00012\f\u0010\u0002\u001a\b\u0012\u0002\b\u0003\u0018\u00010\u00012\u0006\u0010\u0003\u001a\u00020\u0004H\n¢\u0006\u0002\b\u0005"}, d2 = {"<no name provided>", "Lkotlinx/coroutines/ThreadContextElement;", "found", "element", "Lkotlin/coroutines/CoroutineContext$Element;", "invoke"}, k = 3, mv = {1, 6, 0}, xi = 48)
    public static final class b extends Lambda implements Function2<ThreadContextElement<?>, CoroutineContext.a, ThreadContextElement<?>> {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public static final b f9361f = new b();

        public b() {
            super(2);
        }

        @Override // kotlin.jvm.functions.Function2
        public ThreadContextElement<?> invoke(ThreadContextElement<?> threadContextElement, CoroutineContext.a aVar) {
            ThreadContextElement<?> threadContextElement2 = threadContextElement;
            CoroutineContext.a aVar2 = aVar;
            if (threadContextElement2 != null) {
                return threadContextElement2;
            }
            if (aVar2 instanceof ThreadContextElement) {
                return (ThreadContextElement) aVar2;
            }
            return null;
        }
    }

    @Metadata(d1 = {"\u0000\u0010\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\u0010\u0000\u001a\u00020\u00012\u0006\u0010\u0002\u001a\u00020\u00012\u0006\u0010\u0003\u001a\u00020\u0004H\n¢\u0006\u0002\b\u0005"}, d2 = {"<no name provided>", "Lkotlinx/coroutines/internal/ThreadState;", "state", "element", "Lkotlin/coroutines/CoroutineContext$Element;", "invoke"}, k = 3, mv = {1, 6, 0}, xi = 48)
    public static final class c extends Lambda implements Function2<ThreadState, CoroutineContext.a, ThreadState> {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public static final c f9362f = new c();

        public c() {
            super(2);
        }

        @Override // kotlin.jvm.functions.Function2
        public ThreadState invoke(ThreadState threadState, CoroutineContext.a aVar) {
            ThreadState threadState2 = threadState;
            CoroutineContext.a aVar2 = aVar;
            if (aVar2 instanceof ThreadContextElement) {
                ThreadContextElement<Object> threadContextElement = (ThreadContextElement) aVar2;
                Object objU = threadContextElement.u(threadState2.a);
                Object[] objArr = threadState2.f9363b;
                int i2 = threadState2.f9365d;
                objArr[i2] = objU;
                ThreadContextElement<Object>[] threadContextElementArr = threadState2.f9364c;
                threadState2.f9365d = i2 + 1;
                threadContextElementArr[i2] = threadContextElement;
            }
            return threadState2;
        }
    }

    public static final void a(CoroutineContext coroutineContext, Object obj) {
        if (obj == a) {
            return;
        }
        if (!(obj instanceof ThreadState)) {
            Object objFold = coroutineContext.fold(null, f9358c);
            Objects.requireNonNull(objFold, "null cannot be cast to non-null type kotlinx.coroutines.ThreadContextElement<kotlin.Any?>");
            ((ThreadContextElement) objFold).o(coroutineContext, obj);
            return;
        }
        ThreadState threadState = (ThreadState) obj;
        int length = threadState.f9364c.length - 1;
        if (length < 0) {
            return;
        }
        while (true) {
            int i2 = length - 1;
            ThreadContextElement<Object> threadContextElement = threadState.f9364c[length];
            o.c(threadContextElement);
            threadContextElement.o(coroutineContext, threadState.f9363b[length]);
            if (i2 < 0) {
                return;
            } else {
                length = i2;
            }
        }
    }

    public static final Object b(CoroutineContext coroutineContext, Object obj) {
        if (obj == null) {
            obj = coroutineContext.fold(0, f9357b);
            o.c(obj);
        }
        return obj == 0 ? a : obj instanceof Integer ? coroutineContext.fold(new ThreadState(coroutineContext, ((Number) obj).intValue()), f9359d) : ((ThreadContextElement) obj).u(coroutineContext);
    }
}
