package pg;

import ch.i;
import dn.l;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;
import kotlin.jvm.internal.u0;
import kotlin.jvm.internal.v;
import kotlin.text.y;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import rm.n0;
import sm.q0;
import wg.i0;

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

    /* renamed from: h, reason: collision with root package name */
    public static final a f32630h = new a(null);

    /* renamed from: a, reason: collision with root package name */
    private final pg.a f32631a;

    /* renamed from: b, reason: collision with root package name */
    private final pg.b f32632b;

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

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

    /* renamed from: e, reason: collision with root package name */
    private final Map f32635e;

    /* renamed from: f, reason: collision with root package name */
    private Map f32636f;

    /* renamed from: g, reason: collision with root package name */
    private final Map f32637g;

    /* loaded from: classes4.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(k kVar) {
            this();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static final class b extends v implements l {

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ String f32639q;

        /* renamed from: r, reason: collision with root package name */
        final /* synthetic */ l f32640r;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        b(String str, l lVar) {
            super(1);
            this.f32639q = str;
            this.f32640r = lVar;
        }

        public final void a(Map map) {
            if (map == null) {
                this.f32640r.invoke(null);
                return;
            }
            d.this.l(map);
            d.this.f32637g.put(this.f32639q, new Date());
            this.f32640r.invoke(d.this.e());
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            a((Map) obj);
            return n0.f34904a;
        }
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public d(pg.a appIdManager) {
        this(appIdManager, new pg.b());
        t.g(appIdManager, "appIdManager");
    }

    private final void c() {
        boolean P;
        Object obj = this.f32635e.get("build.environment");
        if (!(obj instanceof String)) {
            obj = null;
        }
        String str = (String) obj;
        if (str == null) {
            str = "";
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        Iterator it = this.f32635e.entrySet().iterator();
        while (it.hasNext()) {
            String str2 = (String) ((Map.Entry) it.next()).getKey();
            P = y.P(str2, "__", false, 2, null);
            if (!P) {
                String f10 = f(str2, str);
                if (this.f32635e.get(f10) == null) {
                    f10 = str2;
                }
                Object obj2 = this.f32635e.get(f10);
                if (obj2 != null) {
                    linkedHashMap.put(str2, obj2);
                }
            }
        }
        this.f32636f = linkedHashMap;
    }

    private final String f(String str, String str2) {
        if (str2.length() == 0) {
            return str;
        }
        return "__" + str2 + "__" + str;
    }

    private final Map g() {
        i0 f10 = i0.f();
        t.f(f10, "ServiceProvider.getInstance()");
        wg.v a10 = f10.d().a("AdobeMobile_ConfigState");
        t.f(a10, "ServiceProvider.getInsta…Collection(DATASTORE_KEY)");
        String string = a10.getString("config.overridden.map", null);
        if (string == null || string.length() == 0) {
            return null;
        }
        try {
            JSONObject jSONObject = new JSONObject(new JSONTokener(string));
            wg.t.e("Configuration", "ConfigurationStateManager", "Loaded persisted programmatic Configuration", new Object[0]);
            return com.adobe.marketing.mobile.internal.util.d.c(jSONObject);
        } catch (JSONException e10) {
            wg.t.a("Configuration", "ConfigurationStateManager", "Unable to parse the Configuration from JSON Object. Exception: (" + e10 + ')', new Object[0]);
            return null;
        }
    }

    private final Map j(String str) {
        wg.t.e("Configuration", "ConfigurationStateManager", "Attempting to load cached config.", new Object[0]);
        u0 u0Var = u0.f26205a;
        boolean z10 = true;
        String format = String.format("https://assets.adobedtm.com/%s.json", Arrays.copyOf(new Object[]{str}, 1));
        t.f(format, "java.lang.String.format(format, *args)");
        i0 f10 = i0.f();
        t.f(f10, "ServiceProvider.getInstance()");
        xg.c b10 = f10.b().b("config", format);
        String a10 = i.a(b10 != null ? b10.getData() : null);
        if (a10 != null && a10.length() != 0) {
            z10 = false;
        }
        if (z10) {
            wg.t.e("Configuration", "ConfigurationStateManager", "Cached config is null/empty.", new Object[0]);
            return null;
        }
        try {
            return com.adobe.marketing.mobile.internal.util.d.c(new JSONObject(new JSONTokener(a10)));
        } catch (JSONException e10) {
            wg.t.a("Configuration", "ConfigurationStateManager", "Failed to load cached config " + e10, new Object[0]);
            return null;
        }
    }

    public final void b() {
        i0 f10 = i0.f();
        t.f(f10, "ServiceProvider.getInstance()");
        wg.v a10 = f10.d().a("AdobeMobile_ConfigState");
        t.f(a10, "ServiceProvider.getInsta…Collection(DATASTORE_KEY)");
        a10.remove("config.overridden.map");
        this.f32634d.clear();
        this.f32635e.clear();
        this.f32635e.putAll(this.f32633c);
        c();
        wg.t.e("Configuration", "ConfigurationStateManager", "Cleared programmatic configuration.", new Object[0]);
    }

    public final Map d(String filePath) {
        Map map;
        t.g(filePath, "filePath");
        String g10 = com.adobe.marketing.mobile.internal.util.c.g(new File(filePath));
        boolean z10 = true;
        if (g10 == null || g10.length() == 0) {
            wg.t.a("Configuration", "ConfigurationStateManager", "Empty configuration from file path while configuring with file path.", new Object[0]);
            return null;
        }
        try {
            map = com.adobe.marketing.mobile.internal.util.d.c(new JSONObject(new JSONTokener(g10)));
        } catch (JSONException unused) {
            wg.t.f("Configuration", "ConfigurationStateManager", "Failed to parse JSON config from file while configuring with file path.", new Object[0]);
            map = null;
        }
        if (map != null && !map.isEmpty()) {
            z10 = false;
        }
        if (!z10) {
            return map;
        }
        wg.t.a("Configuration", "ConfigurationStateManager", "Empty configuration found when processing JSON string.", new Object[0]);
        return null;
    }

    public final Map e() {
        return this.f32636f;
    }

    public final boolean h(String appId) {
        t.g(appId, "appId");
        Date date = (Date) this.f32637g.get(appId);
        return date == null || new Date(date.getTime() + 15000).compareTo(new Date()) < 0;
    }

    public final Map i(String bundledConfigFileName) {
        t.g(bundledConfigFileName, "bundledConfigFileName");
        wg.t.e("Configuration", "ConfigurationStateManager", "Attempting to load bundled config.", new Object[0]);
        i0 f10 = i0.f();
        t.f(f10, "ServiceProvider.getInstance()");
        String a10 = i.a(f10.e().k(bundledConfigFileName));
        if (a10 == null || a10.length() == 0) {
            wg.t.a("Configuration", "ConfigurationStateManager", "Bundled config asset is not present/is empty. Cannot load bundled config.", new Object[0]);
            return null;
        }
        try {
            return com.adobe.marketing.mobile.internal.util.d.c(new JSONObject(new JSONTokener(a10)));
        } catch (JSONException e10) {
            wg.t.a("Configuration", "ConfigurationStateManager", "Failed to load bundled config " + e10, new Object[0]);
            return null;
        }
    }

    public final Map k() {
        Map j10;
        this.f32635e.clear();
        String c10 = this.f32631a.c();
        if (c10 == null || c10.length() == 0) {
            wg.t.e("Configuration", "ConfigurationStateManager", "AppID from persistence and manifest is null.", new Object[0]);
            j10 = i("ADBMobileConfig.json");
        } else {
            j10 = j(c10);
            if (j10 == null) {
                j10 = i("ADBMobileConfig.json");
            }
        }
        l(j10);
        return this.f32636f;
    }

    public final void l(Map map) {
        this.f32633c.clear();
        if (map != null) {
            this.f32633c.putAll(map);
        }
        this.f32635e.clear();
        this.f32635e.putAll(this.f32633c);
        this.f32635e.putAll(this.f32634d);
        c();
        wg.t.e("Configuration", "ConfigurationStateManager", "Replaced configuration.", new Object[0]);
    }

    public final void m(String appId, l completion) {
        boolean C;
        t.g(appId, "appId");
        t.g(completion, "completion");
        C = y.C(appId);
        if (C) {
            wg.t.e("Configuration", "ConfigurationStateManager", "Attempting to set empty App Id into persistence.", new Object[0]);
            return;
        }
        this.f32631a.e(appId);
        u0 u0Var = u0.f26205a;
        String format = String.format("https://assets.adobedtm.com/%s.json", Arrays.copyOf(new Object[]{appId}, 1));
        t.f(format, "java.lang.String.format(format, *args)");
        this.f32632b.b(format, new b(appId, completion));
    }

    public final boolean n(String fileAssetName) {
        t.g(fileAssetName, "fileAssetName");
        Map i10 = i(fileAssetName);
        if (i10 == null || i10.isEmpty()) {
            wg.t.a("Configuration", "ConfigurationStateManager", "Empty configuration found when processing JSON string.", new Object[0]);
            return false;
        }
        l(i10);
        return true;
    }

    public final boolean o(String filePath) {
        t.g(filePath, "filePath");
        Map d10 = d(filePath);
        if (d10 == null) {
            wg.t.a("Configuration", "ConfigurationStateManager", "Unable to read config from provided file (content is invalid)", new Object[0]);
            return false;
        }
        l(d10);
        return true;
    }

    public final void p(Map config) {
        t.g(config, "config");
        this.f32634d.putAll(config);
        i0 f10 = i0.f();
        t.f(f10, "ServiceProvider.getInstance()");
        wg.v a10 = f10.d().a("AdobeMobile_ConfigState");
        t.f(a10, "ServiceProvider.getInsta…Collection(DATASTORE_KEY)");
        String jSONObject = new JSONObject(this.f32634d).toString();
        t.f(jSONObject, "JSONObject(programmaticConfiguration).toString()");
        a10.e("config.overridden.map", jSONObject);
        this.f32635e.putAll(this.f32634d);
        c();
        wg.t.a("Configuration", "ConfigurationStateManager", "Updated programmatic configuration.", new Object[0]);
    }

    public d(pg.a appIdManager, pg.b configDownloader) {
        Map i10;
        t.g(appIdManager, "appIdManager");
        t.g(configDownloader, "configDownloader");
        this.f32633c = new LinkedHashMap();
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        this.f32634d = linkedHashMap;
        this.f32635e = new LinkedHashMap();
        i10 = q0.i();
        this.f32636f = i10;
        this.f32637g = new LinkedHashMap();
        this.f32631a = appIdManager;
        this.f32632b = configDownloader;
        Map g10 = g();
        if (g10 != null) {
            linkedHashMap.putAll(g10);
        }
    }
}
