package com.google.firebase.sessions.settings;

import A4.d;
import A4.j;
import B4.a;
import C4.e;
import V4.C;
import V4.E;
import android.net.Uri;
import com.google.firebase.sessions.ApplicationInfo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
import org.json.JSONObject;
import w4.C3137A;

/* JADX INFO: loaded from: classes3.dex */
public final class RemoteSettingsFetcher implements CrashlyticsSettingsFetcher {
    public static final Companion Companion = new Companion(null);
    private static final String FIREBASE_PLATFORM = "android";
    private static final String FIREBASE_SESSIONS_BASE_URL_STRING = "firebase-settings.crashlytics.com";
    private final ApplicationInfo appInfo;
    private final String baseUrl;
    private final j blockingDispatcher;

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

        private Companion() {
        }
    }

    /* JADX INFO: renamed from: com.google.firebase.sessions.settings.RemoteSettingsFetcher$doConfigFetch$2, reason: invalid class name */
    @e(c = "com.google.firebase.sessions.settings.RemoteSettingsFetcher$doConfigFetch$2", f = "RemoteSettingsFetcher.kt", l = {68, 70, 73}, m = "invokeSuspend")
    public static final class AnonymousClass2 extends C4.j implements J4.e {
        final /* synthetic */ Map<String, String> $headerOptions;
        final /* synthetic */ J4.e $onFailure;
        final /* synthetic */ J4.e $onSuccess;
        int label;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass2(Map<String, String> map, J4.e eVar, J4.e eVar2, d dVar) {
            super(2, dVar);
            this.$headerOptions = map;
            this.$onSuccess = eVar;
            this.$onFailure = eVar2;
        }

        @Override // C4.a
        public final d create(Object obj, d dVar) {
            return RemoteSettingsFetcher.this.new AnonymousClass2(this.$headerOptions, this.$onSuccess, this.$onFailure, dVar);
        }

        @Override // J4.e
        public final Object invoke(C c6, d dVar) {
            return ((AnonymousClass2) create(c6, dVar)).invokeSuspend(C3137A.f25453a);
        }

        /* JADX WARN: Code restructure failed: missing block: B:29:0x00c2, code lost:
        
            if (r8.invoke(r1, r7) == r0) goto L36;
         */
        /* JADX WARN: Code restructure failed: missing block: B:35:0x00d7, code lost:
        
            if (r1.invoke(r3, r7) != r0) goto L37;
         */
        @Override // C4.a
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final Object invokeSuspend(Object obj) {
            a aVar = a.f492a;
            int i6 = this.label;
            try {
            } catch (Exception e6) {
                J4.e eVar = this.$onFailure;
                String message = e6.getMessage();
                if (message == null) {
                    message = e6.toString();
                }
                this.label = 3;
            }
            if (i6 == 0) {
                AbstractC2467b.l(obj);
                URLConnection uRLConnectionOpenConnection = RemoteSettingsFetcher.this.settingsUrl().openConnection();
                o.f(uRLConnectionOpenConnection, "null cannot be cast to non-null type javax.net.ssl.HttpsURLConnection");
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) uRLConnectionOpenConnection;
                httpsURLConnection.setRequestMethod("GET");
                httpsURLConnection.setRequestProperty("Accept", "application/json");
                for (Map.Entry<String, String> entry : this.$headerOptions.entrySet()) {
                    httpsURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
                }
                int responseCode = httpsURLConnection.getResponseCode();
                if (responseCode == 200) {
                    InputStream inputStream = httpsURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder sb = new StringBuilder();
                    while (true) {
                        String line = bufferedReader.readLine();
                        if (line == null) {
                            break;
                        }
                        sb.append(line);
                    }
                    bufferedReader.close();
                    inputStream.close();
                    JSONObject jSONObject = new JSONObject(sb.toString());
                    J4.e eVar2 = this.$onSuccess;
                    this.label = 1;
                    if (eVar2.invoke(jSONObject, this) == aVar) {
                        return aVar;
                    }
                } else {
                    J4.e eVar3 = this.$onFailure;
                    String str = "Bad response code: " + responseCode;
                    this.label = 2;
                }
            } else {
                if (i6 != 1 && i6 != 2 && i6 != 3) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                AbstractC2467b.l(obj);
            }
            return C3137A.f25453a;
        }
    }

    public RemoteSettingsFetcher(ApplicationInfo appInfo, j blockingDispatcher, String baseUrl) {
        o.h(appInfo, "appInfo");
        o.h(blockingDispatcher, "blockingDispatcher");
        o.h(baseUrl, "baseUrl");
        this.appInfo = appInfo;
        this.blockingDispatcher = blockingDispatcher;
        this.baseUrl = baseUrl;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final URL settingsUrl() {
        return new URL(new Uri.Builder().scheme("https").authority(this.baseUrl).appendPath("spi").appendPath("v2").appendPath("platforms").appendPath(FIREBASE_PLATFORM).appendPath("gmp").appendPath(this.appInfo.getAppId()).appendPath("settings").appendQueryParameter("build_version", this.appInfo.getAndroidAppInfo().getAppBuildVersion()).appendQueryParameter("display_version", this.appInfo.getAndroidAppInfo().getVersionName()).build().toString());
    }

    @Override // com.google.firebase.sessions.settings.CrashlyticsSettingsFetcher
    public Object doConfigFetch(Map<String, String> map, J4.e eVar, J4.e eVar2, d dVar) {
        Object objH = E.H(this.blockingDispatcher, new AnonymousClass2(map, eVar, eVar2, null), dVar);
        return objH == a.f492a ? objH : C3137A.f25453a;
    }

    public /* synthetic */ RemoteSettingsFetcher(ApplicationInfo applicationInfo, j jVar, String str, int i6, h hVar) {
        this(applicationInfo, jVar, (i6 & 4) != 0 ? FIREBASE_SESSIONS_BASE_URL_STRING : str);
    }
}
