package com.ss.android.ugc.aweme.relation.base.list;

import X.3PM;
import X.3Pg;
import X.4JY;
import X.4gw;
import X.4gy;
import X.4h6;
import X.4h7;
import X.4r1;
import X.7AW;
import X.A4l;
import X.Amw;
import X.Amx;
import X.Amy;
import X.Amz;
import X.An0;
import X.An1;
import X.An7;
import X.C0402Cvm;
import X.C0782DAc;
import X.C0849DCr;
import X.C1481DaP;
import X.C1483DaR;
import X.C1486DaU;
import X.C1508Daq;
import X.C1647Dd5;
import X.C1676DdY;
import X.Cv9;
import X.D8E;
import X.DAU;
import X.DB2;
import X.DBT;
import X.DCZ;
import X.DD1;
import X.DWI;
import X.DWM;
import X.DZ8;
import X.DZA;
import X.DZB;
import X.DZD;
import X.DZJ;
import X.DZK;
import X.LeS;
import X.Lea;
import X.LsC;
import X.M12;
import com.bytedance.covode.number.Covode;
import com.bytedance.ext_power_list.AssemListViewModel;
import com.ss.android.ugc.aweme.relation.base.list.cell.BaseCellVM;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import kotlin.h.b.n;

public abstract class RelationBaseAssemListVM<S extends DWI<S, ITEM>, Type, ITEM extends C1481DaP, PARAM extends DZK<KEY>, KEY> extends AssemListViewModel<S, ITEM, PARAM> {
    public final Set<String> LIZ;
    public C1676DdY<C1481DaP> LIZIZ;
    public volatile DZD LIZJ;
    public volatile 3Pg<? extends A4l<PARAM>> LIZLLL;
    public boolean LJ;
    public final D8E LJFF;
    public final List<Amy> LJI;
    public final Map<String, BaseCellVM<?, ?>> LJII;
    public final DZB LJIIIIZZ;
    public final C1483DaR<PARAM> LJIIIZ;
    public final DB2 LJIIJ;
    public final DZJ<Type, KEY, PARAM> LJIIJJI;

    static {
        Covode.recordClassIndex(102708);
    }

    public abstract LsC<Type, ITEM> LIZ();

    public abstract void LIZIZ(Amy amy);

    public Object onRefresh(4r1<? super A4l<PARAM>> r2) {
        return LIZ(this, r2);
    }

    public final void LIZIZ() {
        setState(new C0402Cvm(this));
        this.LJI.clear();
        this.LIZ.clear();
        this.LJI.add(Amx.LIZ);
        this.LJIIJJI.LIZIZ();
    }

    public void manualListRefresh() {
        DBT unused = C0782DAc.LIZ(getAssemVMScope(), this.LJIIJ, (DAU) null, new DZ8(this, (4r1) null), 2);
    }

    public void onCleared() {
        DCZ<? super A4l<T>> dcz;
        this.LJIIIIZZ.LIZJ();
        C1483DaR<PARAM> daR = this.LJIIIZ;
        if (!(daR == null || (dcz = daR.LIZJ) == null)) {
            dcz.LIZIZ((Throwable) null);
        }
        3Pg<? extends A4l<PARAM>> r0 = this.LIZLLL;
        if (r0 != null) {
            r0.LIZ((CancellationException) null);
        }
        for (BaseCellVM destroy : this.LJII.values()) {
            destroy.destroy();
        }
        3PM.LIZIZ("UserCard_Base", "component is destroy!");
        super.onCleared();
    }

    public void onPrepared() {
        super.onPrepared();
        C1483DaR<PARAM> daR = this.LJIIIZ;
        if (daR != null) {
            Cv9 assemVMScope = getAssemVMScope();
            C1486DaU config = getConfig();
            M12.LIZ(assemVMScope, config);
            daR.LIZ = assemVMScope;
            daR.LIZIZ = config;
            Field declaredField = C1647Dd5.class.getDeclaredField("LIZLLL");
            n.LIZIZ(declaredField, "");
            declaredField.setAccessible(true);
            declaredField.set(getConfig(), daR);
        }
    }

    public final void LIZ(Amy amy) {
        Object LJIIJ2 = Lea.LJIIJ(this.LJI);
        3PM.LIZJ("UserCard_Base", "component old:(" + LJIIJ2 + "), new:(" + amy + ')');
        if (n.LIZ(LJIIJ2, An7.LIZ)) {
            throw new IllegalStateException(("UserCard_Base, " + getClass().getSimpleName() + " has destroy!").toString());
        } else if (!n.LIZ(getState().LIZ(), amy)) {
            this.LJI.add(amy);
            runOnUIThread(new DZA(this, amy));
        }
    }

    public void newState(DWM<ITEM> dwm) {
        int i;
        int i2;
        An1 an1;
        An0 an0;
        An0 an02;
        M12.LIZ(dwm);
        DWM listState = getState().getListState();
        Amz LIZ2 = getState().LIZ();
        List<ITEM> list = listState.LIZLLL;
        boolean z = false;
        if (list != null) {
            i = list.size();
        } else {
            i = 0;
        }
        4JY r7 = dwm.LIZ;
        4JY r4 = dwm.LIZIZ;
        List<ITEM> list2 = dwm.LIZLLL;
        if (list2 != null) {
            i2 = list2.size();
        } else {
            i2 = 0;
        }
        Amw amw = null;
        if (!n.LIZ(listState.LIZ, r7)) {
            if (r7 instanceof 4h7) {
                if (!this.LJ) {
                    if ((LIZ2 instanceof Amx) || ((LIZ2 instanceof Amz) && LIZ2.LIZ())) {
                        an1 = new An1(An0.FIRST_LOAD);
                    } else if (!(LIZ2 instanceof An1) || LIZ2.LIZJ()) {
                        an1 = new An1(An0.ON_REFRESH);
                    } else {
                        3PM.LIZJ("UserCard_Base", "illegal loading state. cur:".concat(String.valueOf(LIZ2)));
                    }
                }
                an1 = null;
            } else if (!(r7 instanceof 4JY)) {
                if (r7 instanceof 4gw) {
                    if (this.LJ) {
                        this.LJ = false;
                    } else {
                        if (!(LIZ2 instanceof An1)) {
                            LIZ2 = null;
                        }
                        An1 an12 = LIZ2;
                        if (an12 == null || (an0 = an12.LIZ) == null) {
                            an0 = An0.FIRST_LOAD;
                        }
                        an1 = new Amz(r7.LIZ, an0);
                    }
                } else if (r7 instanceof 4gy) {
                    3PM.LIZJ("UserCard_Base", "refresh state want update to Uninitialized!");
                } else {
                    throw new 7AW();
                }
                an1 = null;
            } else if (this.LJ) {
                this.LJ = false;
                an1 = null;
            } else {
                if (!(LIZ2 instanceof An1)) {
                    LIZ2 = null;
                }
                An1 an13 = LIZ2;
                if (an13 != null) {
                    an02 = an13.LIZ;
                } else {
                    an02 = null;
                }
                if (an02 == An0.FIRST_LOAD) {
                    z = true;
                }
                an1 = new Amw(i2, z, !((C1508Daq) r7.LIZ).LIZIZ);
            }
            3PM.LIZ("UserCard_Base", "refresh: " + 4h6.LIZ(r7) + ", " + this.LIZJ);
            if (an1 != null) {
                LIZ((Amy) an1);
            }
            if ((r7 instanceof 4JY) || (r7 instanceof 4gw)) {
                this.LJFF.LIZ((Object) null);
            }
        } else if (!n.LIZ(listState.LIZIZ, r4)) {
            if (r4 instanceof 4h7) {
                amw = new An1(An0.ON_LOAD_MORE);
            } else if (!(LIZ2 instanceof An1) || !LIZ2.LIZJ()) {
                3PM.LIZJ("UserCard_Base", "component loading state illegal!, cur: ".concat(String.valueOf(LIZ2)));
            } else if (r4 instanceof 4JY) {
                amw = new Amw(i2, !((C1508Daq) r4.LIZ).LIZIZ);
            } else if (r4 instanceof 4gw) {
                amw = new Amz(r4.LIZ, An0.ON_LOAD_MORE);
            } else if (r4 instanceof 4gy) {
                3PM.LIZJ("UserCard_Base", "loadMoreState state want update to Uninitialized!");
            } else {
                throw new 7AW();
            }
            3PM.LIZ("UserCard_Base", "loadMore: " + 4h6.LIZ(r4));
            if (amw != null) {
                LIZ((Amy) amw);
            }
        } else if (!(LIZ2 instanceof Amw) || i2 == i) {
            3PM.LIZJ("UserCard_Base", "assem ListState error!");
        } else {
            LIZ((Amy) new Amw(i2, false, ((Amw) LIZ2).LIZJ));
        }
    }

    public /* synthetic */ Object onLoadMore(Object obj, 4r1 r3) {
        return LIZ(this, (DZK) obj, r3);
    }

    /* JADX WARNING: Removed duplicated region for block: B:12:0x0045  */
    /* JADX WARNING: Removed duplicated region for block: B:7:0x001f  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static /* synthetic */ java.lang.Object LIZ(com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM r8, X.4r1 r9) {
        /*
            boolean r0 = r9 instanceof X.DZH
            if (r0 == 0) goto L_0x008c
            r4 = r9
            X.DZH r4 = (X.DZH) r4
            int r0 = r4.label
            r1 = -2147483648(0xffffffff80000000, float:-0.0)
            r0 = r0 & r1
            if (r0 == 0) goto L_0x008c
            int r0 = r4.label
            int r0 = r0 - r1
            r4.label = r0
        L_0x0013:
            java.lang.Object r6 = r4.result
            X.Cof r5 = X.C0213Cof.COROUTINE_SUSPENDED
            int r0 = r4.label
            r3 = 2
            r7 = 1
            java.lang.String r2 = "UserCard_Base"
            if (r0 == 0) goto L_0x0045
            if (r0 == r7) goto L_0x009a
            if (r0 != r3) goto L_0x0092
            java.lang.Object r8 = r4.L$0
            com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM r8 = (com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM) r8
            X.2th.LIZ(r6)
        L_0x002a:
            X.DbP r6 = (X.C1543DbP) r6
            java.lang.String r1 = java.lang.String.valueOf(r6)
            java.lang.String r0 = "onRefresh, "
            java.lang.String r0 = r0.concat(r1)
            X.3PM.LIZIZ(r2, r0)
            X.DZB r1 = r8.LJIIIIZZ
            X.DZC r0 = new X.DZC
            r0.<init>(r8, r6)
            java.lang.Object r0 = r1.LIZ(r0)
            return r0
        L_0x0045:
            X.2th.LIZ(r6)
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "refresh preload step: "
            r1.<init>(r0)
            X.DZD r0 = r8.LIZJ
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.3PM.LIZIZ(r2, r0)
            X.DZD r1 = r8.LIZJ
            X.DZD r0 = X.DZD.WAIT_GET_RESULT
            if (r1 != r0) goto L_0x0070
            X.3Pg<? extends X.A4l<PARAM>> r0 = r8.LIZLLL
            if (r0 == 0) goto L_0x007a
            r4.L$0 = r8
            r4.label = r7
            java.lang.Object r6 = r0.LIZ(r4)
            if (r6 != r5) goto L_0x00a1
            return r5
        L_0x0070:
            X.DZD r1 = r8.LIZJ
            X.DZD r0 = X.DZD.PRELOADING
            if (r1 != r0) goto L_0x007a
            X.DZD r0 = X.DZD.WAIT_GET_RESULT
            r8.LIZJ = r0
        L_0x007a:
            X.DZJ<Type, KEY, PARAM> r0 = r8.LJIIJJI
            r0.LIZIZ()
            X.DZJ<Type, KEY, PARAM> r0 = r8.LJIIJJI
            r4.L$0 = r8
            r4.label = r3
            java.lang.Object r6 = r0.LIZ(r0.LIZ(), r4)
            if (r6 != r5) goto L_0x002a
            return r5
        L_0x008c:
            X.DZH r4 = new X.DZH
            r4.<init>(r8, r9)
            goto L_0x0013
        L_0x0092:
            java.lang.IllegalStateException r1 = new java.lang.IllegalStateException
            java.lang.String r0 = "call to 'resume' before 'invoke' with coroutine"
            r1.<init>(r0)
            throw r1
        L_0x009a:
            java.lang.Object r8 = r4.L$0
            com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM r8 = (com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM) r8
            X.2th.LIZ(r6)
        L_0x00a1:
            r0 = 0
            r8.LIZLLL = r0
            X.DZD r0 = X.DZD.NONE
            r8.LIZJ = r0
            java.lang.String r1 = java.lang.String.valueOf(r6)
            java.lang.String r0 = "get preload result: "
            java.lang.String r0 = r0.concat(r1)
            X.3PM.LIZIZ(r2, r0)
            return r6
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM.LIZ(com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM, X.4r1):java.lang.Object");
    }

    public /* synthetic */ RelationBaseAssemListVM(DZJ dzj, DZB dzb, C1483DaR daR) {
        this(dzj, dzb, daR, C0849DCr.LIZLLL);
    }

    /* JADX WARNING: Removed duplicated region for block: B:11:0x0040  */
    /* JADX WARNING: Removed duplicated region for block: B:7:0x001c  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static /* synthetic */ java.lang.Object LIZ(com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM r5, X.DZK r6, X.4r1 r7) {
        /*
            boolean r0 = r7 instanceof X.DZG
            if (r0 == 0) goto L_0x0050
            r4 = r7
            X.DZG r4 = (X.DZG) r4
            int r0 = r4.label
            r1 = -2147483648(0xffffffff80000000, float:-0.0)
            r0 = r0 & r1
            if (r0 == 0) goto L_0x0050
            int r0 = r4.label
            int r0 = r0 - r1
            r4.label = r0
        L_0x0013:
            java.lang.Object r3 = r4.result
            X.Cof r2 = X.C0213Cof.COROUTINE_SUSPENDED
            int r0 = r4.label
            r1 = 1
            if (r0 == 0) goto L_0x0040
            if (r0 != r1) goto L_0x0056
            java.lang.Object r5 = r4.L$0
            com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM r5 = (com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM) r5
            X.2th.LIZ(r3)
        L_0x0025:
            X.DbP r3 = (X.C1543DbP) r3
            java.lang.String r1 = java.lang.String.valueOf(r3)
            java.lang.String r0 = "onLoadMore, "
            java.lang.String r1 = r0.concat(r1)
            java.lang.String r0 = "UserCard_Base"
            X.3PM.LIZIZ(r0, r1)
            X.LsC r1 = r5.LIZ()
            r0 = 0
            X.A4l r0 = X.C1574Dbu.LIZ(r3, r0, r1)
            return r0
        L_0x0040:
            X.2th.LIZ(r3)
            X.DZJ<Type, KEY, PARAM> r0 = r5.LJIIJJI
            r4.L$0 = r5
            r4.label = r1
            java.lang.Object r3 = r0.LIZ(r6, r4)
            if (r3 != r2) goto L_0x0025
            return r2
        L_0x0050:
            X.DZG r4 = new X.DZG
            r4.<init>(r5, r7)
            goto L_0x0013
        L_0x0056:
            java.lang.IllegalStateException r1 = new java.lang.IllegalStateException
            java.lang.String r0 = "call to 'resume' before 'invoke' with coroutine"
            r1.<init>(r0)
            throw r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM.LIZ(com.ss.android.ugc.aweme.relation.base.list.RelationBaseAssemListVM, X.DZK, X.4r1):java.lang.Object");
    }

    public RelationBaseAssemListVM(DZJ<Type, KEY, PARAM> dzj, DZB dzb, C1483DaR<PARAM> daR, DB2 db2) {
        M12.LIZ(dzj, dzb, db2);
        this.LJIIJJI = dzj;
        this.LJIIIIZZ = dzb;
        this.LJIIIZ = daR;
        this.LJIIJ = db2;
        this.LIZ = new LinkedHashSet();
        this.LIZJ = DZD.NONE;
        this.LJFF = DD1.LIZ();
        this.LJI = LeS.LIZJ(new Amy[]{Amx.LIZ});
        this.LJII = new LinkedHashMap();
    }
}
