package com.ss.android.ugc.aweme.ecommerce.combinepayment.payment;

import X.2HN;
import X.2Pr;
import X.3SC;
import X.3TX;
import X.4r1;
import X.5Am;
import X.6iy;
import X.8pz;
import X.9HM;
import X.C4394Bio;
import X.C4417BjB;
import X.C4448Bjg;
import X.C4453Bjl;
import X.C4455Bjn;
import X.C4456Bjo;
import X.C4469Bk1;
import X.C4485BkH;
import X.C4489BkL;
import X.C4492BkO;
import X.C4493BkP;
import X.C4495BkR;
import X.C4497BkT;
import X.C4498BkU;
import X.C4499BkV;
import X.C4500BkW;
import X.C4501BkX;
import X.C4509Bkf;
import X.C4517Bkn;
import X.C4534Bl4;
import X.C4555BlP;
import X.C4558BlS;
import X.C4584Bls;
import X.C4589Blx;
import X.C4590Bly;
import X.C4591Blz;
import X.C4651Bmx;
import X.C4662Bn8;
import X.C4682BnS;
import X.C4822Bpi;
import X.DAe;
import X.Hwn;
import X.LWw;
import X.LeS;
import X.Lea;
import X.Lel;
import X.LsC;
import X.M12;
import X.Mk2;
import X.Mk3;
import X.PhE;
import X.PlI;
import com.bytedance.covode.number.Covode;
import com.bytedance.jedi.arch.JediViewModel;
import com.ss.android.ugc.aweme.ecommerce.address.dto.Address;
import com.ss.android.ugc.aweme.ecommerce.address.dto.Region;
import com.ss.android.ugc.aweme.ecommerce.api.model.Price;
import com.ss.android.ugc.aweme.ecommerce.combinepayment.payment.api.PaymentApi;
import com.ss.android.ugc.aweme.ecommerce.combinepayment.payment.dto.BalanceRequest;
import com.ss.android.ugc.aweme.ecommerce.combinepayment.payment.dto.BindInfoItem;
import com.ss.android.ugc.aweme.ecommerce.combinepayment.payment.dto.BindInfoReqInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import kotlin.h.b.n;

public final class PaymentViewModel extends JediViewModel<PaymentState> implements Hwn {
    public static List<? extends Object> LJIILIIL = Lel.INSTANCE;
    public static final C4469Bk1 LJIILJJIL = new C4469Bk1((byte) 0);
    public LsC<? super C4662Bn8, 2Pr> LIZ = C4500BkW.LIZ;
    public Address LIZIZ;
    public List<C4497BkT> LIZJ;
    public C4584Bls LIZLLL;
    public boolean LJ = true;
    public final Map<String, Map<String, List<C4651Bmx>>> LJFF = new LinkedHashMap();
    public List<? extends Object> LJI;
    public final Map<String, BindInfoItem> LJII = new LinkedHashMap();
    public String LJIIJJI;
    public HashMap<String, Object> LJIIL;
    public final Map<String, C4590Bly> LJIILL = new LinkedHashMap();
    public final List<BindInfoItem> LJIILLIIL = new ArrayList();

    public final void LIZ(String str, String str2) {
        M12.LIZ(str, str2);
        if (str.hashCode() == -1413704395 && str.equals("ec_resend_pay_request")) {
            LIZJ(C4498BkU.LIZ);
        }
    }

    public final void LIZ(C4591Blz blz, String str, List<C4651Bmx> list) {
        M12.LIZ(blz, str, list);
        Map map = this.LJFF.get(n.LIZ(blz.LIZ, blz.LJIILLIIL));
        if (map == null) {
            map = new LinkedHashMap();
        }
        this.LJFF.put(n.LIZ(blz.LIZ, blz.LJIILLIIL), map);
        map.put(str, list);
    }

    public final void LIZ(C4591Blz blz, boolean z, LsC<? super String, 2Pr> lsC) {
        C4591Blz blz2 = blz;
        if (blz2 != null) {
            BindInfoReqInfo[] bindInfoReqInfoArr = new BindInfoReqInfo[1];
            String str = blz2.LIZ;
            String str2 = blz2.LJIILLIIL;
            String uuid = UUID.randomUUID().toString();
            C4499BkV LJFF2 = blz2.LJFF();
            bindInfoReqInfoArr[0] = new BindInfoReqInfo(str, str2, uuid, LJFF2 != null ? LJFF2.LIZ : null, "snssdk1180://ec/app_bind_result");
            DAe.LIZ(3SC.LIZ, 8pz.LIZIZ, new C4555BlP(this, LeS.LIZLLL(bindInfoReqInfoArr), z, blz2, lsC, (4r1) null), 2);
        }
    }

    static {
        Covode.recordClassIndex(69288);
    }

    public final /* synthetic */ 5Am dU_() {
        return new PaymentState((C4591Blz) null, (List) null, (C4534Bl4) null, (String) null, false, (C4534Bl4) null, (C4534Bl4) null, false, (Object) null, (Object) null, 1023, (2HN) null);
    }

    public final void LIZ(boolean z) {
        LIZJ(new C4453Bjl(this, z));
    }

    public final void LIZIZ(boolean z) {
        LIZJ(new C4489BkL(z));
    }

    public final C4590Bly LIZ(C4591Blz blz) {
        M12.LIZ(blz);
        return this.LJIILL.get(n.LIZ(blz.LIZ, blz.LJIILLIIL));
    }

    public final boolean LIZIZ(C4591Blz blz) {
        T t;
        C4591Blz blz2;
        List<? extends Object> list = LJIILIIL;
        if (list == null || list.isEmpty() || blz == null) {
            return false;
        }
        Iterator<T> it = LJIILIIL.iterator();
        while (true) {
            String str = null;
            if (!it.hasNext()) {
                break;
            }
            T next = it.next();
            if (!(next instanceof C4485BkH)) {
                t = null;
            } else {
                t = next;
            }
            C4485BkH bkH = (C4485BkH) t;
            if (!(bkH == null || (blz2 = bkH.LIZ) == null)) {
                str = blz2.LIZ;
            }
            if (n.LIZ(str, blz.LIZ)) {
                if (next != null) {
                    return true;
                }
            }
        }
        return false;
    }

    public final void LIZ(C4517Bkn bkn) {
        List<C4591Blz> list;
        3TX r0;
        List<C4591Blz> list2;
        3TX r02;
        ArrayList arrayList = new ArrayList();
        C4509Bkf bkf = bkn.LIZLLL;
        if (!(bkf == null || (list2 = bkf.LIZ) == null)) {
            for (C4591Blz blz : list2) {
                3TX r2 = 3TX.BALANCE_QUERY_FAILED;
                C4493BkP bkP = blz.LJIJJ;
                if (bkP != null) {
                    r02 = bkP.LJ;
                } else {
                    r02 = null;
                }
                if (r2 == r02) {
                    arrayList.add(new C4589Blx(blz.LIZ, blz.LJIILLIIL, blz.LIZIZ(), (Boolean) null, (String) null));
                }
            }
        }
        C4509Bkf bkf2 = bkn.LIZLLL;
        if (!(bkf2 == null || (list = bkf2.LIZLLL) == null)) {
            for (C4591Blz blz2 : list) {
                3TX r22 = 3TX.BALANCE_QUERY_FAILED;
                C4493BkP bkP2 = blz2.LJIJJ;
                if (bkP2 != null) {
                    r0 = bkP2.LJ;
                } else {
                    r0 = null;
                }
                if (r22 == r0) {
                    arrayList.add(new C4589Blx(blz2.LIZ, blz2.LJIILLIIL, blz2.LIZIZ(), (Boolean) null, (String) null));
                }
            }
        }
        if (bkn.LJIIJ != null && (!arrayList.isEmpty())) {
            String str = bkn.LJIIJ;
            String str2 = bkn.LJIIJJI;
            if (str2 == null) {
                str2 = "";
            }
            BalanceRequest balanceRequest = new BalanceRequest(arrayList, str, str2);
            M12.LIZ(balanceRequest);
            ((PaymentApi) C4558BlS.LIZ.LIZ(PaymentApi.class)).getBalance(balanceRequest).LIZIZ(PhE.LIZIZ(PlI.LIZJ)).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZ(new C4492BkO(this, bkn), C4501BkX.LIZ);
        }
    }

    public final int LIZ(C4591Blz blz, List<? extends Object> list) {
        T t;
        T t2;
        M12.LIZ(blz, list);
        Iterator<T> it = list.iterator();
        while (true) {
            t = null;
            if (!it.hasNext()) {
                t2 = null;
                break;
            }
            t2 = it.next();
            if ((t2 instanceof C4485BkH) && C4822Bpi.LIZ(((C4485BkH) t2).LIZ, blz)) {
                break;
            }
        }
        if (t2 instanceof C4485BkH) {
            t = t2;
        }
        C4485BkH bkH = (C4485BkH) t;
        if (bkH != null) {
            return bkH.LIZJ;
        }
        return 0;
    }

    public final List<C4651Bmx> LIZIZ(C4591Blz blz, String str) {
        M12.LIZ(blz, str);
        Map map = this.LJFF.get(n.LIZ(blz.LIZ, blz.LJIILLIIL));
        if (map == null) {
            map = new LinkedHashMap();
        }
        List<C4651Bmx> list = (List) map.get(str);
        if (list == null) {
            return Lel.INSTANCE;
        }
        return list;
    }

    public final String LIZ(C4591Blz blz, String str) {
        List<C4497BkT> list;
        Price price;
        String priceStr;
        M12.LIZ(str);
        T t = null;
        if (blz == null || blz.LIZ == null || (list = this.LIZJ) == null) {
            return str;
        }
        Iterator<T> it = list.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            T next = it.next();
            if (n.LIZ(((C4497BkT) next).LIZ, blz.LIZ)) {
                t = next;
                break;
            }
        }
        C4497BkT bkT = (C4497BkT) t;
        if (bkT == null || (price = bkT.LIZIZ) == null || (priceStr = price.getPriceStr()) == null) {
            return str;
        }
        return priceStr;
    }

    public final List<C4651Bmx> LIZ(List<C4682BnS> list, Address address) {
        Lel LIZ2;
        int i;
        C4682BnS bnS;
        C4651Bmx bmx;
        String str;
        String str2;
        String str3;
        Address address2 = address;
        List<Region> list2 = address2.LIZLLL;
        int i2 = 0;
        if (list2 == null || list2.isEmpty() || address2.LIZJ == null) {
            return Lel.INSTANCE;
        }
        List LJII2 = Lea.LJII(address2.LIZLLL);
        LJII2.add(0, address2.LIZJ);
        List<C4682BnS> list3 = list;
        if (list3 == null) {
            return Lel.INSTANCE;
        }
        int i3 = 10;
        ArrayList arrayList = new ArrayList(9HM.LIZ(list3, 10));
        for (C4682BnS bnS2 : list3) {
            String str4 = bnS2.LIZ;
            if (str4 == null) {
                LIZ2 = Lel.INSTANCE;
            } else {
                int hashCode = str4.hashCode();
                if (hashCode != -304863367) {
                    if (hashCode != 1981981399) {
                        if (hashCode == 2002908406 && str4.equals("eg_ccdc_global_billing_address_street_mobile")) {
                            LIZ2 = 6iy.LIZ(new C4651Bmx(bnS2.LIZ, bnS2.LJFF, address2.LIZ("address"), bnS2.LJIIJJI, (Boolean) null, 16, (2HN) null));
                        }
                    } else if (str4.equals("eg_ccdc_global_billing_address")) {
                        List<C4682BnS> list4 = bnS2.LJIIIZ;
                        if (list4 != null) {
                            i = list4.size();
                        } else {
                            i = 0;
                        }
                        List subList = LJII2.subList(i2, i);
                        LIZ2 = new ArrayList(9HM.LIZ(subList, i3));
                        int i4 = 0;
                        for (Object next : subList) {
                            int i5 = i4 + 1;
                            if (i4 < 0) {
                                LeS.LIZIZ();
                            }
                            Region region = (Region) next;
                            List<C4682BnS> list5 = bnS2.LJIIIZ;
                            Boolean bool = null;
                            if (list5 != null) {
                                bnS = list5.get(i4);
                            } else {
                                bnS = null;
                            }
                            if (i4 == 0) {
                                if (bnS != null) {
                                    str3 = bnS.LIZ;
                                } else {
                                    str3 = null;
                                }
                                if (n.LIZ(str3, "eg_ccdc_global_billing_address_country_regin")) {
                                    bmx = new C4651Bmx(bnS.LIZ, bnS.LJFF, n.LIZ(C4495BkR.LIZ.LIZ(region.LIZIZ), "," + region.LIZ + ',' + region.LIZIZ), bnS.LJIIJJI, (Boolean) null, 16, (2HN) null);
                                    LIZ2.add(bmx);
                                    i4 = i5;
                                }
                            }
                            if (bnS != null) {
                                str = bnS.LIZ;
                                str2 = bnS.LJFF;
                            } else {
                                str = null;
                                str2 = null;
                            }
                            String str5 = region.LIZ;
                            if (bnS != null) {
                                bool = bnS.LJIIJJI;
                            }
                            bmx = new C4651Bmx(str, str2, str5, bool, (Boolean) null, 16, (2HN) null);
                            LIZ2.add(bmx);
                            i4 = i5;
                        }
                    }
                } else if (str4.equals("eg_ccdc_global_billing_address_postal_code")) {
                    LIZ2 = 6iy.LIZ(new C4651Bmx(bnS2.LIZ, bnS2.LJFF, address2.LIZ("zipcode"), bnS2.LJIIJJI, (Boolean) null, 16, (2HN) null));
                }
                LIZ2 = 6iy.LIZ(new C4651Bmx(bnS2.LIZ, bnS2.LJFF, address2.LIZ(bnS2.LIZ), bnS2.LJIIJJI, (Boolean) null, 16, (2HN) null));
            }
            arrayList.add(LIZ2);
            i2 = 0;
            i3 = 10;
        }
        return 9HM.LIZIZ(arrayList);
    }

    public final void LIZ(C4591Blz blz, C4590Bly bly) {
        M12.LIZ(blz);
        this.LJIILL.put(n.LIZ(blz.LIZ, blz.LJIILLIIL), bly);
    }

    public final String LIZ(C4394Bio bio, C4591Blz blz, List<C4651Bmx> list) {
        M12.LIZ(bio, blz, list);
        String LIZ2 = C4448Bjg.LIZ.LIZ(bio.LIZJ, blz, list);
        if (!n.LIZ(LIZ2, "system_error")) {
            return LIZ2;
        }
        LIZJ(C4456Bjo.LIZ);
        return "";
    }

    public final Map<String, String> LIZ(C4417BjB bjB, C4591Blz blz, List<C4651Bmx> list) {
        Map<String, String> LIZLLL2 = LWw.LIZLLL(C4448Bjg.LIZ.LIZIZ(bjB.LIZJ, blz, list));
        boolean z = false;
        for (Map.Entry next : LIZLLL2.entrySet()) {
            if (n.LIZ(next.getValue(), "system_error")) {
                LIZLLL2.put(next.getKey(), "");
                z = true;
            }
        }
        if (z) {
            LIZJ(C4455Bjn.LIZ);
        }
        return LIZLLL2;
    }
}
