package pg;

import com.adobe.marketing.mobile.Event;
import com.adobe.marketing.mobile.ExtensionApi;
import com.adobe.marketing.mobile.SharedStateResolution;
import com.adobe.marketing.mobile.SharedStateResult;
import com.adobe.marketing.mobile.SharedStateStatus;
import com.adobe.marketing.mobile.VisitorID;
import com.adobe.marketing.mobile.internal.util.k;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import kotlin.jvm.internal.t;
import org.json.JSONException;
import org.json.JSONObject;
import rm.c0;
import sm.p0;
import sm.q0;
import sm.u;
import sm.v;

/* loaded from: classes4.dex */
public final class e {

    /* renamed from: a, reason: collision with root package name */
    private static final List f32641a;

    /* renamed from: b, reason: collision with root package name */
    public static final e f32642b = new e();

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public static final class a {

        /* renamed from: a, reason: collision with root package name */
        private final String f32643a;

        /* renamed from: b, reason: collision with root package name */
        private final String f32644b;

        /* renamed from: c, reason: collision with root package name */
        private final String f32645c;

        public a(String namespace, String value, String type) {
            t.g(namespace, "namespace");
            t.g(value, "value");
            t.g(type, "type");
            this.f32643a = namespace;
            this.f32644b = value;
            this.f32645c = type;
        }

        public final String a() {
            return this.f32643a;
        }

        public final String b() {
            return this.f32645c;
        }

        public final String c() {
            return this.f32644b;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof a)) {
                return false;
            }
            a aVar = (a) obj;
            return t.b(this.f32643a, aVar.f32643a) && t.b(this.f32644b, aVar.f32644b) && t.b(this.f32645c, aVar.f32645c);
        }

        public int hashCode() {
            String str = this.f32643a;
            int hashCode = (str != null ? str.hashCode() : 0) * 31;
            String str2 = this.f32644b;
            int hashCode2 = (hashCode + (str2 != null ? str2.hashCode() : 0)) * 31;
            String str3 = this.f32645c;
            return hashCode2 + (str3 != null ? str3.hashCode() : 0);
        }

        public String toString() {
            return "ID(namespace=" + this.f32643a + ", value=" + this.f32644b + ", type=" + this.f32645c + ")";
        }
    }

    static {
        List n10;
        n10 = u.n("com.adobe.module.analytics", "com.adobe.module.audience", "com.adobe.module.configuration", "com.adobe.module.target", "com.adobe.module.identity");
        f32641a = n10;
    }

    private e() {
    }

    private final List b(Event event, ExtensionApi extensionApi) {
        SharedStateResult e10 = e("com.adobe.module.analytics", event, extensionApi);
        ArrayList arrayList = new ArrayList();
        if (!h(e10)) {
            return arrayList;
        }
        String k10 = ch.b.k(e10 != null ? e10.b() : null, "aid", null);
        if (k10 != null) {
            if (k10.length() > 0) {
                arrayList.add(new a("AVID", k10, "integrationCode"));
            }
        }
        String k11 = ch.b.k(e10 != null ? e10.b() : null, "vid", null);
        if (k11 != null) {
            if (k11.length() > 0) {
                arrayList.add(new a("vid", k11, "analytics"));
            }
        }
        return arrayList;
    }

    private final List c(Event event, ExtensionApi extensionApi) {
        SharedStateResult e10 = e("com.adobe.module.audience", event, extensionApi);
        ArrayList arrayList = new ArrayList();
        if (!h(e10)) {
            return arrayList;
        }
        String k10 = ch.b.k(e10 != null ? e10.b() : null, "dpuuid", null);
        if (k10 != null) {
            if (k10.length() > 0) {
                String dpid = ch.b.k(e10 != null ? e10.b() : null, "dpid", "");
                t.f(dpid, "dpid");
                arrayList.add(new a(dpid, k10, "namespaceId"));
            }
        }
        String k11 = ch.b.k(e10 != null ? e10.b() : null, "uuid", null);
        if (k11 != null) {
            if (k11.length() > 0) {
                arrayList.add(new a("0", k11, "namespaceId"));
            }
        }
        return arrayList;
    }

    private final String d(Event event, ExtensionApi extensionApi) {
        SharedStateResult e10 = e("com.adobe.module.configuration", event, extensionApi);
        if (!h(e10)) {
            return null;
        }
        String k10 = ch.b.k(e10 != null ? e10.b() : null, "experienceCloud.org", null);
        if (k10 == null) {
            return null;
        }
        if (k10.length() > 0) {
            return k10;
        }
        return null;
    }

    private final SharedStateResult e(String str, Event event, ExtensionApi extensionApi) {
        return extensionApi.e(str, event, false, SharedStateResolution.ANY);
    }

    private final List f(Event event, ExtensionApi extensionApi) {
        ArrayList arrayList = new ArrayList();
        SharedStateResult e10 = e("com.adobe.module.target", event, extensionApi);
        if (!h(e10)) {
            return arrayList;
        }
        String k10 = ch.b.k(e10 != null ? e10.b() : null, "tntid", null);
        if (k10 != null) {
            if (k10.length() > 0) {
                arrayList.add(new a("tntid", k10, "target"));
            }
        }
        String k11 = ch.b.k(e10 != null ? e10.b() : null, "thirdpartyid", null);
        if (k11 != null) {
            if (k11.length() > 0) {
                arrayList.add(new a("3rdpartyid", k11, "target"));
            }
        }
        return arrayList;
    }

    private final List g(Event event, ExtensionApi extensionApi) {
        List k10;
        ArrayList arrayList = new ArrayList();
        SharedStateResult e10 = e("com.adobe.module.identity", event, extensionApi);
        String k11 = ch.b.k(e10 != null ? e10.b() : null, "mid", null);
        if (k11 != null) {
            arrayList.add(new a("4", k11, "namespaceId"));
        }
        Map b10 = e10 != null ? e10.b() : null;
        k10 = u.k();
        List m10 = ch.b.m(Map.class, b10, "visitoridslist", k10);
        if (m10 != null) {
            List<VisitorID> a10 = k.a(m10);
            t.f(a10, "VisitorIDSerializer.conv…itorIds(customVisitorIDs)");
            for (VisitorID visitorID : a10) {
                String b11 = visitorID.b();
                if (!(b11 == null || b11.length() == 0)) {
                    String d10 = visitorID.d();
                    t.f(d10, "visitorID.idType");
                    String b12 = visitorID.b();
                    t.f(b12, "visitorID.id");
                    arrayList.add(new a(d10, b12, "integrationCode"));
                }
            }
        }
        String k12 = ch.b.k(e10 != null ? e10.b() : null, "pushidentifier", null);
        if (k12 != null) {
            if (k12.length() > 0) {
                arrayList.add(new a("20919", k12, "integrationCode"));
            }
        }
        return arrayList;
    }

    private final boolean h(SharedStateResult sharedStateResult) {
        return (sharedStateResult != null ? sharedStateResult.a() : null) == SharedStateStatus.SET;
    }

    public final String a(Event event, ExtensionApi extensionApi) {
        int v10;
        String jSONObject;
        Map e10;
        List e11;
        Map l10;
        Map l11;
        t.g(event, "event");
        t.g(extensionApi, "extensionApi");
        ArrayList<a> arrayList = new ArrayList();
        arrayList.addAll(b(event, extensionApi));
        arrayList.addAll(c(event, extensionApi));
        arrayList.addAll(g(event, extensionApi));
        arrayList.addAll(f(event, extensionApi));
        v10 = v.v(arrayList, 10);
        ArrayList arrayList2 = new ArrayList(v10);
        for (a aVar : arrayList) {
            l11 = q0.l(c0.a("namespace", aVar.a()), c0.a("value", aVar.c()), c0.a("type", aVar.b()));
            arrayList2.add(l11);
        }
        ArrayList arrayList3 = new ArrayList();
        String d10 = d(event, extensionApi);
        if (d10 != null) {
            l10 = q0.l(c0.a("namespace", "imsOrgID"), c0.a("value", d10));
            arrayList3.add(l10);
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        if (!arrayList3.isEmpty()) {
            linkedHashMap.put("companyContexts", arrayList3);
        }
        if (!arrayList2.isEmpty()) {
            e10 = p0.e(c0.a("userIDs", arrayList2));
            e11 = sm.t.e(e10);
            linkedHashMap.put("users", e11);
        }
        try {
            jSONObject = new JSONObject(linkedHashMap).toString();
        } catch (JSONException unused) {
            jSONObject = new JSONObject().toString();
        }
        t.f(jSONObject, "try {\n            JSONOb…ct().toString()\n        }");
        return jSONObject;
    }
}
