package com.ss.android.ugc.aweme.influencer.ecommercelive.framework.settings;

import X.0Zk;
import X.6iy;
import X.8pz;
import X.AnonymousClass2ZV;
import X.C05492Zf;
import X.C09102hy;
import X.C09112hz;
import X.C09122i0;
import X.C09132i1;
import X.C15482sG;
import X.C221837i;
import X.C24503Gg;
import X.C65674r1;
import X.CwZ;
import X.Cwu;
import X.D8s;
import X.DAe;
import X.DAj;
import X.L1H;
import X.LWw;
import X.Lf3;
import X.M12;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import com.bytedance.android.monitorV2.HybridMultiMonitor;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.router.RouteIntent;
import com.bytedance.router.interceptor.IInterceptor;
import com.google.gson.j;
import com.google.gson.m;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import kotlin.h.b.n;
import org.json.JSONObject;

public final class TEARouterInterceptor implements IInterceptor {
    static {
        Covode.recordClassIndex(88419);
    }

    public final boolean matchInterceptRules(RouteIntent routeIntent) {
        Uri uri;
        List<String> list;
        if (routeIntent == null || (uri = routeIntent.getUri()) == null || uri.isOpaque() || n.LIZ(AnonymousClass2ZV.LIZ(uri, "disable_tea_intercept"), "1")) {
            return false;
        }
        C09122i0 LIZ = C09112hz.LIZ();
        if (LIZ.LIZ && (list = LIZ.LIZIZ) != null) {
            for (String LIZ2 : list) {
                if (LIZ(LIZ2, uri.toString())) {
                    return true;
                }
            }
        }
        return false;
    }

    private final boolean LIZ(String str, String str2) {
        if (str == null || str2 == null || n.LIZ(str, "") || n.LIZ(str2, "")) {
            return false;
        }
        return new Lf3(str).containsMatchIn(str2);
    }

    private final boolean LIZ(List<C09102hy> list, Uri uri) {
        if (list == null) {
            return true;
        }
        for (C09102hy r0 : list) {
            if (!LIZ(r0.LIZ, AnonymousClass2ZV.LIZ(uri, r0.LIZIZ))) {
                return false;
            }
        }
        return true;
    }

    private final boolean LIZ(C09132i1 r7, Uri uri) {
        List<String> list;
        String str;
        if (r7.LIZLLL == null) {
            return false;
        }
        if (AnonymousClass2ZV.LIZ(uri)) {
            List<C05492Zf> list2 = r7.LIZLLL.LIZ;
            if (list2 != null) {
                for (C05492Zf r2 : list2) {
                    if (LIZ(r2.LIZIZ, uri.getQueryParameter("channel")) && LIZ(r2.LIZ, uri.getQueryParameter("bundle")) && LIZ(r7.LIZIZ, uri)) {
                        return true;
                    }
                }
            }
        } else if (AnonymousClass2ZV.LIZIZ(uri) && (list = r7.LIZLLL.LIZIZ) != null) {
            for (String str2 : list) {
                Uri LIZJ = AnonymousClass2ZV.LIZJ(uri);
                if (LIZJ != null) {
                    str = LIZJ.getPath();
                } else {
                    str = null;
                }
                if (LIZ(str2, str) && LIZ(r7.LIZIZ, uri)) {
                    return true;
                }
            }
        }
        return false;
    }

    public final boolean onInterceptRoute(Context context, RouteIntent routeIntent) {
        Uri uri;
        List<C09132i1> list;
        MethodCollector.i(3285);
        Context context2 = context;
        if (context2 == null) {
            MethodCollector.o(3285);
            return false;
        }
        RouteIntent routeIntent2 = routeIntent;
        if (routeIntent2 == null || (uri = routeIntent2.getUri()) == null) {
            MethodCollector.o(3285);
            return false;
        }
        C09122i0 LIZ = C09112hz.LIZ();
        ArrayList arrayList = new ArrayList();
        if (n.LIZ(AnonymousClass2ZV.LIZ(uri, "need_tea_intercept"), "1") && (list = LIZ.LIZJ) != null) {
            for (C09132i1 r1 : list) {
                if (LIZ(r1, uri)) {
                    arrayList.add(r1);
                }
            }
        }
        List<C09132i1> list2 = LIZ.LIZLLL;
        if (list2 != null) {
            for (C09132i1 r12 : list2) {
                if (LIZ(r12, uri)) {
                    arrayList.add(r12);
                }
            }
        }
        Iterator it = arrayList.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            Object next = it.next();
            List<C09102hy> list3 = ((C09132i1) next).LIZJ;
            if (list3 != null && !list3.isEmpty()) {
                if (next != null) {
                    C221837i r11 = new C221837i(context2, (AttributeSet) null, 0, 6);
                    C15482sG r122 = new C15482sG();
                    r122.element = null;
                    if (context2 instanceof Activity) {
                        Window window = ((Activity) context2).getWindow();
                        n.LIZIZ(window, "");
                        View decorView = window.getDecorView();
                        n.LIZIZ(decorView, "");
                        T rootView = decorView.getRootView();
                        if (rootView != null) {
                            r122.element = rootView;
                            r11.setLayoutParams(new FrameLayout.LayoutParams(-2, -2, 17));
                            ((ViewGroup) r122.element).addView(r11);
                        } else {
                            NullPointerException nullPointerException = new NullPointerException("null cannot be cast to non-null type android.view.ViewGroup");
                            MethodCollector.o(3285);
                            throw nullPointerException;
                        }
                    }
                    DAe.LIZ(DAj.LIZ(8pz.LIZIZ), (D8s) null, new C24503Gg(this, r11, r122, arrayList, uri, context2, (C65674r1) null), 3);
                    MethodCollector.o(3285);
                    return true;
                }
            }
        }
        routeIntent2.setUrl(LIZ((List<C09132i1>) arrayList, uri, new m()));
        MethodCollector.o(3285);
        return false;
    }

    private final void LIZ(String str, Map<String, Object> map, Uri uri) {
        String str2;
        HybridMultiMonitor instance = HybridMultiMonitor.getInstance();
        L1H l1h = new L1H(str);
        if (AnonymousClass2ZV.LIZ(uri)) {
            str2 = "tiktok_ecom_affiliate";
        } else {
            str2 = "ecom_tiktok_h5";
        }
        l1h.LIZIZ = str2;
        l1h.LIZ(0);
        JSONObject jSONObject = new JSONObject();
        for (Map.Entry next : map.entrySet()) {
            jSONObject.put((String) next.getKey(), String.valueOf(next.getValue()));
        }
        l1h.LIZJ = jSONObject;
        instance.customReport(l1h.LIZ());
    }

    public final String LIZ(List<C09132i1> list, Uri uri, m mVar) {
        T t;
        Uri uri2;
        Map<String, String> LIZ;
        String str;
        String str2;
        String str3;
        Iterator<T> it = list.iterator();
        loop0:
        while (true) {
            if (!it.hasNext()) {
                t = null;
                break;
            }
            t = it.next();
            List<C09102hy> list2 = ((C09132i1) t).LIZJ;
            if (list2 == null) {
                break;
            }
            Iterator<T> it2 = list2.iterator();
            while (true) {
                if (!it2.hasNext()) {
                    break loop0;
                }
                C09102hy r0 = (C09102hy) it2.next();
                String str4 = r0.LIZ;
                j LIZJ = mVar.LIZJ(r0.LIZIZ);
                if (LIZJ == null || (str3 = LIZJ.toString()) == null) {
                    str3 = "";
                }
                if (!LIZ(str4, str3)) {
                }
            }
        }
        C09132i1 r5 = (C09132i1) t;
        if (r5 == null) {
            uri2 = uri;
        } else {
            String str5 = r5.LJ;
            if (str5 == null || str5.length() == 0) {
                uri2 = uri;
            } else {
                uri2 = Uri.parse(r5.LJ);
            }
            List<String> list3 = r5.LJFF;
            if (list3 != null && !list3.isEmpty()) {
                LinkedHashMap linkedHashMap = new LinkedHashMap();
                M12.LIZ(uri);
                Uri LIZJ2 = AnonymousClass2ZV.LIZJ(uri);
                if (LIZJ2 != null) {
                    LIZ = AnonymousClass2ZV.LIZLLL(LIZJ2);
                } else {
                    LIZ = LWw.LIZ();
                }
                linkedHashMap.putAll(LIZ);
                linkedHashMap.putAll(AnonymousClass2ZV.LIZLLL(uri));
                n.LIZIZ(uri2, "");
                LinkedHashMap linkedHashMap2 = new LinkedHashMap();
                for (Map.Entry entry : linkedHashMap.entrySet()) {
                    if (r5.LJFF.contains(entry.getKey())) {
                        linkedHashMap2.put(entry.getKey(), entry.getValue());
                    }
                }
                uri2 = AnonymousClass2ZV.LIZ(uri2, (Map<String, String>) linkedHashMap2);
            }
            List<String> list4 = r5.LJI;
            if (list4 != null && !list4.isEmpty()) {
                n.LIZIZ(uri2, "");
                uri2 = AnonymousClass2ZV.LIZ(uri2, r5.LJI);
            }
            n.LIZIZ(uri2, "");
        }
        Uri LIZ2 = AnonymousClass2ZV.LIZ(AnonymousClass2ZV.LIZ(uri2, (List<String>) 6iy.LIZ("need_tea_intercept")), (Map<String, String>) LWw.LIZ(new Cwu[]{CwZ.LIZ("disable_tea_intercept", "1"), CwZ.LIZ("tea_init_time", String.valueOf(System.currentTimeMillis()))}));
        if (r5 != null || n.LIZ(AnonymousClass2ZV.LIZ(uri, "need_tea_intercept"), "1")) {
            Cwu[] cwuArr = new Cwu[5];
            if (r5 != null) {
                str = "1";
            } else {
                str = "0";
            }
            cwuArr[0] = CwZ.LIZ("redirect_status", str);
            cwuArr[1] = CwZ.LIZ("is_native", "1");
            if (r5 == null || (str2 = r5.LIZ) == null) {
                str2 = "";
            }
            cwuArr[2] = CwZ.LIZ("interceptor_name", str2);
            cwuArr[3] = CwZ.LIZ("cur_schema", uri.toString());
            cwuArr[4] = CwZ.LIZ("redirect_schema", LIZ2);
            Map LIZIZ = LWw.LIZIZ(cwuArr);
            LIZIZ.putAll(AnonymousClass2ZV.LJ(uri));
            LIZIZ.putAll(0Zk.LIZ(mVar));
            LIZ("rd_tea_intercept", (Map<String, Object>) LIZIZ, uri);
        }
        Map LIZIZ2 = LWw.LIZIZ(new Cwu[]{CwZ.LIZ("status", "start")});
        LIZIZ2.putAll(AnonymousClass2ZV.LJ(LIZ2));
        LIZ("rd_tea_open", (Map<String, Object>) LIZIZ2, LIZ2);
        String uri3 = LIZ2.toString();
        n.LIZIZ(uri3, "");
        return uri3;
    }
}
