package com.adobe.marketing.mobile.identity;

import ch.l;
import com.adobe.marketing.mobile.Event;
import com.adobe.marketing.mobile.Extension;
import com.adobe.marketing.mobile.ExtensionApi;
import com.adobe.marketing.mobile.ExtensionEventListener;
import com.adobe.marketing.mobile.Identity;
import com.adobe.marketing.mobile.MobilePrivacyStatus;
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.amplifyframework.core.model.ModelIdentifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import wg.d0;
import wg.i0;
import wg.r;
import wg.t;
import wg.v;
import wg.w;
import wg.x;
import wg.z;

/* loaded from: classes4.dex */
public final class IdentityExtension extends Extension {

    /* renamed from: p, reason: collision with root package name */
    private static boolean f12250p = false;

    /* renamed from: q, reason: collision with root package name */
    private static final Object f12251q = new Object();

    /* renamed from: b, reason: collision with root package name */
    private wg.n f12252b;

    /* renamed from: c, reason: collision with root package name */
    a f12253c;

    /* renamed from: d, reason: collision with root package name */
    private final v f12254d;

    /* renamed from: e, reason: collision with root package name */
    private String f12255e;

    /* renamed from: f, reason: collision with root package name */
    private String f12256f;

    /* renamed from: g, reason: collision with root package name */
    private String f12257g;

    /* renamed from: h, reason: collision with root package name */
    private String f12258h;

    /* renamed from: i, reason: collision with root package name */
    private String f12259i;

    /* renamed from: j, reason: collision with root package name */
    private long f12260j;

    /* renamed from: k, reason: collision with root package name */
    private long f12261k;

    /* renamed from: l, reason: collision with root package name */
    private List f12262l;

    /* renamed from: m, reason: collision with root package name */
    private MobilePrivacyStatus f12263m;

    /* renamed from: n, reason: collision with root package name */
    private boolean f12264n;

    /* renamed from: o, reason: collision with root package name */
    private boolean f12265o;

    IdentityExtension(ExtensionApi extensionApi) {
        this(extensionApi, i0.f().d().a("visitorIDServiceDataStore"), null);
    }

    private void K(String str, Map map, Event event) {
        Event a10 = event == null ? new Event.Builder(str, "com.adobe.eventType.identity", "com.adobe.eventSource.responseIdentity").d(map).a() : new Event.Builder(str, "com.adobe.eventType.identity", "com.adobe.eventSource.responseIdentity").d(map).c(event).a();
        a().c(a10);
        t.e("Identity", "IdentityExtension", "dispatchResponse : Identity Response event has been added to event hub : %s", a10.toString());
    }

    private boolean P(String str, Event event) {
        SharedStateResult e10 = a().e(str, event, false, SharedStateResolution.LAST_SET);
        if (e10 == null || e10.a() != SharedStateStatus.SET) {
            return false;
        }
        return !ch.f.a(e10.b());
    }

    private void Q() {
        if (this.f12252b == null) {
            this.f12252b = new d0(i0.f().c().a("com.adobe.module.identity"), new m(this));
        }
    }

    private boolean T() {
        synchronized (f12251q) {
            v vVar = this.f12254d;
            if (vVar == null) {
                t.e("Identity", "IdentityExtension", "isPushEnabled : Unable to update push flag because the LocalStorageService was not available.", new Object[0]);
                return false;
            }
            boolean z10 = vVar.getBoolean("ADOBEMOBILE_PUSH_ENABLED", false);
            f12250p = z10;
            return z10;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void V(wg.o oVar) {
        if (oVar == null) {
            return;
        }
        if (oVar.d() == 200) {
            t.e("Identity", "IdentityExtension", "sendOptOutHit - Successfully sent the opt-out hit.", new Object[0]);
        } else {
            t.e("Identity", "IdentityExtension", "sendOptOutHit - Failed to send the opt-out hit with connection status (%s).", Integer.valueOf(oVar.d()));
        }
        oVar.close();
    }

    private void W(Map map) {
        this.f12263m = MobilePrivacyStatus.fromString(ch.b.k(map, "global.privacy", b.f12269a.getValue()));
    }

    private static VisitorID b0(String str) {
        int indexOf = str.indexOf(61);
        if (indexOf == -1) {
            t.a("Identity", "IdentityExtension", "parseCustomerIDStringToVisitorIDObject : Unable to load Visitor ID from Shared Preferences: (%s).", str);
            return null;
        }
        try {
            String substring = str.substring(0, indexOf);
            String substring2 = str.substring(indexOf + 1);
            List asList = Arrays.asList(substring2.split("%01"));
            if (asList.size() != 3) {
                t.a("Identity", "IdentityExtension", "parseCustomerIDStringToVisitorIDObject : Unable to load Visitor ID from Shared Preferences because the value was malformed: (%s).", substring2);
                return null;
            }
            if (ch.j.a((String) asList.get(1))) {
                t.a("Identity", "IdentityExtension", "parseCustomerIDStringToVisitorIDObject : Unable to load Visitor ID from Shared Preferences because the ECID had null or empty id: (%s).", substring2);
                return null;
            }
            try {
                return new VisitorID(substring, (String) asList.get(0), (String) asList.get(1), VisitorID.AuthenticationState.fromInteger(Integer.parseInt((String) asList.get(2))));
            } catch (IllegalStateException | NumberFormatException e10) {
                t.a("Identity", "IdentityExtension", "parseCustomerIDStringToVisitorIDObject : Unable to parse the ECID: (%s) due to an exception: (%s).", str, e10.getLocalizedMessage());
                return null;
            }
        } catch (IndexOutOfBoundsException e11) {
            t.a("Identity", "IdentityExtension", "parseCustomerIDStringToVisitorIDObject : Unable to load Visitor ID: (%s) from Shared Preference because the name or value was malformed as in the exception: (%s).", str, e11);
            return null;
        }
    }

    private static boolean h0(VisitorID visitorID, VisitorID visitorID2) {
        if (visitorID == null || visitorID2 == null) {
            return false;
        }
        return visitorID.d() != null ? visitorID.d().equals(visitorID2.d()) : visitorID2.d() == null;
    }

    private void k0(boolean z10) {
        synchronized (f12251q) {
            v vVar = this.f12254d;
            if (vVar != null) {
                vVar.c("ADOBEMOBILE_PUSH_ENABLED", z10);
            } else {
                t.e("Identity", "IdentityExtension", "setPushStatus : Unable to update push flag because the LocalStorageService was not available.", new Object[0]);
            }
            f12250p = z10;
            StringBuilder sb2 = new StringBuilder();
            sb2.append("setPushStatus : Push notifications status is now: ");
            sb2.append(f12250p ? "Enabled" : "Disabled");
            t.e("Identity", "IdentityExtension", sb2.toString(), new Object[0]);
        }
    }

    private boolean l0(List list, Map map, boolean z10, a aVar) {
        boolean z11;
        boolean z12;
        if (aVar.a()) {
            z11 = true;
        } else {
            t.a("Identity", "IdentityExtension", "shouldSync : Ignoring ID Sync due to privacy status opt-out or missing experienceCloud.org.", new Object[0]);
            z11 = false;
        }
        boolean z13 = ch.k.h() - this.f12260j > this.f12261k || z10;
        boolean z14 = (list == null || list.isEmpty()) ? false : true;
        boolean z15 = map != null;
        if (ch.j.a(this.f12255e) || z14 || z15 || z13) {
            if (ch.j.a(this.f12255e)) {
                t.e("Identity", "IdentityExtension", "shouldSync : ECID is null when sync identifiers event received. Generate new ECID value.", new Object[0]);
                this.f12255e = B();
            }
            z12 = true;
        } else {
            z12 = false;
        }
        return z12 && z11;
    }

    private static void m0(v vVar, String str, String str2) {
        if (ch.j.a(str2)) {
            vVar.remove(str);
        } else {
            vVar.e(str, str2);
        }
    }

    private void n() {
        X();
        u();
        Q();
        if (ch.j.a(this.f12255e)) {
            return;
        }
        a().b(a0(), null);
        this.f12265o = true;
    }

    private String n0(List list) {
        if (list == null) {
            return "";
        }
        StringBuilder sb2 = new StringBuilder();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            VisitorID visitorID = (VisitorID) it.next();
            sb2.append("&");
            sb2.append("d_cid_ic");
            sb2.append("=");
            sb2.append(visitorID.d());
            sb2.append("%01");
            if (visitorID.b() != null) {
                sb2.append(visitorID.b());
            }
            sb2.append("%01");
            sb2.append(visitorID.a().getValue());
        }
        return sb2.toString();
    }

    private String o(a aVar) {
        if (aVar == null || aVar.c() == null || this.f12255e == null) {
            return null;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("d_orgid", aVar.c());
        hashMap.put("d_mid", this.f12255e);
        ch.l lVar = new ch.l();
        lVar.a("demoptout.jpg").f(aVar.b()).d(hashMap);
        return lVar.e();
    }

    private void o0(String str) {
        this.f12256f = str;
        i0();
    }

    private String p(List list, Map map, a aVar, boolean z10) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("d_ver", "2");
        linkedHashMap.put("d_rtbd", "json");
        if (z10) {
            if (ch.j.a(this.f12256f)) {
                linkedHashMap.put("device_consent", "0");
                linkedHashMap.put("d_consent_ic", "DSID_20914");
            } else {
                linkedHashMap.put("device_consent", "1");
            }
        }
        linkedHashMap.put("d_orgid", aVar.c());
        String str = this.f12255e;
        if (str != null) {
            linkedHashMap.put("d_mid", str);
        }
        String str2 = this.f12258h;
        if (str2 != null) {
            linkedHashMap.put("d_blob", str2);
        }
        String str3 = this.f12259i;
        if (str3 != null) {
            linkedHashMap.put("dcs_region", str3);
        }
        ch.l lVar = new ch.l();
        lVar.a("id").f(aVar.b()).d(linkedHashMap);
        String C = C(list);
        if (!ch.j.a(C)) {
            lVar.b(C, l.a.NONE);
        }
        String A = A(map);
        if (!ch.j.a(A)) {
            lVar.b(A, l.a.NONE);
        }
        return lVar.e();
    }

    private void q(boolean z10) {
        k0(z10);
        HashMap hashMap = new HashMap();
        hashMap.put("a.push.optin", String.valueOf(z10));
        HashMap hashMap2 = new HashMap();
        hashMap2.put("action", "Push");
        hashMap2.put("contextdata", hashMap);
        hashMap2.put("trackinternal", Boolean.TRUE);
        Event a10 = new Event.Builder("AnalyticsForIdentityRequest", "com.adobe.eventType.analytics", "com.adobe.eventSource.requestContent").d(hashMap2).a();
        a().c(a10);
        t.e("Identity", "IdentityExtension", "dispatchAnalyticsHit : Analytics event has been added to event hub : (%s)", a10);
    }

    private List s(List list) {
        ArrayList arrayList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            arrayList.add(com.adobe.marketing.mobile.internal.util.k.b((VisitorID) it.next()));
        }
        return arrayList;
    }

    static List t(String str) {
        VisitorID visitorID;
        if (ch.j.a(str)) {
            return new ArrayList();
        }
        ArrayList arrayList = new ArrayList();
        for (String str2 : str.split("&")) {
            if (!ch.j.a(str2)) {
                VisitorID b02 = b0(str2);
                Iterator it = arrayList.iterator();
                while (true) {
                    if (!it.hasNext()) {
                        visitorID = null;
                        break;
                    }
                    visitorID = (VisitorID) it.next();
                    if (h0(visitorID, b02)) {
                        break;
                    }
                }
                if (visitorID != null) {
                    arrayList.remove(visitorID);
                }
                if (b02 != null) {
                    arrayList.add(b02);
                }
            }
        }
        return arrayList;
    }

    private void u() {
        ch.g.a("ADBMobileIdentity.sqlite");
    }

    private Map w(Map map) {
        if (map == null) {
            return null;
        }
        HashMap hashMap = new HashMap();
        if (map.containsKey("pushidentifier")) {
            try {
                String k10 = ch.b.k(map, "pushidentifier", null);
                q0(k10);
                hashMap.put("20919", k10);
            } catch (Exception e10) {
                t.b("Identity", "IdentityExtension", "extractDPID : Unable to update the push identifier due to: (%s).", e10);
            }
        }
        if (hashMap.size() == 0) {
            return null;
        }
        return hashMap;
    }

    String A(Map map) {
        if (ch.f.a(map)) {
            return "";
        }
        HashMap hashMap = new HashMap(map);
        StringBuilder sb2 = new StringBuilder();
        for (Map.Entry entry : hashMap.entrySet()) {
            sb2.append("&d_cid=");
            sb2.append(ch.m.b((String) entry.getKey()));
            sb2.append("%01");
            sb2.append(ch.m.b((String) entry.getValue()));
        }
        if (sb2.charAt(0) == '&') {
            sb2.deleteCharAt(0);
        }
        return sb2.toString();
    }

    String B() {
        UUID randomUUID = UUID.randomUUID();
        long mostSignificantBits = randomUUID.getMostSignificantBits();
        long leastSignificantBits = randomUUID.getLeastSignificantBits();
        Locale locale = Locale.US;
        Object[] objArr = new Object[2];
        if (mostSignificantBits < 0) {
            mostSignificantBits = -mostSignificantBits;
        }
        objArr[0] = Long.valueOf(mostSignificantBits);
        if (leastSignificantBits < 0) {
            leastSignificantBits = -leastSignificantBits;
        }
        objArr[1] = Long.valueOf(leastSignificantBits);
        String format = String.format(locale, "%019d%019d", objArr);
        t.e("Identity", "IdentityExtension", "generateMID : Generating new ECID %s", format);
        return format;
    }

    String C(List list) {
        if (list == null || list.isEmpty()) {
            t.a("Identity", "IdentityExtension", "generateURLEncodedValuesCustomerIdString : No Visitor ID exists in the provided list to generate for URL.", new Object[0]);
            return null;
        }
        StringBuilder sb2 = new StringBuilder();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            VisitorID visitorID = (VisitorID) it.next();
            sb2.append("&");
            sb2.append("d_cid_ic");
            sb2.append("=");
            sb2.append(ch.m.b(visitorID.d()));
            sb2.append("%01");
            String b10 = ch.m.b(visitorID.b());
            if (b10 != null) {
                sb2.append(b10);
            }
            sb2.append("%01");
            sb2.append(visitorID.a().getValue());
        }
        if (sb2.charAt(0) == '&') {
            sb2.deleteCharAt(0);
        }
        return sb2.toString();
    }

    StringBuilder D(a aVar, Map map) {
        String str;
        StringBuilder sb2 = new StringBuilder();
        String l10 = l(l(null, "TS", String.valueOf(ch.k.h())), "MCMID", this.f12255e);
        if (map != null) {
            String k10 = ch.b.k(map, "aid", null);
            if (!ch.j.a(k10)) {
                l10 = l(l10, "MCAID", k10);
            }
            str = ch.b.k(map, "vid", null);
        } else {
            str = null;
        }
        String c10 = aVar != null ? aVar.c() : null;
        if (!ch.j.a(c10)) {
            l10 = l(l10, "MCORGID", c10);
        }
        sb2.append("adobe_mc");
        sb2.append("=");
        sb2.append(ch.m.b(l10));
        if (!ch.j.a(str)) {
            sb2.append("&");
            sb2.append("adobe_aa_vid");
            sb2.append("=");
            sb2.append(ch.m.b(str));
        }
        return sb2;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void E(Event event) {
        Map o10;
        v vVar;
        if (event == null || (o10 = event.o()) == null) {
            return;
        }
        String k10 = ch.b.k(o10, "aid", null);
        if (ch.j.a(k10) || (vVar = this.f12254d) == null || vVar.contains("ADOBEMOBILE_AID_SYNCED")) {
            return;
        }
        this.f12254d.c("ADOBEMOBILE_AID_SYNCED", true);
        HashMap hashMap = new HashMap();
        hashMap.put("AVID", k10);
        HashMap hashMap2 = new HashMap();
        hashMap2.put("visitoridentifiers", hashMap);
        hashMap2.put("authenticationstate", Integer.valueOf(VisitorID.AuthenticationState.UNKNOWN.getValue()));
        hashMap2.put("forcesync", Boolean.FALSE);
        hashMap2.put("issyncevent", Boolean.TRUE);
        a().c(new Event.Builder("AVID Sync", "com.adobe.eventType.identity", "com.adobe.eventSource.requestIdentity").d(hashMap2).a());
    }

    void F(Event event, a aVar, Map map) {
        m(ch.b.k(event.o(), "baseurl", null), event, aVar, map, null);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void G(Event event) {
        Map o10;
        if (event == null || (o10 = event.o()) == null) {
            return;
        }
        p0(o10);
        MobilePrivacyStatus fromString = MobilePrivacyStatus.fromString(ch.b.k(o10, "global.privacy", b.f12269a.getValue()));
        this.f12252b.d(fromString);
        if (fromString.equals(MobilePrivacyStatus.OPT_OUT)) {
            N(o10);
        }
        f0(event, o10);
    }

    void H(Event event, a aVar, Map map) {
        StringBuilder D = D(aVar, map);
        HashMap hashMap = new HashMap();
        hashMap.put("urlvariables", D.toString());
        K("IDENTITY_URL_VARIABLES", hashMap, event);
    }

    void I(Map map) {
        Event a10 = new Event.Builder("Configuration Update From IdentityExtension", "com.adobe.eventType.configuration", "com.adobe.eventSource.requestContent").d(map).a();
        a().c(a10);
        t.e("Identity", "IdentityExtension", "dispatchConfigUpdateRequest : Configuration Update event has been added to event hub : %s", a10.toString());
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void J(Event event) {
        if (this.f12263m == MobilePrivacyStatus.OPT_OUT) {
            t.a("Identity", "IdentityExtension", "handleIdentityRequestReset: Privacy is opt-out, ignoring event.", new Object[0]);
            return;
        }
        this.f12255e = null;
        this.f12256f = null;
        this.f12258h = null;
        this.f12259i = null;
        this.f12262l = null;
        this.f12257g = null;
        v vVar = this.f12254d;
        if (vVar != null) {
            vVar.remove("ADOBEMOBILE_AID_SYNCED");
            this.f12254d.remove("ADOBEMOBILE_PUSH_ENABLED");
        }
        i0();
        if (O(event, false)) {
            a().b(a0(), event);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void L(Event event) {
        Map o10;
        if (event == null || (o10 = event.o()) == null || !ch.b.h(o10, "updatesharedstate", false)) {
            return;
        }
        a().b(a0(), event);
    }

    /* JADX WARN: Code restructure failed: missing block: B:49:0x0090, code lost:
    
        if (ch.j.a(r10.f12258h) == false) goto L29;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    boolean M(n nVar) {
        String str;
        boolean z10;
        String str2;
        boolean z11 = false;
        if (nVar == null) {
            t.a("Identity", "IdentityExtension", "handleNetworkResponseMap : Received an empty JSON in response from ECID Service, so there is nothing to handle.", new Object[0]);
            return false;
        }
        List list = nVar.f12290f;
        if (list != null && !list.isEmpty()) {
            t.a("Identity", "IdentityExtension", "handleNetworkResponseMap : Received opt-out response from ECID Service, so updating the privacy status in the configuration to opt-out.", new Object[0]);
            HashMap hashMap = new HashMap();
            hashMap.put("global.privacy", MobilePrivacyStatus.OPT_OUT.getValue());
            HashMap hashMap2 = new HashMap();
            hashMap2.put("config.update", hashMap);
            I(hashMap2);
        }
        if (!ch.j.a(nVar.f12288d)) {
            t.f("Identity", "IdentityExtension", "handleNetworkResponseMap : ECID Service returned an error: (%s).", nVar.f12288d);
            if (this.f12255e != null) {
                return false;
            }
            t.e("Identity", "IdentityExtension", "handleNetworkResponseMap : ECID is null when network response error received. Generate new ECID value.", new Object[0]);
            this.f12255e = B();
            return true;
        }
        if (ch.j.a(nVar.f12286b) || !nVar.f12286b.equals(this.f12255e)) {
            return false;
        }
        try {
            str = nVar.f12285a;
        } catch (Exception e10) {
            e = e10;
        }
        try {
            if (str == null || str.equals(this.f12258h)) {
                if (ch.j.a(nVar.f12285a)) {
                }
                z10 = false;
                str2 = nVar.f12287c;
                if ((str2 != null && !str2.equals(this.f12259i)) || (ch.j.a(nVar.f12287c) && !ch.j.a(this.f12259i))) {
                    z10 = true;
                }
                String str3 = nVar.f12285a;
                this.f12258h = str3;
                String str4 = nVar.f12287c;
                this.f12259i = str4;
                long j10 = nVar.f12289e;
                this.f12261k = j10;
                t.a("Identity", "IdentityExtension", "handleNetworkResponseMap : ECID Service returned (mid: %s, blob: %s, hint: %s, ttl: %d).", this.f12255e, str3, str4, Long.valueOf(j10));
                return z10;
            }
            str2 = nVar.f12287c;
            if (str2 != null) {
                z10 = true;
                String str32 = nVar.f12285a;
                this.f12258h = str32;
                String str42 = nVar.f12287c;
                this.f12259i = str42;
                long j102 = nVar.f12289e;
                this.f12261k = j102;
                t.a("Identity", "IdentityExtension", "handleNetworkResponseMap : ECID Service returned (mid: %s, blob: %s, hint: %s, ttl: %d).", this.f12255e, str32, str42, Long.valueOf(j102));
                return z10;
            }
            z10 = true;
            String str322 = nVar.f12285a;
            this.f12258h = str322;
            String str422 = nVar.f12287c;
            this.f12259i = str422;
            long j1022 = nVar.f12289e;
            this.f12261k = j1022;
            t.a("Identity", "IdentityExtension", "handleNetworkResponseMap : ECID Service returned (mid: %s, blob: %s, hint: %s, ttl: %d).", this.f12255e, str322, str422, Long.valueOf(j1022));
            return z10;
        } catch (Exception e11) {
            e = e11;
            z11 = z10;
            t.f("Identity", "IdentityExtension", "handleNetworkResponseMap : Error parsing the response from ECID Service : (%s).", e);
            return z11;
        }
        z10 = true;
    }

    void N(Map map) {
        if (map.containsKey("audience.server")) {
            return;
        }
        a aVar = new a(map);
        if (aVar.d().equals(MobilePrivacyStatus.OPT_OUT)) {
            j0(aVar);
        }
    }

    boolean O(Event event, boolean z10) {
        a aVar = this.f12253c;
        if (aVar == null) {
            t.a("Identity", "IdentityExtension", "handleSyncIdentifiers : Unable to process sync identifiers request as the configuration did not contain a valid Experience Cloud organization ID.", new Object[0]);
            return false;
        }
        if (aVar.d() == MobilePrivacyStatus.OPT_OUT) {
            t.a("Identity", "IdentityExtension", "handleSyncIdentifiers : Ignored the Sync Identifiers call because the privacy status was opt-out.", new Object[0]);
            return false;
        }
        if (event == null) {
            t.a("Identity", "IdentityExtension", "handleSyncIdentifiers : Ignoring the Sync Identifiers call because the event sent was null.", new Object[0]);
            return false;
        }
        Map o10 = event.o();
        Map w10 = w(o10);
        Map x10 = x(o10);
        VisitorID.AuthenticationState fromInteger = VisitorID.AuthenticationState.fromInteger(ch.b.i(o10, "authenticationstate", 0));
        boolean z11 = z10 || ch.b.h(o10, "forcesync", false);
        List z12 = z(x10, fromInteger);
        j v10 = v(o10);
        boolean booleanValue = ((Boolean) v10.b()).booleanValue();
        VisitorID visitorID = (VisitorID) v10.a();
        if (visitorID != null) {
            z12.add(visitorID);
        }
        List Y = Y(z12);
        this.f12262l = Y;
        this.f12262l = r(Y);
        List r10 = r(z12);
        if (l0(r10, w10, z11 || booleanValue, this.f12253c)) {
            this.f12252b.e(new k(p(r10, w10, this.f12253c, booleanValue), event).d());
        } else {
            t.a("Identity", "IdentityExtension", "handleSyncIdentifiers : Ignoring ID sync because nothing new to sync after the last sync.", new Object[0]);
        }
        i0();
        return true;
    }

    boolean R(Event event) {
        return event.o() != null && event.o().containsKey("baseurl");
    }

    boolean S(Event event) {
        return ch.b.h(event.o(), "urlvariables", false);
    }

    boolean U(Event event) {
        return ch.b.h(event.o(), "issyncevent", false) || event.w().equals("com.adobe.eventType.generic.identity");
    }

    void X() {
        v vVar = this.f12254d;
        int i10 = 0;
        if (vVar == null) {
            t.a("Identity", "IdentityExtension", "loadVariablesFromPersistentData : Unable to load the Identity data from persistence because the LocalStorageService was null.", new Object[0]);
            return;
        }
        this.f12255e = vVar.getString("ADOBEMOBILE_PERSISTED_MID", null);
        List t10 = t(this.f12254d.getString("ADOBEMOBILE_VISITORID_IDS", null));
        this.f12262l = (t10 == null || t10.isEmpty()) ? null : t10;
        if (t10 != null && !t10.isEmpty()) {
            i10 = this.f12262l.size();
        }
        this.f12259i = this.f12254d.getString("ADOBEMOBILE_PERSISTED_MID_HINT", null);
        this.f12258h = this.f12254d.getString("ADOBEMOBILE_PERSISTED_MID_BLOB", null);
        this.f12261k = this.f12254d.getLong("ADOBEMOBILE_VISITORID_TTL", 600L);
        this.f12260j = this.f12254d.getLong("ADOBEMOBILE_VISITORID_SYNC", 0L);
        this.f12256f = this.f12254d.getString("ADOBEMOBILE_ADVERTISING_IDENTIFIER", null);
        this.f12257g = this.f12254d.getString("ADOBEMOBILE_PUSH_IDENTIFIER", null);
        t.e("Identity", "IdentityExtension", "loadVariablesFromPersistentData : Successfully loaded the Identity data from persistence. Loaded %d VisitorIds. ECID is set to %s. ", Integer.valueOf(i10), this.f12255e);
    }

    List Y(List list) {
        VisitorID visitorID;
        VisitorID visitorID2;
        if (list == null || list.isEmpty()) {
            return this.f12262l;
        }
        ArrayList arrayList = this.f12262l != null ? new ArrayList(this.f12262l) : new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            VisitorID visitorID3 = (VisitorID) it.next();
            Iterator it2 = arrayList.iterator();
            while (true) {
                if (!it2.hasNext()) {
                    visitorID = null;
                    visitorID2 = null;
                    break;
                }
                visitorID = (VisitorID) it2.next();
                if (h0(visitorID, visitorID3)) {
                    visitorID2 = new VisitorID(visitorID.c(), visitorID.d(), visitorID3.b(), visitorID3.a());
                    break;
                }
            }
            if (visitorID2 != null) {
                arrayList.remove(visitorID);
                arrayList.add(visitorID2);
            } else {
                arrayList.add(visitorID3);
            }
        }
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void Z(n nVar, Event event) {
        boolean z10;
        this.f12260j = ch.k.h();
        if (this.f12263m != MobilePrivacyStatus.OPT_OUT) {
            z10 = M(nVar);
            i0();
        } else {
            z10 = false;
        }
        Map a02 = a0();
        if (z10) {
            a02.put("updatesharedstate", Boolean.TRUE);
        }
        K("UPDATED_IDENTITY_RESPONSE", a02, null);
        if (event != null) {
            K("UPDATED_IDENTITY_RESPONSE", a02, event);
        }
    }

    Map a0() {
        HashMap hashMap = new HashMap();
        if (!ch.j.a(this.f12255e)) {
            hashMap.put("mid", this.f12255e);
        }
        if (!ch.j.a(this.f12256f)) {
            hashMap.put("advertisingidentifier", this.f12256f);
        }
        if (!ch.j.a(this.f12257g)) {
            hashMap.put("pushidentifier", this.f12257g);
        }
        if (!ch.j.a(this.f12258h)) {
            hashMap.put("blob", this.f12258h);
        }
        if (!ch.j.a(this.f12259i)) {
            hashMap.put("locationhint", this.f12259i);
        }
        List list = this.f12262l;
        if (list != null && !list.isEmpty()) {
            hashMap.put("visitoridslist", s(this.f12262l));
        }
        hashMap.put("lastsync", Long.valueOf(this.f12260j));
        return hashMap;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.adobe.marketing.mobile.Extension
    public String b() {
        return "Identity";
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void c0(Event event) {
        Map o10;
        if (event == null || (o10 = event.o()) == null || !o10.containsKey("optedouthitsent") || ch.b.h(o10, "optedouthitsent", false)) {
            return;
        }
        SharedStateResult e10 = a().e("com.adobe.module.configuration", event, false, SharedStateResolution.ANY);
        if (e10 == null || e10.a() != SharedStateStatus.SET) {
            t.e("Identity", "IdentityExtension", "processAudienceResponse : Unable to process the Identity events in the event queue because the configuration shared state is pending.", new Object[0]);
            return;
        }
        a aVar = new a(e10.b());
        if (aVar.d().equals(MobilePrivacyStatus.OPT_OUT)) {
            j0(aVar);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void d0(Event event) {
        if (event.w().equals("com.adobe.eventType.identity") && event.t().equals("com.adobe.eventSource.requestIdentity") && (event.o() == null || event.o().isEmpty())) {
            K("IDENTITY_RESPONSE_CONTENT_ONE_TIME", a0(), event);
            return;
        }
        ExtensionApi a10 = a();
        SharedStateResolution sharedStateResolution = SharedStateResolution.LAST_SET;
        SharedStateResult e10 = a10.e("com.adobe.module.configuration", event, false, sharedStateResolution);
        if (e10 == null) {
            return;
        }
        a aVar = new a(e10.b());
        t.e("Identity", "IdentityExtension", "processEvent : Processing the Identity event: %s", event);
        if (U(event) || "com.adobe.eventType.generic.identity".equals(event.w())) {
            if (O(event, false)) {
                a().b(a0(), event);
                return;
            }
            return;
        }
        if (R(event)) {
            SharedStateResult e11 = a().e("com.adobe.module.analytics", event, false, sharedStateResolution);
            F(event, aVar, e11 != null ? e11.b() : null);
        } else if (S(event)) {
            SharedStateResult e12 = a().e("com.adobe.module.analytics", event, false, sharedStateResolution);
            H(event, aVar, e12 != null ? e12.b() : null);
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.adobe.marketing.mobile.Extension
    public String e() {
        return "com.adobe.module.identity";
    }

    boolean e0(String str) {
        v vVar = this.f12254d;
        if (vVar == null) {
            t.e("Identity", "IdentityExtension", "processNewPushToken : Unable to update push settings because the LocalStorageService was not available.", new Object[0]);
            return false;
        }
        String string = vVar.getString("ADOBEMOBILE_PUSH_IDENTIFIER", null);
        boolean z10 = this.f12254d.getBoolean("ADOBEMOBILE_ANALYTICS_PUSH_SYNC", false);
        boolean z11 = (ch.j.a(str) && string == null) || (string != null && string.equals(str));
        if ((z11 && !ch.j.a(str)) || (z11 && z10)) {
            return false;
        }
        if (!z10) {
            this.f12254d.c("ADOBEMOBILE_ANALYTICS_PUSH_SYNC", true);
        }
        if (ch.j.a(str)) {
            this.f12254d.remove("ADOBEMOBILE_PUSH_IDENTIFIER");
        } else {
            this.f12254d.e("ADOBEMOBILE_PUSH_IDENTIFIER", str);
        }
        return true;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.adobe.marketing.mobile.Extension
    public String f() {
        return Identity.b();
    }

    void f0(Event event, Map map) {
        MobilePrivacyStatus fromString;
        if (map == null || this.f12263m == (fromString = MobilePrivacyStatus.fromString(ch.b.k(map, "global.privacy", b.f12269a.getValue())))) {
            return;
        }
        this.f12263m = fromString;
        t.e("Identity", "IdentityExtension", "processPrivacyChange : Processed privacy change request. New privacy status is: (%s).", fromString.getValue());
        if (this.f12263m == MobilePrivacyStatus.OPT_OUT) {
            this.f12255e = null;
            this.f12256f = null;
            this.f12258h = null;
            this.f12259i = null;
            this.f12262l = null;
            v vVar = this.f12254d;
            if (vVar != null) {
                vVar.remove("ADOBEMOBILE_AID_SYNCED");
            }
            q0(null);
            i0();
            a().b(a0(), event);
        } else if (ch.j.a(this.f12255e) && O(event, false)) {
            a().b(a0(), event);
        }
        Q();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.adobe.marketing.mobile.Extension
    public void g() {
        a().f("com.adobe.eventType.identity", "com.adobe.eventSource.requestIdentity", new ExtensionEventListener() { // from class: com.adobe.marketing.mobile.identity.c
            @Override // com.adobe.marketing.mobile.ExtensionEventListener
            public final void a(Event event) {
                IdentityExtension.this.d0(event);
            }
        });
        a().f("com.adobe.eventType.generic.identity", "com.adobe.eventSource.requestContent", new ExtensionEventListener() { // from class: com.adobe.marketing.mobile.identity.c
            @Override // com.adobe.marketing.mobile.ExtensionEventListener
            public final void a(Event event) {
                IdentityExtension.this.d0(event);
            }
        });
        a().f("com.adobe.eventType.generic.identity", "com.adobe.eventSource.requestReset", new ExtensionEventListener() { // from class: com.adobe.marketing.mobile.identity.d
            @Override // com.adobe.marketing.mobile.ExtensionEventListener
            public final void a(Event event) {
                IdentityExtension.this.J(event);
            }
        });
        a().f("com.adobe.eventType.identity", "com.adobe.eventSource.responseIdentity", new ExtensionEventListener() { // from class: com.adobe.marketing.mobile.identity.e
            @Override // com.adobe.marketing.mobile.ExtensionEventListener
            public final void a(Event event) {
                IdentityExtension.this.L(event);
            }
        });
        a().f("com.adobe.eventType.analytics", "com.adobe.eventSource.responseIdentity", new ExtensionEventListener() { // from class: com.adobe.marketing.mobile.identity.f
            @Override // com.adobe.marketing.mobile.ExtensionEventListener
            public final void a(Event event) {
                IdentityExtension.this.E(event);
            }
        });
        a().f("com.adobe.eventType.audienceManager", "com.adobe.eventSource.responseContent", new ExtensionEventListener() { // from class: com.adobe.marketing.mobile.identity.g
            @Override // com.adobe.marketing.mobile.ExtensionEventListener
            public final void a(Event event) {
                IdentityExtension.this.c0(event);
            }
        });
        a().f("com.adobe.eventType.configuration", "com.adobe.eventSource.responseContent", new ExtensionEventListener() { // from class: com.adobe.marketing.mobile.identity.h
            @Override // com.adobe.marketing.mobile.ExtensionEventListener
            public final void a(Event event) {
                IdentityExtension.this.G(event);
            }
        });
        n();
    }

    boolean g0(Map map) {
        p0(map);
        a aVar = this.f12253c;
        if (aVar != null && !ch.j.a(aVar.c())) {
            return true;
        }
        t.a("Identity", "IdentityExtension", "Cannot sync identifiers, waiting for configuration with valid 'experienceCloud.org' value.", new Object[0]);
        return false;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.adobe.marketing.mobile.Extension
    public void i() {
        this.f12252b.c();
    }

    void i0() {
        v vVar = this.f12254d;
        if (vVar == null) {
            t.e("Identity", "IdentityExtension", "savePersistently : Unable to save the IdentityExtension fields into persistence because the data store was null.", new Object[0]);
            return;
        }
        m0(vVar, "ADOBEMOBILE_VISITORID_IDS", n0(this.f12262l));
        m0(this.f12254d, "ADOBEMOBILE_PERSISTED_MID", this.f12255e);
        m0(this.f12254d, "ADOBEMOBILE_PUSH_IDENTIFIER", this.f12257g);
        m0(this.f12254d, "ADOBEMOBILE_ADVERTISING_IDENTIFIER", this.f12256f);
        m0(this.f12254d, "ADOBEMOBILE_PERSISTED_MID_HINT", this.f12259i);
        m0(this.f12254d, "ADOBEMOBILE_PERSISTED_MID_BLOB", this.f12258h);
        this.f12254d.b("ADOBEMOBILE_VISITORID_TTL", this.f12261k);
        this.f12254d.b("ADOBEMOBILE_VISITORID_SYNC", this.f12260j);
        t.e("Identity", "IdentityExtension", "savePersistently : Successfully saved the Identity data into persistence.", new Object[0]);
    }

    @Override // com.adobe.marketing.mobile.Extension
    public boolean j(Event event) {
        SharedStateResult e10;
        if (!y(event)) {
            return false;
        }
        if (event.w().equals("com.adobe.eventType.identity") && event.t().equals("com.adobe.eventSource.requestIdentity") && (event.o() == null || event.o().isEmpty())) {
            return true;
        }
        if (U(event) || "com.adobe.eventType.generic.identity".equals(event.w())) {
            SharedStateResult e11 = a().e("com.adobe.module.configuration", event, false, SharedStateResolution.LAST_SET);
            if (e11 != null && e11.a() == SharedStateStatus.SET) {
                return g0(e11.b());
            }
            t.e("Identity", "IdentityExtension", "Waiting for the Configuration shared state to be set before processing [event: %s].", event.q());
            return false;
        }
        if ((R(event) || S(event)) && (e10 = a().e("com.adobe.module.analytics", event, false, SharedStateResolution.LAST_SET)) != null && e10.a() != SharedStateStatus.SET) {
            t.e("Identity", "IdentityExtension", "Waiting for the Analytics shared state to be set before processing [event: %s].", event.q());
            return false;
        }
        if (P("com.adobe.module.configuration", event)) {
            return true;
        }
        t.e("Identity", "IdentityExtension", "Waiting for the Configuration shared state to get required configuration fields before processing [event: %s].", event.q());
        return false;
    }

    void j0(a aVar) {
        String o10 = o(aVar);
        if (ch.j.a(o10)) {
            t.a("Identity", "IdentityExtension", "sendOptOutHit : Unable to send network hit because the opt-out URL was null.", new Object[0]);
            return;
        }
        z h10 = i0.f().h();
        if (h10 == null) {
            t.a("Identity", "IdentityExtension", "sendOptOutHit : Unable to send network request to the opt-out URL (%s) because NetworkService is unavailable.", o10);
        } else {
            t.a("Identity", "IdentityExtension", "sendOptOutHit : Sending network request to the opt-out URL: (%s).", o10);
            h10.a(new x(o10, r.GET, null, null, 2, 2), new w() { // from class: com.adobe.marketing.mobile.identity.i
                @Override // wg.w
                public final void a(wg.o oVar) {
                    IdentityExtension.V(oVar);
                }
            });
        }
    }

    String l(String str, String str2, String str3) {
        if (ch.j.a(str2) || ch.j.a(str3)) {
            return str;
        }
        String format = String.format("%s=%s", str2, str3);
        return ch.j.a(str) ? format : String.format("%s|%s", str, format);
    }

    void m(String str, Event event, a aVar, Map map, StringBuilder sb2) {
        if (ch.j.a(str)) {
            HashMap hashMap = new HashMap();
            hashMap.put("updatedurl", str);
            K("IDENTITY_APPENDED_URL", hashMap, event);
            return;
        }
        StringBuilder sb3 = new StringBuilder(str);
        if (sb2 == null) {
            sb2 = D(aVar, map);
        }
        if (!ch.j.a(sb2.toString())) {
            int indexOf = sb3.indexOf("?");
            int indexOf2 = sb3.indexOf(ModelIdentifier.Helper.PRIMARY_KEY_DELIMITER);
            int length = indexOf2 > 0 ? indexOf2 : sb3.length();
            boolean z10 = indexOf2 > 0 && indexOf2 < indexOf;
            if (indexOf > 0 && indexOf != sb3.length() - 1 && !z10) {
                sb2.insert(0, "&");
            } else if (indexOf < 0 || z10) {
                sb2.insert(0, "?");
            }
            sb3.insert(length, sb2.toString());
        }
        HashMap hashMap2 = new HashMap();
        hashMap2.put("updatedurl", sb3.toString());
        K("IDENTITY_APPENDED_URL", hashMap2, event);
    }

    void p0(Map map) {
        if (ch.j.a(ch.b.k(map, "experienceCloud.org", null))) {
            return;
        }
        this.f12253c = new a(map);
    }

    void q0(String str) {
        this.f12257g = str;
        if (!e0(str)) {
            t.a("Identity", "IdentityExtension", "updatePushIdentifier : Ignored a push token (%s) as it matches with an existing token, and the push notification status will not be re-sent to Analytics.", str);
            return;
        }
        if (str == null && !T()) {
            q(false);
            t.a("Identity", "IdentityExtension", "updatePushIdentifier : First time sending a.push.optin false", new Object[0]);
        } else if (str == null) {
            q(false);
        } else {
            if (T()) {
                return;
            }
            q(true);
        }
    }

    List r(List list) {
        if (list == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList(list);
        try {
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                if (ch.j.a(((VisitorID) it.next()).b())) {
                    it.remove();
                    t.e("Identity", "IdentityExtension", "cleanupVisitorIdentifiers : VisitorID was discarded due to an empty/null identifier value.", new Object[0]);
                }
            }
        } catch (ClassCastException e10) {
            t.b("Identity", "IdentityExtension", "cleanupVisitorIdentifiers : Caught ClassCastException while iterating through visitor identifiers: %s", e10.getLocalizedMessage());
        } catch (NullPointerException e11) {
            t.b("Identity", "IdentityExtension", "cleanupVisitorIdentifiers : Caught NullPointerException while iterating through visitor identifiers: %s", e11.getLocalizedMessage());
        }
        return arrayList;
    }

    j v(Map map) {
        String str = "";
        VisitorID visitorID = null;
        if (map == null || !map.containsKey("advertisingidentifier")) {
            return new j(null, Boolean.FALSE);
        }
        try {
            String k10 = ch.b.k(map, "advertisingidentifier", "");
            if (!"00000000-0000-0000-0000-000000000000".equals(k10)) {
                str = k10;
            }
            if ((!str.isEmpty() && !str.equals(this.f12256f)) || (str.isEmpty() && !ch.j.a(this.f12256f))) {
                r6 = str.isEmpty() || ch.j.a(this.f12256f) || "00000000-0000-0000-0000-000000000000".equals(this.f12256f);
                VisitorID visitorID2 = new VisitorID("d_cid_ic", "DSID_20914", str, VisitorID.AuthenticationState.AUTHENTICATED);
                try {
                    o0(str);
                    t.e("Identity", "IdentityExtension", "extractAndUpdateAdid : The advertising identifier was set to: (%s).", str);
                    visitorID = visitorID2;
                } catch (Exception e10) {
                    e = e10;
                    visitorID = visitorID2;
                    t.b("Identity", "IdentityExtension", "extractAndUpdateAdid : Unable to update the advertising identifier due to: (%s)", e);
                    return new j(visitorID, Boolean.valueOf(r6));
                }
            }
        } catch (Exception e11) {
            e = e11;
        }
        return new j(visitorID, Boolean.valueOf(r6));
    }

    Map x(Map map) {
        Map n10;
        return (map == null || !map.containsKey("visitoridentifiers") || (n10 = ch.b.n(String.class, map, "visitoridentifiers", null)) == null) ? new HashMap() : n10;
    }

    boolean y(Event event) {
        if (this.f12264n) {
            return true;
        }
        SharedStateResult e10 = a().e("com.adobe.module.configuration", null, false, SharedStateResolution.LAST_SET);
        if (e10 == null || e10.a() != SharedStateStatus.SET) {
            t.e("Identity", "IdentityExtension", "Waiting for Configuration shared state before processing event [name: %s, id: %s]", event.q(), event.x());
            return false;
        }
        if (!g0(e10.b())) {
            return false;
        }
        W(e10.b());
        this.f12252b.d(this.f12263m);
        boolean z10 = O(event, true) || MobilePrivacyStatus.OPT_OUT.equals(this.f12263m);
        this.f12264n = z10;
        if (z10 && !this.f12265o) {
            a().b(a0(), event);
            this.f12265o = true;
        }
        return this.f12264n;
    }

    List z(Map map, VisitorID.AuthenticationState authenticationState) {
        if (map == null) {
            return Collections.emptyList();
        }
        ArrayList arrayList = new ArrayList();
        for (Map.Entry entry : map.entrySet()) {
            try {
                arrayList.add(new VisitorID("d_cid_ic", (String) entry.getKey(), (String) entry.getValue(), authenticationState));
            } catch (IllegalStateException e10) {
                t.a("Identity", "IdentityExtension", "generateCustomerIds : Unable to create Visitor IDs after encoding the provided list due to: (%s).", e10);
            }
        }
        return arrayList;
    }

    IdentityExtension(ExtensionApi extensionApi, v vVar, wg.n nVar) {
        super(extensionApi);
        this.f12263m = b.f12269a;
        this.f12264n = false;
        this.f12265o = false;
        this.f12254d = vVar;
        this.f12252b = nVar;
    }
}
