package kotlinx.coroutines.flow.internal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import kotlin.Metadata;
import kotlin.Result;
import kotlin.collections.n;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.EmptyCoroutineContext;
import kotlin.coroutines.intrinsics.CoroutineSingletons;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import kotlin.jvm.JvmField;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.SourceDebugExtension;
import kotlinx.coroutines.g1;
import kotlinx.coroutines.internal.r;
import kotlinx.coroutines.u;
import kotlinx.coroutines.y0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import sd.p;

/* JADX INFO: loaded from: classes.dex */
@Metadata(d1 = {"\u00002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\b\u0000\u0018\u0000*\u0004\b\u0000\u0010\u00012\b\u0012\u0004\u0012\u00028\u00000\u00022\u00020\u00032\u00020\u0004R\u001a\u0010\u0005\u001a\b\u0012\u0004\u0012\u00028\u00000\u00028\u0000X\u0081\u0004¢\u0006\u0006\n\u0004\b\u0005\u0010\u0006R\u0014\u0010\b\u001a\u00020\u00078\u0000X\u0081\u0004¢\u0006\u0006\n\u0004\b\b\u0010\tR\u0014\u0010\u000b\u001a\u00020\n8\u0000X\u0081\u0004¢\u0006\u0006\n\u0004\b\u000b\u0010\fR\u0018\u0010\r\u001a\u0004\u0018\u00010\u00078\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\r\u0010\tR\u001e\u0010\u0010\u001a\n\u0012\u0004\u0012\u00020\u000f\u0018\u00010\u000e8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0010\u0010\u0011¨\u0006\u0012"}, d2 = {"Lkotlinx/coroutines/flow/internal/SafeCollector;", "T", "Lkotlinx/coroutines/flow/g;", "Lkotlin/coroutines/jvm/internal/ContinuationImpl;", "Lnd/b;", "collector", "Lkotlinx/coroutines/flow/g;", "Lkotlin/coroutines/h;", "collectContext", "Lkotlin/coroutines/h;", "", "collectContextSize", "I", "lastEmissionContext", "Lkotlin/coroutines/Continuation;", "Lkd/e;", "completion_", "Lkotlin/coroutines/Continuation;", "kotlinx-coroutines-core"}, k = 1, mv = {1, 9, 0})
@SourceDebugExtension({"SMAP\nSafeCollector.kt\nKotlin\n*S Kotlin\n*F\n+ 1 SafeCollector.kt\nkotlinx/coroutines/flow/internal/SafeCollector\n+ 2 fake.kt\nkotlin/jvm/internal/FakeKt\n*L\n1#1,182:1\n1#2:183\n*E\n"})
public final class SafeCollector<T> extends ContinuationImpl implements kotlinx.coroutines.flow.g {

    @JvmField
    @NotNull
    public final kotlin.coroutines.h collectContext;

    @JvmField
    public final int collectContextSize;

    @JvmField
    @NotNull
    public final kotlinx.coroutines.flow.g collector;

    @Nullable
    private Continuation completion_;

    @Nullable
    private kotlin.coroutines.h lastEmissionContext;

    public SafeCollector(kotlinx.coroutines.flow.g gVar, kotlin.coroutines.h hVar) {
        super(i.f29392a, EmptyCoroutineContext.f29221a);
        this.collector = gVar;
        this.collectContext = hVar;
        this.collectContextSize = ((Number) hVar.E(0, new Function2() { // from class: kotlinx.coroutines.flow.internal.SafeCollector$collectContextSize$1
            @Override // kotlin.jvm.functions.Function2
            public final Object invoke(Object obj, Object obj2) {
                return Integer.valueOf(((Number) obj).intValue() + 1);
            }
        })).intValue();
    }

    @Override // kotlinx.coroutines.flow.g
    public final Object c(Object obj, Continuation continuation) {
        try {
            Object objE = e(continuation, obj);
            return objE == CoroutineSingletons.f29226a ? objE : kd.e.f29109a;
        } catch (Throwable th) {
            this.lastEmissionContext = new g(continuation.getContext(), th);
            throw th;
        }
    }

    public final Object e(Continuation continuation, Object obj) {
        Comparable comparable;
        kotlin.coroutines.h context = continuation.getContext();
        y0 y0Var = (y0) context.m(u.f29565b);
        if (y0Var != null && !y0Var.isActive()) {
            throw ((g1) y0Var).z();
        }
        kotlin.coroutines.h hVar = this.lastEmissionContext;
        if (hVar != context) {
            int i10 = 0;
            if (hVar instanceof g) {
                String str = "\n            Flow exception transparency is violated:\n                Previous 'emit' call has thrown exception " + ((g) hVar).f29390a + ", but then emission attempt of value '" + obj + "' has been detected.\n                Emissions from 'catch' blocks are prohibited in order to avoid unspecified behaviour, 'Flow.catch' operator can be used instead.\n                For a more detailed explanation, please refer to Flow documentation.\n            ";
                nb.g.i(str, "<this>");
                List listB1 = kotlin.sequences.j.b1(kotlin.text.i.u0(str));
                ArrayList<String> arrayList = new ArrayList();
                for (T t10 : listB1) {
                    if (!kotlin.text.h.e0((String) t10)) {
                        arrayList.add(t10);
                    }
                }
                ArrayList arrayList2 = new ArrayList(kotlin.collections.i.Z0(10, arrayList));
                for (String str2 : arrayList) {
                    int length = str2.length();
                    int length2 = 0;
                    while (true) {
                        if (length2 >= length) {
                            length2 = -1;
                            break;
                        }
                        if (!ua.b.C(str2.charAt(length2))) {
                            break;
                        }
                        length2++;
                    }
                    if (length2 == -1) {
                        length2 = str2.length();
                    }
                    arrayList2.add(Integer.valueOf(length2));
                }
                Iterator it = arrayList2.iterator();
                if (it.hasNext()) {
                    comparable = (Comparable) it.next();
                    while (it.hasNext()) {
                        Comparable comparable2 = (Comparable) it.next();
                        if (comparable.compareTo(comparable2) > 0) {
                            comparable = comparable2;
                        }
                    }
                } else {
                    comparable = null;
                }
                Integer num = (Integer) comparable;
                int iIntValue = num != null ? num.intValue() : 0;
                int length3 = str.length();
                listB1.size();
                sd.l lVarW = kotlin.text.d.W();
                int iB0 = hb.a.b0(listB1);
                ArrayList arrayList3 = new ArrayList();
                for (T t11 : listB1) {
                    int i11 = i10 + 1;
                    if (i10 < 0) {
                        hb.a.C0();
                        throw null;
                    }
                    String str3 = (String) t11;
                    if ((i10 == 0 || i10 == iB0) && kotlin.text.h.e0(str3)) {
                        str3 = null;
                    } else {
                        nb.g.i(str3, "<this>");
                        if (iIntValue < 0) {
                            throw new IllegalArgumentException(defpackage.e.e("Requested character count ", iIntValue, " is less than zero.").toString());
                        }
                        int length4 = str3.length();
                        if (iIntValue <= length4) {
                            length4 = iIntValue;
                        }
                        String strSubstring = str3.substring(length4);
                        nb.g.h(strSubstring, "this as java.lang.String).substring(startIndex)");
                        String str4 = (String) lVarW.invoke(strSubstring);
                        if (str4 != null) {
                            str3 = str4;
                        }
                    }
                    if (str3 != null) {
                        arrayList3.add(str3);
                    }
                    i10 = i11;
                }
                StringBuilder sb = new StringBuilder(length3);
                n.h1(arrayList3, sb, "\n", "", "", -1, "...", null);
                String string = sb.toString();
                nb.g.h(string, "mapIndexedNotNull { inde…\"\\n\")\n        .toString()");
                throw new IllegalStateException(string.toString());
            }
            if (((Number) context.E(0, new Function2() { // from class: kotlinx.coroutines.flow.internal.SafeCollector_commonKt$checkContext$result$1
                {
                    super(2);
                }

                @Override // kotlin.jvm.functions.Function2
                public final Object invoke(Object obj2, Object obj3) {
                    int iIntValue2 = ((Number) obj2).intValue();
                    kotlin.coroutines.f fVar = (kotlin.coroutines.f) obj3;
                    kotlin.coroutines.g key = fVar.getKey();
                    kotlin.coroutines.f fVarM = this.$this_checkContext.collectContext.m(key);
                    if (key != u.f29565b) {
                        return Integer.valueOf(fVar != fVarM ? Integer.MIN_VALUE : iIntValue2 + 1);
                    }
                    y0 y0Var2 = (y0) fVarM;
                    y0 parent = (y0) fVar;
                    while (true) {
                        if (parent != null) {
                            if (parent == y0Var2 || !(parent instanceof r)) {
                                break;
                            }
                            kotlinx.coroutines.k kVar = (kotlinx.coroutines.k) g1.f29434b.get((g1) parent);
                            parent = kVar != null ? kVar.getParent() : null;
                        } else {
                            parent = null;
                            break;
                        }
                    }
                    if (parent == y0Var2) {
                        if (y0Var2 != null) {
                            iIntValue2++;
                        }
                        return Integer.valueOf(iIntValue2);
                    }
                    throw new IllegalStateException(("Flow invariant is violated:\n\t\tEmission from another coroutine is detected.\n\t\tChild of " + parent + ", expected child of " + y0Var2 + ".\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());
                }
            })).intValue() != this.collectContextSize) {
                throw new IllegalStateException(("Flow invariant is violated:\n\t\tFlow was collected in " + this.collectContext + ",\n\t\tbut emission happened in " + context + ".\n\t\tPlease refer to 'flow' documentation or use 'flowOn' instead").toString());
            }
            this.lastEmissionContext = context;
        }
        this.completion_ = continuation;
        p pVar = j.f29394a;
        kotlinx.coroutines.flow.g gVar = this.collector;
        nb.g.g(gVar, "null cannot be cast to non-null type kotlinx.coroutines.flow.FlowCollector<kotlin.Any?>");
        Object objA = pVar.a(gVar, obj, this);
        if (!nb.g.c(objA, CoroutineSingletons.f29226a)) {
            this.completion_ = null;
        }
        return objA;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl, nd.b
    public final nd.b getCallerFrame() {
        Continuation continuation = this.completion_;
        if (continuation instanceof nd.b) {
            return (nd.b) continuation;
        }
        return null;
    }

    @Override // kotlin.coroutines.jvm.internal.ContinuationImpl, kotlin.coroutines.Continuation
    public final kotlin.coroutines.h getContext() {
        kotlin.coroutines.h hVar = this.lastEmissionContext;
        return hVar == null ? EmptyCoroutineContext.f29221a : hVar;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final StackTraceElement getStackTraceElement() {
        return null;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Object invokeSuspend(Object obj) {
        Throwable thA = Result.a(obj);
        if (thA != null) {
            this.lastEmissionContext = new g(getContext(), thA);
        }
        Continuation continuation = this.completion_;
        if (continuation != null) {
            continuation.resumeWith(obj);
        }
        return CoroutineSingletons.f29226a;
    }

    @Override // kotlin.coroutines.jvm.internal.ContinuationImpl, kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final void releaseIntercepted() {
        super.releaseIntercepted();
    }
}
