package com.bytedance.jedi.arch;

import X.0Bt;
import X.0Bz;
import X.0C1;
import X.0CB;
import X.0CH;
import X.0CN;
import X.7xU;
import X.C6584Cd2;
import X.Crj;
import X.Ls2;
import X.M12;
import androidx.fragment.app.Fragment;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.Serializable;
import kotlin.h.b.n;

public class lifecycleAwareLazy<T extends 0Bt> implements Serializable, Crj<T>, Crj {
    public volatile Object _value;
    public Ls2<? extends T> initializer;
    public final Ls2<String> keyFactory;
    public final lifecycleAwareLazy<T> lock;
    public final 0CH owner;

    static {
        Covode.recordClassIndex(34776);
    }

    public static /* synthetic */ void value$annotations() {
    }

    public void onStateChanged(0CH r2, 0CB r3) {
        if (r3 == 0CB.ON_CREATE) {
            onStart();
        }
    }

    public boolean isInitialized() {
        if (this._value != C6584Cd2.LIZ) {
            return true;
        }
        return false;
    }

    public String toString() {
        if (isInitialized()) {
            return getValue().toString();
        }
        return "Lazy value not initialized yet.";
    }

    @0CN(LIZ = 0CB.ON_CREATE)
    public final void onStart() {
        if (!isInitialized()) {
            getValue();
        }
        if (this.keyFactory == null) {
            this.owner.getLifecycle().LIZIZ(this);
        } else {
            LIZ(this.owner, getValue(), this.keyFactory);
        }
    }

    public T getValue() {
        T t;
        MethodCollector.i(4096);
        T t2 = this._value;
        if (t2 == C6584Cd2.LIZ) {
            synchronized (this.lock) {
                try {
                    T t3 = this._value;
                    if (t3 == C6584Cd2.LIZ) {
                        Ls2<? extends T> ls2 = this.initializer;
                        if (ls2 == null) {
                            n.LIZ();
                        }
                        t = (0Bt) ls2.invoke();
                        this._value = t;
                        this.initializer = null;
                    } else if (t3 != null) {
                        t = (0Bt) t3;
                    } else {
                        7xU r1 = new 7xU("null cannot be cast to non-null type T");
                        MethodCollector.o(4096);
                        throw r1;
                    }
                } finally {
                    MethodCollector.o(4096);
                }
            }
            return t;
        } else if (t2 != null) {
            T t4 = (0Bt) t2;
            MethodCollector.o(4096);
            return t4;
        } else {
            7xU r12 = new 7xU("null cannot be cast to non-null type T");
            MethodCollector.o(4096);
            throw r12;
        }
    }

    /* JADX INFO: this call moved to the top of the method (can break code semantics) */
    public lifecycleAwareLazy(0CH r2, Ls2<? extends T> ls2) {
        this(r2, (Ls2<String>) null, ls2);
        M12.LIZ(r2, ls2);
    }

    public lifecycleAwareLazy(0CH r3, Ls2<String> ls2, Ls2<? extends T> ls22) {
        M12.LIZ(r3, ls22);
        this.owner = r3;
        this.keyFactory = ls2;
        this.initializer = ls22;
        this._value = C6584Cd2.LIZ;
        this.lock = this;
        r3.getLifecycle().LIZ(this);
    }

    public void LIZ(0CH r4, T t, Ls2<String> ls2) {
        M12.LIZ(r4, t, ls2);
        Fragment fragment = (Fragment) r4;
        M12.LIZ(fragment, t, ls2);
        0Bz LIZ = 0C1.LIZ(fragment);
        n.LIZ(LIZ, "");
        String str = (String) ls2.invoke();
        if (LIZ.LIZ(str) == null) {
            LIZ.LIZ(str, t);
        }
    }
}
