package com.bytedance.assem.jedi_vm.viewModel;

import X.0CB;
import X.0CC;
import X.0CH;
import X.1Bj;
import X.2HN;
import X.2Pr;
import X.2cN;
import X.7r1;
import X.C1326DVa;
import X.C1330DVe;
import X.CuX;
import X.CwN;
import X.DVV;
import X.DVX;
import X.DVY;
import X.DVZ;
import X.LsC;
import X.M12;
import X.Pfd;
import X.PgX;
import X.PhE;
import com.bytedance.covode.number.Covode;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

public final class LifecycleAwareObserver<T> extends AtomicReference<2cN> implements 1Bj, 2cN, 7r1<T> {
    public final boolean alwaysDeliverLastValueWhenActivate;
    public final boolean force;
    public final AtomicBoolean isActive;
    public final boolean isUsedInReusedScene;
    public T lastValue;
    public 0CH owner;
    public 7r1<T> sourceObserver;
    public T undeliveredValue;

    static {
        Covode.recordClassIndex(24077);
    }

    public final void onComplete() {
        requireSourceObserver().onComplete();
    }

    public final boolean isDisposed() {
        if (get() == C1330DVe.LIZ) {
            return true;
        }
        return false;
    }

    public final void dispose() {
        2cN r0;
        Object obj = get();
        2cN r1 = C1330DVe.LIZ;
        if (obj != r1 && (r0 = (2cN) getAndSet(r1)) != r1 && r0 != null) {
            r0.dispose();
        }
    }

    public final 0CH requireOwner() {
        0CH r0 = this.owner;
        if (r0 != null) {
            return r0;
        }
        throw new IllegalArgumentException("cannot access owner after destroy".toString());
    }

    public final 7r1<T> requireSourceObserver() {
        7r1<T> r0 = this.sourceObserver;
        if (r0 != null) {
            return r0;
        }
        throw new IllegalArgumentException("cannot access observer after destroy".toString());
    }

    public final void onError(Throwable th) {
        M12.LIZ(th);
        if (!isDisposed()) {
            lazySet(C1330DVe.LIZ);
            requireSourceObserver().onError(th);
        }
    }

    public final void onNext(T t) {
        if (this.force) {
            requireSourceObserver().onNext(t);
        } else if (this.isActive.get()) {
            requireSourceObserver().onNext(t);
        } else {
            this.undeliveredValue = t;
        }
        this.lastValue = t;
    }

    public final void onSubscribe(2cN r3) {
        M12.LIZ(r3);
        if (!compareAndSet((Object) null, r3)) {
            r3.dispose();
            if (get() != C1330DVe.LIZ) {
                PhE.LIZ(new C1326DVa("Disposable already set!"));
            }
        } else if (CwN.LIZ()) {
            requireOwner().getLifecycle().LIZ(this);
            requireSourceObserver().onSubscribe(this);
        } else {
            CwN.LIZ.post(new DVV(this));
        }
    }

    public final void onStateChanged(0CH r5, 0CB r6) {
        boolean z;
        T t;
        M12.LIZ(r5, r6);
        if (r5.getLifecycle().LIZ().isAtLeast(0CC.STARTED)) {
            if (r5 instanceof CuX) {
                z = ((CuX) r5).LJII();
            } else {
                z = true;
            }
            if (!this.isActive.getAndSet(true) && !isDisposed()) {
                if (z || !this.alwaysDeliverLastValueWhenActivate) {
                    t = this.undeliveredValue;
                } else {
                    t = this.undeliveredValue;
                    if (t == null) {
                        t = this.lastValue;
                    }
                }
                this.undeliveredValue = null;
                if (t != null) {
                    onNext(t);
                }
            }
        } else {
            this.isActive.set(false);
        }
        if (r6 != 0CB.ON_DESTROY) {
            return;
        }
        if (CwN.LIZ()) {
            requireOwner().getLifecycle().LIZIZ(this);
            if (!isDisposed()) {
                dispose();
            }
            this.owner = null;
            this.sourceObserver = null;
            return;
        }
        CwN.LIZ.post(new DVX(this));
    }

    public LifecycleAwareObserver(0CH r6, boolean z, boolean z2, boolean z3, LsC<? super T, 2Pr> lsC, LsC<? super Throwable, 2Pr> lsC2) {
        M12.LIZ(r6, lsC);
        this.alwaysDeliverLastValueWhenActivate = z;
        this.force = z2;
        this.isUsedInReusedScene = z3;
        this.owner = r6;
        this.sourceObserver = new Pfd(new DVZ(lsC), new DVY(lsC2), PgX.LIZJ, PgX.LIZLLL);
        this.isActive = new AtomicBoolean(false);
    }

    /* JADX INFO: this call moved to the top of the method (can break code semantics) */
    public /* synthetic */ LifecycleAwareObserver(0CH r8, boolean z, boolean z2, boolean z3, LsC lsC, LsC lsC2, int i, 2HN r15) {
        this(r8, (i & 2) != 0 ? false : z, (i & 4) != 0 ? false : z2, (i & 8) != 0 ? false : z3, lsC, (i & 32) != 0 ? null : lsC2);
    }
}
