package androidx.lifecycle;

import androidx.lifecycle.n;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import q.b;

/* loaded from: classes.dex */
public class w extends n {

    /* renamed from: k, reason: collision with root package name */
    public static final a f4904k = new a(null);

    /* renamed from: b, reason: collision with root package name */
    private final boolean f4905b;

    /* renamed from: c, reason: collision with root package name */
    private q.a f4906c;

    /* renamed from: d, reason: collision with root package name */
    private n.b f4907d;

    /* renamed from: e, reason: collision with root package name */
    private final WeakReference f4908e;

    /* renamed from: f, reason: collision with root package name */
    private int f4909f;

    /* renamed from: g, reason: collision with root package name */
    private boolean f4910g;

    /* renamed from: h, reason: collision with root package name */
    private boolean f4911h;

    /* renamed from: i, reason: collision with root package name */
    private ArrayList f4912i;

    /* renamed from: j, reason: collision with root package name */
    private final wp.t f4913j;

    /* loaded from: classes.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(kotlin.jvm.internal.k kVar) {
            this();
        }

        public final n.b a(n.b state1, n.b bVar) {
            kotlin.jvm.internal.t.g(state1, "state1");
            return (bVar == null || bVar.compareTo(state1) >= 0) ? state1 : bVar;
        }
    }

    /* loaded from: classes.dex */
    public static final class b {

        /* renamed from: a, reason: collision with root package name */
        private n.b f4914a;

        /* renamed from: b, reason: collision with root package name */
        private r f4915b;

        public b(t tVar, n.b initialState) {
            kotlin.jvm.internal.t.g(initialState, "initialState");
            kotlin.jvm.internal.t.d(tVar);
            this.f4915b = z.f(tVar);
            this.f4914a = initialState;
        }

        public final void a(u uVar, n.a event) {
            kotlin.jvm.internal.t.g(event, "event");
            n.b targetState = event.getTargetState();
            this.f4914a = w.f4904k.a(this.f4914a, targetState);
            r rVar = this.f4915b;
            kotlin.jvm.internal.t.d(uVar);
            rVar.f(uVar, event);
            this.f4914a = targetState;
        }

        public final n.b b() {
            return this.f4914a;
        }
    }

    private w(u uVar, boolean z10) {
        this.f4905b = z10;
        this.f4906c = new q.a();
        n.b bVar = n.b.INITIALIZED;
        this.f4907d = bVar;
        this.f4912i = new ArrayList();
        this.f4908e = new WeakReference(uVar);
        this.f4913j = wp.j0.a(bVar);
    }

    private final void e(u uVar) {
        Iterator descendingIterator = this.f4906c.descendingIterator();
        kotlin.jvm.internal.t.f(descendingIterator, "observerMap.descendingIterator()");
        while (descendingIterator.hasNext() && !this.f4911h) {
            Map.Entry entry = (Map.Entry) descendingIterator.next();
            kotlin.jvm.internal.t.f(entry, "next()");
            t tVar = (t) entry.getKey();
            b bVar = (b) entry.getValue();
            while (bVar.b().compareTo(this.f4907d) > 0 && !this.f4911h && this.f4906c.contains(tVar)) {
                n.a a10 = n.a.Companion.a(bVar.b());
                if (a10 == null) {
                    throw new IllegalStateException("no event down from " + bVar.b());
                }
                m(a10.getTargetState());
                bVar.a(uVar, a10);
                l();
            }
        }
    }

    private final n.b f(t tVar) {
        b bVar;
        Map.Entry o10 = this.f4906c.o(tVar);
        n.b bVar2 = null;
        n.b b10 = (o10 == null || (bVar = (b) o10.getValue()) == null) ? null : bVar.b();
        if (!this.f4912i.isEmpty()) {
            bVar2 = (n.b) this.f4912i.get(r0.size() - 1);
        }
        a aVar = f4904k;
        return aVar.a(aVar.a(this.f4907d, b10), bVar2);
    }

    private final void g(String str) {
        if (!this.f4905b || x.a()) {
            return;
        }
        throw new IllegalStateException(("Method " + str + " must be called on the main thread").toString());
    }

    private final void h(u uVar) {
        b.d i10 = this.f4906c.i();
        kotlin.jvm.internal.t.f(i10, "observerMap.iteratorWithAdditions()");
        while (i10.hasNext() && !this.f4911h) {
            Map.Entry entry = (Map.Entry) i10.next();
            t tVar = (t) entry.getKey();
            b bVar = (b) entry.getValue();
            while (bVar.b().compareTo(this.f4907d) < 0 && !this.f4911h && this.f4906c.contains(tVar)) {
                m(bVar.b());
                n.a c10 = n.a.Companion.c(bVar.b());
                if (c10 == null) {
                    throw new IllegalStateException("no event up from " + bVar.b());
                }
                bVar.a(uVar, c10);
                l();
            }
        }
    }

    private final boolean j() {
        if (this.f4906c.size() == 0) {
            return true;
        }
        Map.Entry e10 = this.f4906c.e();
        kotlin.jvm.internal.t.d(e10);
        n.b b10 = ((b) e10.getValue()).b();
        Map.Entry k10 = this.f4906c.k();
        kotlin.jvm.internal.t.d(k10);
        n.b b11 = ((b) k10.getValue()).b();
        return b10 == b11 && this.f4907d == b11;
    }

    private final void k(n.b bVar) {
        n.b bVar2 = this.f4907d;
        if (bVar2 == bVar) {
            return;
        }
        if (!((bVar2 == n.b.INITIALIZED && bVar == n.b.DESTROYED) ? false : true)) {
            throw new IllegalStateException(("State must be at least CREATED to move to " + bVar + ", but was " + this.f4907d + " in component " + this.f4908e.get()).toString());
        }
        this.f4907d = bVar;
        if (this.f4910g || this.f4909f != 0) {
            this.f4911h = true;
            return;
        }
        this.f4910g = true;
        o();
        this.f4910g = false;
        if (this.f4907d == n.b.DESTROYED) {
            this.f4906c = new q.a();
        }
    }

    private final void l() {
        this.f4912i.remove(r1.size() - 1);
    }

    private final void m(n.b bVar) {
        this.f4912i.add(bVar);
    }

    private final void o() {
        u uVar = (u) this.f4908e.get();
        if (uVar == null) {
            throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is already garbage collected. It is too late to change lifecycle state.");
        }
        while (!j()) {
            this.f4911h = false;
            n.b bVar = this.f4907d;
            Map.Entry e10 = this.f4906c.e();
            kotlin.jvm.internal.t.d(e10);
            if (bVar.compareTo(((b) e10.getValue()).b()) < 0) {
                e(uVar);
            }
            Map.Entry k10 = this.f4906c.k();
            if (!this.f4911h && k10 != null && this.f4907d.compareTo(((b) k10.getValue()).b()) > 0) {
                h(uVar);
            }
        }
        this.f4911h = false;
        this.f4913j.setValue(b());
    }

    @Override // androidx.lifecycle.n
    public void a(t observer) {
        u uVar;
        kotlin.jvm.internal.t.g(observer, "observer");
        g("addObserver");
        n.b bVar = this.f4907d;
        n.b bVar2 = n.b.DESTROYED;
        if (bVar != bVar2) {
            bVar2 = n.b.INITIALIZED;
        }
        b bVar3 = new b(observer, bVar2);
        if (((b) this.f4906c.m(observer, bVar3)) == null && (uVar = (u) this.f4908e.get()) != null) {
            boolean z10 = this.f4909f != 0 || this.f4910g;
            n.b f10 = f(observer);
            this.f4909f++;
            while (bVar3.b().compareTo(f10) < 0 && this.f4906c.contains(observer)) {
                m(bVar3.b());
                n.a c10 = n.a.Companion.c(bVar3.b());
                if (c10 == null) {
                    throw new IllegalStateException("no event up from " + bVar3.b());
                }
                bVar3.a(uVar, c10);
                l();
                f10 = f(observer);
            }
            if (!z10) {
                o();
            }
            this.f4909f--;
        }
    }

    @Override // androidx.lifecycle.n
    public n.b b() {
        return this.f4907d;
    }

    @Override // androidx.lifecycle.n
    public void d(t observer) {
        kotlin.jvm.internal.t.g(observer, "observer");
        g("removeObserver");
        this.f4906c.n(observer);
    }

    public void i(n.a event) {
        kotlin.jvm.internal.t.g(event, "event");
        g("handleLifecycleEvent");
        k(event.getTargetState());
    }

    public void n(n.b state) {
        kotlin.jvm.internal.t.g(state, "state");
        g("setCurrentState");
        k(state);
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public w(u provider) {
        this(provider, true);
        kotlin.jvm.internal.t.g(provider, "provider");
    }
}
