package androidx.navigation;

import C0.C0104e;
import Y4.c0;
import android.util.Log;
import androidx.lifecycle.EnumC1108q;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import x4.AbstractC3233J;
import x4.AbstractC3251q;
import x4.C3224A;
import x4.C3249o;

/* JADX INFO: renamed from: androidx.navigation.n, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1139n {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ReentrantLock f12771a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final c0 f12772b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final c0 f12773c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public boolean f12774d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Y4.J f12775e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final Y4.J f12776f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final X f12777g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final /* synthetic */ H f12778h;

    public C1139n(H h5, X navigator) {
        kotlin.jvm.internal.o.h(navigator, "navigator");
        this.f12778h = h5;
        this.f12771a = new ReentrantLock(true);
        c0 c0VarC = Y4.O.c(x4.y.f26065a);
        this.f12772b = c0VarC;
        c0 c0VarC2 = Y4.O.c(C3224A.f26032a);
        this.f12773c = c0VarC2;
        this.f12775e = new Y4.J(c0VarC);
        this.f12776f = new Y4.J(c0VarC2);
        this.f12777g = navigator;
    }

    public final void a(C1137l backStackEntry) {
        kotlin.jvm.internal.o.h(backStackEntry, "backStackEntry");
        ReentrantLock reentrantLock = this.f12771a;
        reentrantLock.lock();
        try {
            c0 c0Var = this.f12772b;
            ArrayList arrayListT0 = AbstractC3251q.t0((Collection) c0Var.getValue(), backStackEntry);
            c0Var.getClass();
            c0Var.k(null, arrayListT0);
        } finally {
            reentrantLock.unlock();
        }
    }

    public final void b(C1137l entry) {
        NavControllerViewModel navControllerViewModel;
        kotlin.jvm.internal.o.h(entry, "entry");
        H h5 = this.f12778h;
        LinkedHashMap linkedHashMap = h5.f12692z;
        boolean zC = kotlin.jvm.internal.o.c(linkedHashMap.get(entry), Boolean.TRUE);
        c0 c0Var = this.f12773c;
        c0Var.k(null, AbstractC3233J.q((Set) c0Var.getValue(), entry));
        linkedHashMap.remove(entry);
        C3249o c3249o = h5.f12674g;
        boolean zContains = c3249o.contains(entry);
        c0 c0Var2 = h5.f12676i;
        if (zContains) {
            if (this.f12774d) {
                return;
            }
            h5.w();
            ArrayList arrayListE0 = AbstractC3251q.E0(c3249o);
            c0 c0Var3 = h5.f12675h;
            c0Var3.getClass();
            c0Var3.k(null, arrayListE0);
            ArrayList arrayListT = h5.t();
            c0Var2.getClass();
            c0Var2.k(null, arrayListT);
            return;
        }
        h5.v(entry);
        if (entry.f12763k.f12191c.compareTo(EnumC1108q.f12182c) >= 0) {
            entry.b(EnumC1108q.f12180a);
        }
        String str = entry.f12761f;
        if (c3249o == null || !c3249o.isEmpty()) {
            Iterator it = c3249o.iterator();
            while (it.hasNext()) {
                if (kotlin.jvm.internal.o.c(((C1137l) it.next()).f12761f, str)) {
                    break;
                }
            }
            if (!zC && (navControllerViewModel = h5.f12682p) != null) {
                navControllerViewModel.clear(str);
            }
        } else if (!zC) {
            navControllerViewModel.clear(str);
        }
        h5.w();
        ArrayList arrayListT2 = h5.t();
        c0Var2.getClass();
        c0Var2.k(null, arrayListT2);
    }

    public final void c(C1137l popUpTo, boolean z6) {
        kotlin.jvm.internal.o.h(popUpTo, "popUpTo");
        H h5 = this.f12778h;
        X xB = h5.f12688v.b(popUpTo.f12757b.f12639a);
        h5.f12692z.put(popUpTo, Boolean.valueOf(z6));
        if (!xB.equals(this.f12777g)) {
            Object obj = h5.f12689w.get(xB);
            kotlin.jvm.internal.o.e(obj);
            ((C1139n) obj).c(popUpTo, z6);
            return;
        }
        C1140o c1140o = h5.f12691y;
        if (c1140o != null) {
            c1140o.invoke(popUpTo);
            d(popUpTo);
            return;
        }
        C0104e c0104e = new C0104e(this, popUpTo, z6);
        C3249o c3249o = h5.f12674g;
        int iIndexOf = c3249o.indexOf(popUpTo);
        if (iIndexOf < 0) {
            Log.i("NavController", "Ignoring pop of " + popUpTo + " as it was not found on the current back stack");
            return;
        }
        int i6 = iIndexOf + 1;
        if (i6 != c3249o.size()) {
            h5.q(true, ((C1137l) c3249o.get(i6)).f12757b.f12644f, false);
        }
        H.s(h5, popUpTo);
        c0104e.invoke();
        h5.x();
        h5.b();
    }

    public final void d(C1137l popUpTo) {
        kotlin.jvm.internal.o.h(popUpTo, "popUpTo");
        ReentrantLock reentrantLock = this.f12771a;
        reentrantLock.lock();
        try {
            c0 c0Var = this.f12772b;
            Iterable iterable = (Iterable) c0Var.getValue();
            ArrayList arrayList = new ArrayList();
            for (Object obj : iterable) {
                if (kotlin.jvm.internal.o.c((C1137l) obj, popUpTo)) {
                    break;
                } else {
                    arrayList.add(obj);
                }
            }
            c0Var.getClass();
            c0Var.k(null, arrayList);
            reentrantLock.unlock();
        } catch (Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    public final void e(C1137l popUpTo, boolean z6) {
        Object objPrevious;
        kotlin.jvm.internal.o.h(popUpTo, "popUpTo");
        c0 c0Var = this.f12773c;
        Iterable iterable = (Iterable) c0Var.getValue();
        boolean z7 = iterable instanceof Collection;
        Y4.J j4 = this.f12775e;
        if (!z7 || !((Collection) iterable).isEmpty()) {
            Iterator it = iterable.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                if (((C1137l) it.next()) == popUpTo) {
                    Iterable iterable2 = (Iterable) ((c0) j4.f8871a).getValue();
                    if ((iterable2 instanceof Collection) && ((Collection) iterable2).isEmpty()) {
                        return;
                    }
                    Iterator it2 = iterable2.iterator();
                    while (it2.hasNext()) {
                        if (((C1137l) it2.next()) == popUpTo) {
                        }
                    }
                    return;
                }
            }
        }
        c0Var.k(null, AbstractC3233J.t((Set) c0Var.getValue(), popUpTo));
        List list = (List) ((c0) j4.f8871a).getValue();
        ListIterator listIterator = list.listIterator(list.size());
        while (true) {
            if (!listIterator.hasPrevious()) {
                objPrevious = null;
                break;
            }
            objPrevious = listIterator.previous();
            C1137l c1137l = (C1137l) objPrevious;
            if (!kotlin.jvm.internal.o.c(c1137l, popUpTo)) {
                Y4.H h5 = j4.f8871a;
                if (((List) ((c0) h5).getValue()).lastIndexOf(c1137l) < ((List) ((c0) h5).getValue()).lastIndexOf(popUpTo)) {
                    break;
                }
            }
        }
        C1137l c1137l2 = (C1137l) objPrevious;
        if (c1137l2 != null) {
            c0Var.k(null, AbstractC3233J.t((Set) c0Var.getValue(), c1137l2));
        }
        c(popUpTo, z6);
    }

    /* JADX WARN: Type inference failed for: r0v7, types: [J4.c, kotlin.jvm.internal.p] */
    public final void f(C1137l backStackEntry) {
        kotlin.jvm.internal.o.h(backStackEntry, "backStackEntry");
        H h5 = this.f12778h;
        X xB = h5.f12688v.b(backStackEntry.f12757b.f12639a);
        if (!xB.equals(this.f12777g)) {
            Object obj = h5.f12689w.get(xB);
            if (obj == null) {
                throw new IllegalStateException(com.google.android.recaptcha.internal.a.n(new StringBuilder("NavigatorBackStack for "), backStackEntry.f12757b.f12639a, " should already be created").toString());
            }
            ((C1139n) obj).f(backStackEntry);
            return;
        }
        ?? r02 = h5.f12690x;
        if (r02 != 0) {
            r02.invoke(backStackEntry);
            a(backStackEntry);
        } else {
            Log.i("NavController", "Ignoring add of destination " + backStackEntry.f12757b + " outside of the call to navigate(). ");
        }
    }
}
