package com.bytedance.sdk.openadsdk.core.YoT.Sg;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import androidx.core.app.NotificationCompat;
import com.amazon.a.a.o.b;
import com.bytedance.sdk.component.qsH.qsH;
import com.bytedance.sdk.component.utils.Ne;
import com.bytedance.sdk.openadsdk.core.YoT.Sg.Sg;
import com.bytedance.sdk.openadsdk.core.YoT.Sg.YFl;
import com.bytedance.sdk.openadsdk.core.lG;
import com.bytedance.sdk.openadsdk.core.model.Wwa;
import com.bytedance.sdk.openadsdk.utils.Sco;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.revenuecat.purchases.common.Constants;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONArray;
import org.json.JSONObject;
/* loaded from: classes2.dex */
public class tN {
    private boolean AlY;
    private EnumC0145tN Sg;
    private final String YFl;
    private boolean tN;
    private static final Map<String, Sg> wN = new ConcurrentHashMap();
    private static final AtomicBoolean vc = new AtomicBoolean(false);

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.bytedance.sdk.openadsdk.core.YoT.Sg.tN$tN  reason: collision with other inner class name */
    /* loaded from: classes2.dex */
    public enum EnumC0145tN {
        TRACKING_URL,
        QUARTILE_EVENT
    }

    public boolean AlY() {
        return this.tN;
    }

    public void j_() {
        this.AlY = true;
    }

    public String tN() {
        return this.YFl;
    }

    public boolean wN() {
        return this.AlY;
    }

    static {
        Ne.YFl(new Ne.YFl() { // from class: com.bytedance.sdk.openadsdk.core.YoT.Sg.tN.1
            @Override // com.bytedance.sdk.component.utils.Ne.YFl
            public void YFl(Context context, Intent intent, boolean z, int i) {
                if (i == 0 || tN.wN.size() <= 0) {
                    return;
                }
                tN.Sg();
            }
        }, lG.YFl());
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public tN(String str, EnumC0145tN enumC0145tN, Boolean bool) {
        this.YFl = str;
        this.Sg = enumC0145tN;
        this.tN = bool.booleanValue();
    }

    /* loaded from: classes2.dex */
    public static class YFl {
        private final String YFl;
        private EnumC0145tN Sg = EnumC0145tN.TRACKING_URL;
        private boolean tN = false;

        public YFl YFl(boolean z) {
            this.tN = z;
            return this;
        }

        public YFl(String str) {
            this.YFl = str;
        }

        public tN YFl() {
            return new tN(this.YFl, this.Sg, Boolean.valueOf(this.tN));
        }
    }

    public static List<String> YFl(List<tN> list, com.bytedance.sdk.openadsdk.core.YoT.YFl.YFl yFl, long j, String str) {
        if (list == null) {
            return new ArrayList();
        }
        ArrayList arrayList = new ArrayList(list.size());
        for (tN tNVar : list) {
            if (tNVar != null && (!tNVar.wN() || tNVar.AlY())) {
                arrayList.add(tNVar.tN());
                tNVar.j_();
            }
        }
        return new com.bytedance.sdk.openadsdk.core.YoT.tN.tN(arrayList).YFl(yFl).YFl(j).YFl(str).YFl();
    }

    public static void YFl(List<tN> list, com.bytedance.sdk.openadsdk.core.YoT.YFl.YFl yFl, long j, String str, Sg sg) {
        YFl(YFl(list, yFl, j, str), sg);
    }

    public static void Sg(List<tN> list, com.bytedance.sdk.openadsdk.core.YoT.YFl.YFl yFl, long j, String str) {
        YFl(list, yFl, j, str, null);
    }

    public static void YFl(List<String> list, Sg sg) {
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            if (!TextUtils.isEmpty(str)) {
                YFl(str, sg, false);
            }
        }
    }

    private static void YFl(final String str, final Sg sg, final boolean z) {
        com.bytedance.sdk.component.DSW.Sg.Sg tN = com.bytedance.sdk.openadsdk.lG.tN.YFl().Sg().tN();
        if (tN == null) {
            return;
        }
        tN.YFl(true);
        tN.Sg(str);
        tN.YFl(new com.bytedance.sdk.component.DSW.YFl.YFl() { // from class: com.bytedance.sdk.openadsdk.core.YoT.Sg.tN.2
            @Override // com.bytedance.sdk.component.DSW.YFl.YFl
            public void YFl(com.bytedance.sdk.component.DSW.Sg.tN tNVar, com.bytedance.sdk.component.DSW.Sg sg2) {
                boolean z2;
                Sg sg3 = Sg.this;
                if (sg3 == null || sg3.Sg == null) {
                    return;
                }
                String str2 = null;
                if (sg2 == null || !sg2.vc()) {
                    z2 = false;
                    if (sg2 != null) {
                        str2 = sg2.YFl() + Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR + sg2.Sg();
                        if (!z && (sg2.YFl() <= 300 || sg2.YFl() >= 400)) {
                            tN.wN.put(str, Sg.this);
                        }
                    }
                } else {
                    z2 = true;
                }
                tN.Sg(z2, str2, Sco.tN(Sg.this.Sg.Uv()), Sg.this, str, z);
                if (sg2 == null || sg2.YFl() != 200 || tN.wN.size() <= 0) {
                    return;
                }
                tN.Sg();
            }

            @Override // com.bytedance.sdk.component.DSW.YFl.YFl
            public void YFl(com.bytedance.sdk.component.DSW.Sg.tN tNVar, IOException iOException) {
                Sg sg2 = Sg.this;
                if (sg2 != null && sg2.Sg != null) {
                    tN.Sg(false, iOException != null ? iOException.getMessage() : null, Sco.tN(Sg.this.Sg.Uv()), Sg.this, str, z);
                }
                if (z || Sg.this == null) {
                    return;
                }
                tN.wN.put(str, Sg.this);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void Sg() {
        if (vc.compareAndSet(false, true)) {
            Map<String, Sg> map = wN;
            HashSet<Map.Entry> hashSet = new HashSet(map.entrySet());
            map.clear();
            for (Map.Entry entry : hashSet) {
                if (entry != null) {
                    YFl((String) entry.getKey(), (Sg) entry.getValue(), true);
                }
            }
            vc.set(false);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void Sg(final boolean z, final String str, final String str2, final Sg sg, final String str3, final boolean z2) {
        com.bytedance.sdk.openadsdk.AlY.tN.YFl(new qsH("dsp_track_link_result") { // from class: com.bytedance.sdk.openadsdk.core.YoT.Sg.tN.3
            @Override // java.lang.Runnable
            public void run() {
                JSONObject jSONObject = new JSONObject();
                try {
                    jSONObject.put("type", sg.YFl);
                    jSONObject.put("success", z);
                    if (!TextUtils.isEmpty(str)) {
                        jSONObject.put(b.c, str);
                    }
                    jSONObject.put("url", str3);
                    if (sg.tN >= 0.0f) {
                        jSONObject.put(NotificationCompat.CATEGORY_PROGRESS, Math.round(sg.tN * 100.0f) / 100.0d);
                    }
                    if (z2) {
                        jSONObject.put("retry", true);
                    }
                } catch (Throwable unused) {
                }
                com.bytedance.sdk.openadsdk.AlY.tN.Sg(sg.Sg, str2, "dsp_track_link_result", jSONObject);
            }
        });
    }

    public static JSONArray YFl(List<tN> list) {
        JSONArray jSONArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            jSONArray.put(list.get(i).tN());
        }
        return jSONArray;
    }

    public static List<tN> YFl(JSONArray jSONArray) {
        return YFl(jSONArray, false);
    }

    public static List<tN> YFl(JSONArray jSONArray, boolean z) {
        ArrayList arrayList = new ArrayList();
        if (jSONArray != null) {
            for (int i = 0; i < jSONArray.length(); i++) {
                String optString = jSONArray.optString(i);
                if (!TextUtils.isEmpty(optString)) {
                    arrayList.add(new YFl(optString).YFl(z).YFl());
                }
            }
        }
        return arrayList;
    }

    public static List<com.bytedance.sdk.openadsdk.core.YoT.Sg.Sg> Sg(JSONArray jSONArray) {
        ArrayList arrayList = new ArrayList();
        if (jSONArray != null) {
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject optJSONObject = jSONArray.optJSONObject(i);
                if (optJSONObject != null) {
                    arrayList.add(new Sg.YFl(optJSONObject.optString("content"), (float) optJSONObject.optDouble("trackingFraction", FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE)).YFl());
                }
            }
        }
        return arrayList;
    }

    public static List<com.bytedance.sdk.openadsdk.core.YoT.Sg.YFl> tN(JSONArray jSONArray) {
        ArrayList arrayList = new ArrayList();
        if (jSONArray != null) {
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject optJSONObject = jSONArray.optJSONObject(i);
                if (optJSONObject != null) {
                    arrayList.add(new YFl.C0144YFl(optJSONObject.optString("content"), optJSONObject.optLong("trackingMilliseconds", 0L)).YFl());
                }
            }
        }
        return arrayList;
    }

    /* loaded from: classes2.dex */
    public static class Sg {
        Wwa Sg;
        String YFl;
        float tN;

        public Sg(String str, Wwa wwa) {
            this(str, wwa, -1.0f);
        }

        public Sg(String str, Wwa wwa, float f) {
            this.YFl = str;
            this.Sg = wwa;
            this.tN = f;
        }
    }
}
