package kotlinx.coroutines.flow.internal;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000\u001a\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\u001a\u001f\u0010\u0004\u001a\u00020\u0003*\u0006\u0012\u0002\b\u00030\u00002\u0006\u0010\u0002\u001a\u00020\u0001H\u0001¢\u0006\u0004\b\u0004\u0010\u0005\u001a\"\u0010\b\u001a\u0004\u0018\u00010\u0006*\u0004\u0018\u00010\u00062\b\u0010\u0007\u001a\u0004\u0018\u00010\u0006H\u0080\u0010¢\u0006\u0004\b\b\u0010\t¨\u0006\n"}, d2 = {"Lkotlinx/coroutines/flow/internal/SafeCollector;", "Lkotlin/coroutines/CoroutineContext;", "currentContext", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "b", "(Lkotlinx/coroutines/flow/internal/SafeCollector;Lkotlin/coroutines/CoroutineContext;)V", "Lkotlinx/coroutines/Job;", "collectJob", "d", "(Lkotlinx/coroutines/Job;Lkotlinx/coroutines/Job;)Lkotlinx/coroutines/Job;", "kotlinx-coroutines-core"}, k = 2, mv = {2, 1, 0}, xi = 48)
public final class SafeCollector_commonKt {
    public static final void b(final kotlinx.coroutines.flow.internal.SafeCollector safeCollector, kotlin.coroutines.CoroutineContext coroutineContext) {
        if (((java.lang.Number) coroutineContext.u(0, new kotlin.jvm.functions.Function2() { // from class: kotlinx.coroutines.flow.internal.b
            public final java.lang.Object invoke(java.lang.Object obj, java.lang.Object obj2) {
                return java.lang.Integer.valueOf(kotlinx.coroutines.flow.internal.SafeCollector_commonKt.c(safeCollector, ((java.lang.Integer) obj).intValue(), (kotlin.coroutines.CoroutineContext.Element) obj2));
            }
        })).intValue() == safeCollector.collectContextSize) {
            return;
        }
        throw new java.lang.IllegalStateException(("Flow invariant is violated:\n\t\tFlow was collected in " + safeCollector.collectContext + ",\n\t\tbut emission happened in " + coroutineContext + ".\n\t\tPlease refer to 'flow' documentation or use 'flowOn' instead").toString());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final int c(kotlinx.coroutines.flow.internal.SafeCollector safeCollector, int i, kotlin.coroutines.CoroutineContext.Element element) {
        kotlinx.coroutines.Job.Key key = element.getKey();
        kotlinx.coroutines.Job jobR = safeCollector.collectContext.r(key);
        if (key != kotlinx.coroutines.Job.x0) {
            if (element != jobR) {
                return Integer.MIN_VALUE;
            }
            return i + 1;
        }
        kotlinx.coroutines.Job job = jobR;
        kotlin.jvm.internal.Intrinsics.j(element, "null cannot be cast to non-null type kotlinx.coroutines.Job");
        kotlinx.coroutines.Job jobD = d((kotlinx.coroutines.Job) element, job);
        if (jobD == job) {
            return job == null ? i : i + 1;
        }
        throw new java.lang.IllegalStateException(("Flow invariant is violated:\n\t\tEmission from another coroutine is detected.\n\t\tChild of " + jobD + ", expected child of " + job + ".\n\t\tFlowCollector is not thread-safe and concurrent emissions are prohibited.\n\t\tTo mitigate this restriction please use 'channelFlow' builder instead of 'flow'").toString());
    }

    public static final kotlinx.coroutines.Job d(kotlinx.coroutines.Job job, kotlinx.coroutines.Job job2) {
        while (job != null) {
            if (job == job2 || !(job instanceof kotlinx.coroutines.internal.ScopeCoroutine)) {
                return job;
            }
            job = ((kotlinx.coroutines.internal.ScopeCoroutine) job).q0();
        }
        return null;
    }
}
