package com.google.firebase.sessions.settings;

import A4.d;
import A4.j;
import C4.c;
import C4.e;
import O1.InterfaceC0569i;
import T4.b;
import V4.E;
import android.os.Build;
import android.util.Log;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.sessions.ApplicationInfo;
import com.google.firebase.sessions.InstallationId;
import e4.C;
import e5.a;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Pattern;
import kotlin.jvm.internal.o;
import m1.g;
import m3.AbstractC2467b;
import w4.C3137A;
import w4.h;
import w4.k;
import x4.AbstractC3229F;

/* JADX INFO: loaded from: classes3.dex */
public final class RemoteSettings implements SettingsProvider {
    private static final Companion Companion = new Companion(null);

    @Deprecated
    public static final String FORWARD_SLASH_STRING = "/";

    @Deprecated
    public static final String TAG = "SessionConfigFetcher";
    private final ApplicationInfo appInfo;
    private final j backgroundDispatcher;
    private final CrashlyticsSettingsFetcher configsFetcher;
    private final a fetchInProgress;
    private final FirebaseInstallationsApi firebaseInstallationsApi;
    private final h settingsCache$delegate;

    public static final class Companion {
        public /* synthetic */ Companion(kotlin.jvm.internal.h hVar) {
            this();
        }

        private Companion() {
        }
    }

    /* JADX INFO: renamed from: com.google.firebase.sessions.settings.RemoteSettings$updateSettings$1, reason: invalid class name */
    @e(c = "com.google.firebase.sessions.settings.RemoteSettings", f = "RemoteSettings.kt", l = {170, 76, 94}, m = "updateSettings")
    public static final class AnonymousClass1 extends c {
        Object L$0;
        Object L$1;
        int label;
        /* synthetic */ Object result;

        public AnonymousClass1(d dVar) {
            super(dVar);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return RemoteSettings.this.updateSettings(this);
        }
    }

    public RemoteSettings(j backgroundDispatcher, FirebaseInstallationsApi firebaseInstallationsApi, ApplicationInfo appInfo, CrashlyticsSettingsFetcher configsFetcher, InterfaceC0569i dataStore) {
        o.h(backgroundDispatcher, "backgroundDispatcher");
        o.h(firebaseInstallationsApi, "firebaseInstallationsApi");
        o.h(appInfo, "appInfo");
        o.h(configsFetcher, "configsFetcher");
        o.h(dataStore, "dataStore");
        this.backgroundDispatcher = backgroundDispatcher;
        this.firebaseInstallationsApi = firebaseInstallationsApi;
        this.appInfo = appInfo;
        this.configsFetcher = configsFetcher;
        this.settingsCache$delegate = g.k(new RemoteSettings$settingsCache$2(dataStore));
        this.fetchInProgress = e5.d.a();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final SettingsCache getSettingsCache() {
        return (SettingsCache) this.settingsCache$delegate.getValue();
    }

    private final String removeForwardSlashesIn(String input) {
        Pattern patternCompile = Pattern.compile(FORWARD_SLASH_STRING);
        o.g(patternCompile, "compile(...)");
        o.h(input, "input");
        String strReplaceAll = patternCompile.matcher(input).replaceAll("");
        o.g(strReplaceAll, "replaceAll(...)");
        return strReplaceAll;
    }

    public final void clearCachedSettings$com_google_firebase_firebase_sessions() {
        E.y(E.c(this.backgroundDispatcher), null, null, new RemoteSettings$clearCachedSettings$1(this, null), 3);
    }

    @Override // com.google.firebase.sessions.settings.SettingsProvider
    public Double getSamplingRate() {
        return getSettingsCache().sessionSamplingRate();
    }

    @Override // com.google.firebase.sessions.settings.SettingsProvider
    public Boolean getSessionEnabled() {
        return getSettingsCache().sessionsEnabled();
    }

    @Override // com.google.firebase.sessions.settings.SettingsProvider
    /* JADX INFO: renamed from: getSessionRestartTimeout-FghU774 */
    public b mo356getSessionRestartTimeoutFghU774() {
        Integer numSessionRestartTimeout = getSettingsCache().sessionRestartTimeout();
        if (numSessionRestartTimeout == null) {
            return null;
        }
        T4.a aVar = b.f6709b;
        return new b(C.i0(numSessionRestartTimeout.intValue(), T4.d.f6716d));
    }

    @Override // com.google.firebase.sessions.settings.SettingsProvider
    public boolean isSettingsStale() {
        return getSettingsCache().hasCacheExpired$com_google_firebase_firebase_sessions();
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:45:0x00c3 A[Catch: all -> 0x0052, TRY_LEAVE, TryCatch #0 {all -> 0x0052, blocks: (B:21:0x004e, B:43:0x00b5, B:45:0x00c3, B:48:0x00ce, B:35:0x008e, B:37:0x0098, B:40:0x00a3), top: B:56:0x002a }] */
    /* JADX WARN: Removed duplicated region for block: B:48:0x00ce A[Catch: all -> 0x0052, TRY_ENTER, TRY_LEAVE, TryCatch #0 {all -> 0x0052, blocks: (B:21:0x004e, B:43:0x00b5, B:45:0x00c3, B:48:0x00ce, B:35:0x008e, B:37:0x0098, B:40:0x00a3), top: B:56:0x002a }] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /* JADX WARN: Type inference failed for: r2v11, types: [e5.a] */
    /* JADX WARN: Type inference failed for: r2v14 */
    /* JADX WARN: Type inference failed for: r2v3 */
    /* JADX WARN: Type inference failed for: r2v4 */
    /* JADX WARN: Type inference failed for: r2v6 */
    /* JADX WARN: Type inference failed for: r2v7 */
    /* JADX WARN: Type inference failed for: r4v0, types: [int] */
    @Override // com.google.firebase.sessions.settings.SettingsProvider
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object updateSettings(d dVar) throws Throwable {
        AnonymousClass1 anonymousClass1;
        ?? r22;
        a aVar;
        RemoteSettings remoteSettings;
        String fid;
        ?? r23;
        if (dVar instanceof AnonymousClass1) {
            anonymousClass1 = (AnonymousClass1) dVar;
            int i6 = anonymousClass1.label;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                anonymousClass1.label = i6 - Integer.MIN_VALUE;
            } else {
                anonymousClass1 = new AnonymousClass1(dVar);
            }
        }
        Object objCreate = anonymousClass1.result;
        B4.a aVar2 = B4.a.f492a;
        ?? r42 = anonymousClass1.label;
        C3137A c3137a = C3137A.f25453a;
        try {
            if (r42 == 0) {
                AbstractC2467b.l(objCreate);
                if (!((e5.c) this.fetchInProgress).d() && !getSettingsCache().hasCacheExpired$com_google_firebase_firebase_sessions()) {
                    return c3137a;
                }
                a aVar3 = this.fetchInProgress;
                anonymousClass1.L$0 = this;
                anonymousClass1.L$1 = aVar3;
                anonymousClass1.label = 1;
                e5.c cVar = (e5.c) aVar3;
                if (cVar.e(anonymousClass1) != aVar2) {
                    aVar = cVar;
                    remoteSettings = this;
                }
                return aVar2;
            }
            if (r42 != 1) {
                if (r42 != 2) {
                    if (r42 != 3) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    r22 = (a) anonymousClass1.L$0;
                    try {
                        AbstractC2467b.l(objCreate);
                        r23 = r22;
                        ((e5.c) r23).g(null);
                        return c3137a;
                    } catch (Throwable th) {
                        th = th;
                        ((e5.c) r22).g(null);
                        throw th;
                    }
                }
                aVar = (a) anonymousClass1.L$1;
                remoteSettings = (RemoteSettings) anonymousClass1.L$0;
                AbstractC2467b.l(objCreate);
                fid = ((InstallationId) objCreate).getFid();
                if (!o.c(fid, "")) {
                    Log.w(TAG, "Error getting Firebase Installation ID. Skipping this Session Event.");
                    ((e5.c) aVar).g(null);
                    return c3137a;
                }
                k kVar = new k("X-Crashlytics-Installation-ID", fid);
                k kVar2 = new k("X-Crashlytics-Device-Model", remoteSettings.removeForwardSlashesIn(String.format("%s/%s", Arrays.copyOf(new Object[]{Build.MANUFACTURER, Build.MODEL}, 2))));
                String INCREMENTAL = Build.VERSION.INCREMENTAL;
                o.g(INCREMENTAL, "INCREMENTAL");
                k kVar3 = new k("X-Crashlytics-OS-Build-Version", remoteSettings.removeForwardSlashesIn(INCREMENTAL));
                String RELEASE = Build.VERSION.RELEASE;
                o.g(RELEASE, "RELEASE");
                Map<String, String> mapW = AbstractC3229F.w(kVar, kVar2, kVar3, new k("X-Crashlytics-OS-Display-Version", remoteSettings.removeForwardSlashesIn(RELEASE)), new k("X-Crashlytics-API-Client-Version", remoteSettings.appInfo.getSessionSdkVersion()));
                Log.d(TAG, "Fetching settings from server.");
                CrashlyticsSettingsFetcher crashlyticsSettingsFetcher = remoteSettings.configsFetcher;
                RemoteSettings$updateSettings$2$1 remoteSettings$updateSettings$2$1 = new RemoteSettings$updateSettings$2$1(remoteSettings, null);
                RemoteSettings$updateSettings$2$2 remoteSettings$updateSettings$2$2 = new RemoteSettings$updateSettings$2$2(null);
                anonymousClass1.L$0 = aVar;
                anonymousClass1.L$1 = null;
                anonymousClass1.label = 3;
                if (crashlyticsSettingsFetcher.doConfigFetch(mapW, remoteSettings$updateSettings$2$1, remoteSettings$updateSettings$2$2, anonymousClass1) != aVar2) {
                    r23 = aVar;
                    ((e5.c) r23).g(null);
                    return c3137a;
                }
                return aVar2;
            }
            aVar = (a) anonymousClass1.L$1;
            remoteSettings = (RemoteSettings) anonymousClass1.L$0;
            AbstractC2467b.l(objCreate);
            if (!remoteSettings.getSettingsCache().hasCacheExpired$com_google_firebase_firebase_sessions()) {
                Log.d(TAG, "Remote settings cache not expired. Using cached values.");
                ((e5.c) aVar).g(null);
                return c3137a;
            }
            InstallationId.Companion companion = InstallationId.Companion;
            FirebaseInstallationsApi firebaseInstallationsApi = remoteSettings.firebaseInstallationsApi;
            anonymousClass1.L$0 = remoteSettings;
            anonymousClass1.L$1 = aVar;
            anonymousClass1.label = 2;
            objCreate = companion.create(firebaseInstallationsApi, anonymousClass1);
            if (objCreate != aVar2) {
                fid = ((InstallationId) objCreate).getFid();
                if (!o.c(fid, "")) {
                }
            }
            return aVar2;
        } catch (Throwable th2) {
            th = th2;
            r22 = r42;
        }
    }
}
