package kotlin.coroutines;

import java.io.Serializable;
import kotlin.Metadata;
import kotlin.SinceKotlin;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Ref$IntRef;
import kotlin.jvm.internal.SourceDebugExtension;
import org.jetbrains.annotations.NotNull;

/* JADX INFO: loaded from: classes.dex */
@SinceKotlin(version = "1.3")
@Metadata(d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\b\u0001\u0018\u00002\u00020\u00012\u00060\u0002j\u0002`\u0003:\u0001\u000bJ\b\u0010\u0005\u001a\u00020\u0004H\u0002R\u0014\u0010\u0006\u001a\u00020\u00018\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0006\u0010\u0007R\u0014\u0010\t\u001a\u00020\b8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\t\u0010\n¨\u0006\f"}, d2 = {"Lkotlin/coroutines/CombinedContext;", "Lkotlin/coroutines/h;", "Ljava/io/Serializable;", "Lkotlin/io/Serializable;", "", "writeReplace", "left", "Lkotlin/coroutines/h;", "Lkotlin/coroutines/f;", "element", "Lkotlin/coroutines/f;", "Serialized", "kotlin-stdlib"}, k = 1, mv = {1, 8, 0})
@SourceDebugExtension({"SMAP\nCoroutineContextImpl.kt\nKotlin\n*S Kotlin\n*F\n+ 1 CoroutineContextImpl.kt\nkotlin/coroutines/CombinedContext\n+ 2 fake.kt\nkotlin/jvm/internal/FakeKt\n*L\n1#1,196:1\n1#2:197\n*E\n"})
public final class CombinedContext implements h, Serializable {

    @NotNull
    private final f element;

    @NotNull
    private final h left;

    @Metadata(d1 = {"\u0000\u001e\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0011\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0002\u0018\u00002\u00060\u0001j\u0002`\u0002J\b\u0010\u0004\u001a\u00020\u0003H\u0002R\u001d\u0010\u0007\u001a\b\u0012\u0004\u0012\u00020\u00060\u00058\u0006¢\u0006\f\n\u0004\b\u0007\u0010\b\u001a\u0004\b\t\u0010\n¨\u0006\u000b"}, d2 = {"Lkotlin/coroutines/CombinedContext$Serialized;", "Ljava/io/Serializable;", "Lkotlin/io/Serializable;", "", "readResolve", "", "Lkotlin/coroutines/h;", "elements", "[Lkotlin/coroutines/h;", "getElements", "()[Lkotlin/coroutines/h;", "kotlin-stdlib"}, k = 1, mv = {1, 8, 0})
    @SourceDebugExtension({"SMAP\nCoroutineContextImpl.kt\nKotlin\n*S Kotlin\n*F\n+ 1 CoroutineContextImpl.kt\nkotlin/coroutines/CombinedContext$Serialized\n+ 2 _Arrays.kt\nkotlin/collections/ArraysKt___ArraysKt\n*L\n1#1,196:1\n12990#2,3:197\n*S KotlinDebug\n*F\n+ 1 CoroutineContextImpl.kt\nkotlin/coroutines/CombinedContext$Serialized\n*L\n193#1:197,3\n*E\n"})
    public static final class Serialized implements Serializable {
        private static final long serialVersionUID = 0;

        @NotNull
        private final h[] elements;

        public Serialized(h[] hVarArr) {
            this.elements = hVarArr;
        }

        private final Object readResolve() {
            h[] hVarArr = this.elements;
            h hVarP = EmptyCoroutineContext.f29221a;
            for (h hVar : hVarArr) {
                hVarP = hVarP.p(hVar);
            }
            return hVarP;
        }
    }

    public CombinedContext(f fVar, h hVar) {
        nb.g.i(hVar, "left");
        nb.g.i(fVar, "element");
        this.left = hVar;
        this.element = fVar;
    }

    private final Object writeReplace() {
        int iA = a();
        final h[] hVarArr = new h[iA];
        final Ref$IntRef ref$IntRef = new Ref$IntRef();
        E(kd.e.f29109a, new Function2() { // from class: kotlin.coroutines.CombinedContext.writeReplace.1
            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            {
                super(2);
            }

            @Override // kotlin.jvm.functions.Function2
            public final Object invoke(Object obj, Object obj2) {
                f fVar = (f) obj2;
                nb.g.i((kd.e) obj, "<anonymous parameter 0>");
                nb.g.i(fVar, "element");
                h[] hVarArr2 = hVarArr;
                Ref$IntRef ref$IntRef2 = ref$IntRef;
                int i10 = ref$IntRef2.element;
                ref$IntRef2.element = i10 + 1;
                hVarArr2[i10] = fVar;
                return kd.e.f29109a;
            }
        });
        if (ref$IntRef.element == iA) {
            return new Serialized(hVarArr);
        }
        throw new IllegalStateException("Check failed.".toString());
    }

    @Override // kotlin.coroutines.h
    public final Object E(Object obj, Function2 function2) {
        nb.g.i(function2, "operation");
        return function2.invoke(this.left.E(obj, function2), this.element);
    }

    public final int a() {
        int i10 = 2;
        CombinedContext combinedContext = this;
        while (true) {
            h hVar = combinedContext.left;
            combinedContext = hVar instanceof CombinedContext ? (CombinedContext) hVar : null;
            if (combinedContext == null) {
                return i10;
            }
            i10++;
        }
    }

    public final boolean equals(Object obj) {
        if (this != obj) {
            if (obj instanceof CombinedContext) {
                CombinedContext combinedContext = (CombinedContext) obj;
                if (combinedContext.a() == a()) {
                    CombinedContext combinedContext2 = this;
                    while (true) {
                        f fVar = combinedContext2.element;
                        if (!nb.g.c(combinedContext.m(fVar.getKey()), fVar)) {
                            break;
                        }
                        h hVar = combinedContext2.left;
                        if (hVar instanceof CombinedContext) {
                            combinedContext2 = (CombinedContext) hVar;
                        } else {
                            nb.g.g(hVar, "null cannot be cast to non-null type kotlin.coroutines.CoroutineContext.Element");
                            f fVar2 = (f) hVar;
                            if (nb.g.c(combinedContext.m(fVar2.getKey()), fVar2)) {
                            }
                        }
                    }
                }
            }
            return false;
        }
        return true;
    }

    public final int hashCode() {
        return this.element.hashCode() + this.left.hashCode();
    }

    @Override // kotlin.coroutines.h
    public final f m(g gVar) {
        nb.g.i(gVar, "key");
        CombinedContext combinedContext = this;
        while (true) {
            f fVarM = combinedContext.element.m(gVar);
            if (fVarM != null) {
                return fVarM;
            }
            h hVar = combinedContext.left;
            if (!(hVar instanceof CombinedContext)) {
                return hVar.m(gVar);
            }
            combinedContext = (CombinedContext) hVar;
        }
    }

    @Override // kotlin.coroutines.h
    public final h p(h hVar) {
        return e.a(this, hVar);
    }

    public final String toString() {
        return "[" + ((String) E("", new Function2() { // from class: kotlin.coroutines.CombinedContext.toString.1
            @Override // kotlin.jvm.functions.Function2
            public final Object invoke(Object obj, Object obj2) {
                String str = (String) obj;
                f fVar = (f) obj2;
                nb.g.i(str, "acc");
                nb.g.i(fVar, "element");
                if (str.length() == 0) {
                    return fVar.toString();
                }
                return str + ", " + fVar;
            }
        })) + ']';
    }

    @Override // kotlin.coroutines.h
    public final h x(g gVar) {
        nb.g.i(gVar, "key");
        if (this.element.m(gVar) != null) {
            return this.left;
        }
        h hVarX = this.left.x(gVar);
        return hVarX == this.left ? this : hVarX == EmptyCoroutineContext.f29221a ? this.element : new CombinedContext(this.element, hVarX);
    }
}
