package com.bytedance.sdk.component.wsy.lb;

import android.net.Uri;
import android.text.TextUtils;
import defpackage.e;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: loaded from: classes.dex */
public class lb {
    private static final lb fm = new lb();
    private final Object ro = new Object();

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private volatile List<String> f6695lb = new ArrayList();
    private final ConcurrentHashMap<String, Integer> yz = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Long> jnr = new ConcurrentHashMap<>();
    private volatile boolean ajl = false;
    private volatile int wsy = 10;
    private volatile long wu = 1800000;

    private lb() {
    }

    public static lb fm() {
        return fm;
    }

    private List<String> ro(String str, List<String> list) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(str);
        if (list != null) {
            arrayList.addAll(list);
        }
        return arrayList;
    }

    public void fm(jnr jnrVar) {
        if (this.ajl == jnrVar.fm && this.wsy == jnrVar.ro && this.wu == jnrVar.f6694lb) {
            return;
        }
        this.ajl = jnrVar.fm;
        this.wsy = jnrVar.ro;
        this.wu = jnrVar.f6694lb;
        String.format("Config updated: enable=%b, K=%d, Cooldown=%dms", Boolean.valueOf(this.ajl), Integer.valueOf(this.wsy), Long.valueOf(this.wu));
    }

    private boolean ro(List<String> list, String str) {
        if (!list.contains(str)) {
            list.add(str);
            return true;
        }
        if (((String) e.c(list, 1)).equals(str)) {
            return false;
        }
        list.remove(str);
        list.add(str);
        return true;
    }

    private String fm(String str) {
        if (str == null) {
            return null;
        }
        try {
            Uri uri = Uri.parse(str);
            String host = uri.getHost();
            String path = uri.getPath();
            if (host != null && path != null) {
                if (path.isEmpty()) {
                    path = "/";
                }
                return host.concat(path);
            }
        } catch (Exception unused) {
        }
        return null;
    }

    private String fm(String str, String str2, List<String> list) {
        if (str.equals(fm(str2))) {
            return str2;
        }
        for (String str3 : list) {
            if (str.equals(fm(str3))) {
                return str3;
            }
        }
        return null;
    }

    public List<String> fm(String str, List<String> list) {
        if (list == null || list.isEmpty()) {
            ArrayList arrayList = new ArrayList();
            arrayList.add(str);
            return arrayList;
        }
        if (!this.ajl) {
            return ro(str, list);
        }
        String strFm = fm(str);
        if (strFm == null) {
            return ro(str, list);
        }
        ArrayList arrayList2 = new ArrayList();
        ArrayList arrayList3 = new ArrayList();
        List<String> list2 = this.f6695lb;
        Iterator<String> it = list2.iterator();
        while (it.hasNext()) {
            String strFm2 = fm(it.next(), str, list);
            if (strFm2 != null && !arrayList2.contains(strFm2)) {
                arrayList2.add(strFm2);
            }
        }
        for (String str2 : list) {
            if (!list2.contains(fm(str2)) && !arrayList2.contains(str2)) {
                arrayList3.add(str2);
            }
        }
        if (!arrayList2.isEmpty() && !arrayList3.isEmpty()) {
            arrayList2.addAll(1, arrayList3);
        } else if (arrayList2.isEmpty() && !arrayList3.isEmpty()) {
            arrayList2.addAll(arrayList3);
        }
        LinkedHashSet linkedHashSet = new LinkedHashSet(arrayList2);
        linkedHashSet.add(str);
        linkedHashSet.addAll(list);
        ArrayList arrayList4 = new ArrayList(linkedHashSet);
        Long l10 = this.jnr.get(strFm);
        long jCurrentTimeMillis = System.currentTimeMillis();
        if ((l10 == null || jCurrentTimeMillis >= l10.longValue()) && arrayList4.remove(str)) {
            arrayList4.add(0, str);
        }
        arrayList4.toString();
        return arrayList4;
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x00c2 A[Catch: all -> 0x004c, LOOP:0: B:46:0x00c2->B:48:0x00ca, LOOP_START, TryCatch #0 {all -> 0x004c, blocks: (B:20:0x0031, B:23:0x003c, B:46:0x00c2, B:48:0x00ca, B:49:0x00d4, B:50:0x00d6, B:26:0x004f, B:28:0x005d, B:30:0x0065, B:31:0x006a, B:35:0x007a, B:37:0x0089, B:34:0x0076, B:39:0x00a1, B:41:0x00af, B:43:0x00b7, B:44:0x00bc), top: B:55:0x0031 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void fm(String str, String str2, boolean z10) {
        boolean zRo;
        if ((z10 || com.bytedance.sdk.component.wsy.fm.wsy()) && this.ajl && !TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
            String strFm = fm(str);
            String strFm2 = fm(str2);
            if (strFm == null || strFm2 == null) {
                return;
            }
            boolean zEquals = str.equals(str2);
            synchronized (this.ro) {
                try {
                    ArrayList arrayList = new ArrayList(this.f6695lb);
                    if (zEquals) {
                        if (z10) {
                            this.yz.remove(strFm2);
                            this.jnr.remove(strFm2);
                            zRo = fm(arrayList, strFm2);
                        } else {
                            long jCurrentTimeMillis = System.currentTimeMillis();
                            Long l10 = this.jnr.get(strFm2);
                            if (l10 != null && jCurrentTimeMillis < l10.longValue()) {
                                zRo = ro(arrayList, strFm2);
                            } else {
                                Integer num = this.yz.get(strFm2);
                                int iIntValue = (num == null ? 0 : num.intValue()) + 1;
                                this.yz.put(strFm2, Integer.valueOf(iIntValue));
                                if (iIntValue >= this.wsy) {
                                    this.jnr.put(strFm2, Long.valueOf(jCurrentTimeMillis + this.wu));
                                    zRo = ro(arrayList, strFm2);
                                    this.yz.remove(strFm2);
                                }
                            }
                        }
                        if (zRo) {
                            while (arrayList.size() > 15) {
                                arrayList.remove(arrayList.size() - 1);
                            }
                            this.f6695lb = arrayList;
                        }
                    } else {
                        if (z10) {
                            Long l11 = this.jnr.get(strFm2);
                            long jCurrentTimeMillis2 = System.currentTimeMillis();
                            if (l11 != null && l11.longValue() > jCurrentTimeMillis2) {
                                zRo = fm(arrayList, strFm);
                            }
                        } else {
                            zRo = ro(arrayList, strFm);
                        }
                        if (zRo) {
                        }
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    private boolean fm(List<String> list, String str) {
        if (!list.isEmpty() && list.get(0).equals(str)) {
            return false;
        }
        list.remove(str);
        list.add(0, str);
        return true;
    }
}
