package androidx.lifecycle;

import android.os.Looper;
import j.C2301a;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import k.C2365a;
import k.C2366b;
import k.C2367c;
import k.C2368d;
import q.AbstractC2761L;

/* JADX INFO: renamed from: androidx.lifecycle.y, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1115y extends r {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public C2365a f12190b = new C2365a();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public EnumC1108q f12191c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final WeakReference f12192d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f12193e;

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ArrayList f12196h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final Y4.c0 f12197i;

    public C1115y(InterfaceC1113w interfaceC1113w, boolean z6) {
        this.f12189a = z6;
        EnumC1108q enumC1108q = EnumC1108q.f12181b;
        this.f12191c = enumC1108q;
        this.f12196h = new ArrayList();
        this.f12192d = new WeakReference(interfaceC1113w);
        this.f12197i = Y4.O.c(enumC1108q);
    }

    public final EnumC1108q a(InterfaceC1112v interfaceC1112v) {
        HashMap map = this.f12190b.f19602e;
        C2367c c2367c = map.containsKey(interfaceC1112v) ? ((C2367c) map.get(interfaceC1112v)).f19609d : null;
        EnumC1108q enumC1108q = c2367c != null ? ((C1114x) c2367c.f19607b).f12187a : null;
        ArrayList arrayList = this.f12196h;
        EnumC1108q enumC1108q2 = arrayList.isEmpty() ? null : (EnumC1108q) AbstractC2761L.f(1, arrayList);
        EnumC1108q state1 = this.f12191c;
        kotlin.jvm.internal.o.h(state1, "state1");
        if (enumC1108q == null || enumC1108q.compareTo(state1) >= 0) {
            enumC1108q = state1;
        }
        return (enumC1108q2 == null || enumC1108q2.compareTo(enumC1108q) >= 0) ? enumC1108q : enumC1108q2;
    }

    @Override // androidx.lifecycle.r
    public final void addObserver(InterfaceC1112v observer) {
        InterfaceC1111u c1099h;
        Object obj;
        InterfaceC1113w interfaceC1113w;
        ArrayList arrayList = this.f12196h;
        kotlin.jvm.internal.o.h(observer, "observer");
        b("addObserver");
        EnumC1108q enumC1108q = this.f12191c;
        EnumC1108q enumC1108q2 = EnumC1108q.f12180a;
        if (enumC1108q != enumC1108q2) {
            enumC1108q2 = EnumC1108q.f12181b;
        }
        C1114x c1114x = new C1114x();
        HashMap map = AbstractC1116z.f12198a;
        boolean z6 = observer instanceof InterfaceC1111u;
        boolean z7 = observer instanceof DefaultLifecycleObserver;
        if (z6 && z7) {
            c1099h = new C1099h((DefaultLifecycleObserver) observer, (InterfaceC1111u) observer);
        } else if (z7) {
            c1099h = new C1099h((DefaultLifecycleObserver) observer, (InterfaceC1111u) null);
        } else if (z6) {
            c1099h = (InterfaceC1111u) observer;
        } else {
            Class<?> cls = observer.getClass();
            if (AbstractC1116z.b(cls) == 2) {
                Object obj2 = AbstractC1116z.f12199b.get(cls);
                kotlin.jvm.internal.o.e(obj2);
                List list = (List) obj2;
                if (list.size() == 1) {
                    AbstractC1116z.a((Constructor) list.get(0), observer);
                    throw null;
                }
                int size = list.size();
                InterfaceC1101j[] interfaceC1101jArr = new InterfaceC1101j[size];
                if (size > 0) {
                    AbstractC1116z.a((Constructor) list.get(0), observer);
                    throw null;
                }
                c1099h = new C1097f(interfaceC1101jArr, i);
            } else {
                c1099h = new C1099h(observer);
            }
        }
        c1114x.f12188b = c1099h;
        c1114x.f12187a = enumC1108q2;
        C2365a c2365a = this.f12190b;
        C2367c c2367cE = c2365a.e(observer);
        if (c2367cE != null) {
            obj = c2367cE.f19607b;
        } else {
            HashMap map2 = c2365a.f19602e;
            C2367c c2367c = new C2367c(observer, c1114x);
            c2365a.f19616d++;
            C2367c c2367c2 = c2365a.f19614b;
            if (c2367c2 == null) {
                c2365a.f19613a = c2367c;
                c2365a.f19614b = c2367c;
            } else {
                c2367c2.f19608c = c2367c;
                c2367c.f19609d = c2367c2;
                c2365a.f19614b = c2367c;
            }
            map2.put(observer, c2367c);
            obj = null;
        }
        if (((C1114x) obj) == null && (interfaceC1113w = (InterfaceC1113w) this.f12192d.get()) != null) {
            i = (this.f12193e != 0 || this.f12194f) ? 1 : 0;
            EnumC1108q enumC1108qA = a(observer);
            this.f12193e++;
            while (c1114x.f12187a.compareTo(enumC1108qA) < 0 && this.f12190b.f19602e.containsKey(observer)) {
                arrayList.add(c1114x.f12187a);
                C1105n c1105n = EnumC1107p.Companion;
                EnumC1108q state = c1114x.f12187a;
                c1105n.getClass();
                kotlin.jvm.internal.o.h(state, "state");
                int iOrdinal = state.ordinal();
                EnumC1107p enumC1107p = iOrdinal != 1 ? iOrdinal != 2 ? iOrdinal != 3 ? null : EnumC1107p.ON_RESUME : EnumC1107p.ON_START : EnumC1107p.ON_CREATE;
                if (enumC1107p == null) {
                    throw new IllegalStateException("no event up from " + c1114x.f12187a);
                }
                c1114x.a(interfaceC1113w, enumC1107p);
                arrayList.remove(arrayList.size() - 1);
                enumC1108qA = a(observer);
            }
            if (i == 0) {
                f();
            }
            this.f12193e--;
        }
    }

    public final void b(String str) {
        if (this.f12189a) {
            C2301a.Y().f19425n.getClass();
            if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
                throw new IllegalStateException(Z0.o.o("Method ", str, " must be called on the main thread").toString());
            }
        }
    }

    public final void c(EnumC1107p event) {
        kotlin.jvm.internal.o.h(event, "event");
        b("handleLifecycleEvent");
        d(event.a());
    }

    public final void d(EnumC1108q next) {
        if (this.f12191c == next) {
            return;
        }
        InterfaceC1113w interfaceC1113w = (InterfaceC1113w) this.f12192d.get();
        EnumC1108q current = this.f12191c;
        kotlin.jvm.internal.o.h(current, "current");
        kotlin.jvm.internal.o.h(next, "next");
        if (current == EnumC1108q.f12181b && next == EnumC1108q.f12180a) {
            throw new IllegalStateException(("State must be at least '" + EnumC1108q.f12182c + "' to be moved to '" + next + "' in component " + interfaceC1113w).toString());
        }
        EnumC1108q enumC1108q = EnumC1108q.f12180a;
        if (current == enumC1108q && current != next) {
            throw new IllegalStateException(("State is '" + enumC1108q + "' and cannot be moved to `" + next + "` in component " + interfaceC1113w).toString());
        }
        this.f12191c = next;
        if (this.f12194f || this.f12193e != 0) {
            this.f12195g = true;
            return;
        }
        this.f12194f = true;
        f();
        this.f12194f = false;
        if (this.f12191c == enumC1108q) {
            this.f12190b = new C2365a();
        }
    }

    public final void e(EnumC1108q state) {
        kotlin.jvm.internal.o.h(state, "state");
        b("setCurrentState");
        d(state);
    }

    /* JADX WARN: Code restructure failed: missing block: B:11:0x0030, code lost:
    
        r8.f12195g = false;
        r8.f12197i.j(r8.f12191c);
     */
    /* JADX WARN: Code restructure failed: missing block: B:12:0x0039, code lost:
    
        return;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void f() {
        InterfaceC1113w interfaceC1113w = (InterfaceC1113w) this.f12192d.get();
        if (interfaceC1113w == null) {
            throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is already garbage collected. It is too late to change lifecycle state.");
        }
        while (true) {
            C2365a c2365a = this.f12190b;
            if (c2365a.f19616d != 0) {
                C2367c c2367c = c2365a.f19613a;
                kotlin.jvm.internal.o.e(c2367c);
                EnumC1108q enumC1108q = ((C1114x) c2367c.f19607b).f12187a;
                C2367c c2367c2 = this.f12190b.f19614b;
                kotlin.jvm.internal.o.e(c2367c2);
                EnumC1108q enumC1108q2 = ((C1114x) c2367c2.f19607b).f12187a;
                if (enumC1108q == enumC1108q2 && this.f12191c == enumC1108q2) {
                    break;
                }
                this.f12195g = false;
                EnumC1108q enumC1108q3 = this.f12191c;
                C2367c c2367c3 = this.f12190b.f19613a;
                kotlin.jvm.internal.o.e(c2367c3);
                if (enumC1108q3.compareTo(((C1114x) c2367c3.f19607b).f12187a) < 0) {
                    C2365a c2365a2 = this.f12190b;
                    C2366b c2366b = new C2366b(c2365a2.f19614b, c2365a2.f19613a, 1);
                    c2365a2.f19615c.put(c2366b, Boolean.FALSE);
                    while (c2366b.hasNext() && !this.f12195g) {
                        Map.Entry entry = (Map.Entry) c2366b.next();
                        kotlin.jvm.internal.o.e(entry);
                        InterfaceC1112v interfaceC1112v = (InterfaceC1112v) entry.getKey();
                        C1114x c1114x = (C1114x) entry.getValue();
                        while (c1114x.f12187a.compareTo(this.f12191c) > 0 && !this.f12195g && this.f12190b.f19602e.containsKey(interfaceC1112v)) {
                            C1105n c1105n = EnumC1107p.Companion;
                            EnumC1108q enumC1108q4 = c1114x.f12187a;
                            c1105n.getClass();
                            EnumC1107p enumC1107pA = C1105n.a(enumC1108q4);
                            if (enumC1107pA == null) {
                                throw new IllegalStateException("no event down from " + c1114x.f12187a);
                            }
                            this.f12196h.add(enumC1107pA.a());
                            c1114x.a(interfaceC1113w, enumC1107pA);
                            ArrayList arrayList = this.f12196h;
                            arrayList.remove(arrayList.size() - 1);
                        }
                    }
                }
                C2367c c2367c4 = this.f12190b.f19614b;
                if (!this.f12195g && c2367c4 != null && this.f12191c.compareTo(((C1114x) c2367c4.f19607b).f12187a) > 0) {
                    C2365a c2365a3 = this.f12190b;
                    c2365a3.getClass();
                    C2368d c2368d = new C2368d(c2365a3);
                    c2365a3.f19615c.put(c2368d, Boolean.FALSE);
                    while (c2368d.hasNext() && !this.f12195g) {
                        Map.Entry entry2 = (Map.Entry) c2368d.next();
                        InterfaceC1112v interfaceC1112v2 = (InterfaceC1112v) entry2.getKey();
                        C1114x c1114x2 = (C1114x) entry2.getValue();
                        while (c1114x2.f12187a.compareTo(this.f12191c) < 0 && !this.f12195g && this.f12190b.f19602e.containsKey(interfaceC1112v2)) {
                            this.f12196h.add(c1114x2.f12187a);
                            C1105n c1105n2 = EnumC1107p.Companion;
                            EnumC1108q state = c1114x2.f12187a;
                            c1105n2.getClass();
                            kotlin.jvm.internal.o.h(state, "state");
                            int iOrdinal = state.ordinal();
                            EnumC1107p enumC1107p = iOrdinal != 1 ? iOrdinal != 2 ? iOrdinal != 3 ? null : EnumC1107p.ON_RESUME : EnumC1107p.ON_START : EnumC1107p.ON_CREATE;
                            if (enumC1107p == null) {
                                throw new IllegalStateException("no event up from " + c1114x2.f12187a);
                            }
                            c1114x2.a(interfaceC1113w, enumC1107p);
                            ArrayList arrayList2 = this.f12196h;
                            arrayList2.remove(arrayList2.size() - 1);
                        }
                    }
                }
            } else {
                break;
            }
        }
    }

    @Override // androidx.lifecycle.r
    public final EnumC1108q getCurrentState() {
        return this.f12191c;
    }

    @Override // androidx.lifecycle.r
    public final Y4.a0 getCurrentStateFlow() {
        return new Y4.J(this.f12197i);
    }

    @Override // androidx.lifecycle.r
    public final void removeObserver(InterfaceC1112v observer) {
        kotlin.jvm.internal.o.h(observer, "observer");
        b("removeObserver");
        this.f12190b.g(observer);
    }
}
