package defpackage;

import android.text.format.DateUtils;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigClientException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigFetchThrottledException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigServerException;
import com.google.firebase.remoteconfig.internal.ConfigFetchHttpClient;
import java.net.HttpURLConnection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

/* JADX INFO: renamed from: ᵭ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C4107 {

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public static final int[] f15726 = {2, 4, 8, 16, 32, 64, 128, 256};

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final InterfaceC5081 f15727;

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public final Map f15728;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final Executor f15729;

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final C0708 f15730;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final InterfaceC4093 f15731;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final C4331 f15732;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final Random f15733;

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final ConfigFetchHttpClient f15734;

    public C4107(InterfaceC4093 interfaceC4093, InterfaceC5081 interfaceC5081, Executor executor, Random random, C0708 c0708, ConfigFetchHttpClient configFetchHttpClient, C4331 c4331, HashMap map) {
        this.f15731 = interfaceC4093;
        this.f15727 = interfaceC5081;
        this.f15729 = executor;
        this.f15733 = random;
        this.f15730 = c0708;
        this.f15734 = configFetchHttpClient;
        this.f15732 = c4331;
        this.f15728 = map;
    }

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final C1300 m8962(C1300 c1300, long j, final HashMap map) {
        final C4107 c4107;
        C1300 c1300M3821;
        final Date date = new Date(System.currentTimeMillis());
        boolean zM3829 = c1300.m3829();
        C4331 c4331 = this.f15732;
        if (zM3829) {
            Date date2 = new Date(c4331.f16493.getLong("last_fetch_time_in_millis", -1L));
            if (date2.equals(C4331.f16489) ? false : date.before(new Date(TimeUnit.SECONDS.toMillis(j) + date2.getTime()))) {
                return ᵥˈᵛ.ᵸˆˁ(new C3025(2, null, null));
            }
        }
        Date date3 = c4331.m9314().f5711;
        Date date4 = date.before(date3) ? date3 : null;
        Executor executor = this.f15729;
        if (date4 != null) {
            String str = "Fetch is throttled. Please wait before calling fetch again: " + DateUtils.formatElapsedTime((date4.getTime() - date.getTime()) / 1000);
            date4.getTime();
            c1300M3821 = ᵥˈᵛ.ˌᵨʠᵫ(new FirebaseRemoteConfigFetchThrottledException(str));
            c4107 = this;
        } else {
            C3983 c3983 = (C3983) this.f15731;
            final C1300 c1300M8761 = c3983.m8761();
            final C1300 c1300M8755 = c3983.m8755();
            c4107 = this;
            c1300M3821 = ᵥˈᵛ.ˋʿᐧ(new C1300[]{c1300M8761, c1300M8755}).m3821(executor, new InterfaceC5156() { // from class: ˊˏᵥ
                @Override // defpackage.InterfaceC5156
                /* JADX INFO: renamed from: ˊᵫᵗ */
                public final Object mo2607(C1300 c13002) {
                    C4107 c41072 = this.f5572;
                    Date date5 = date;
                    HashMap map2 = map;
                    C1300 c13003 = c1300M8761;
                    if (!c13003.m3829()) {
                        return ᵥˈᵛ.ˌᵨʠᵫ(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation ID for fetch.", c13003.m3830()));
                    }
                    C1300 c13004 = c1300M8755;
                    if (!c13004.m3829()) {
                        return ᵥˈᵛ.ˌᵨʠᵫ(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation auth token for fetch.", c13004.m3830()));
                    }
                    try {
                        C3025 c3025M8964 = c41072.m8964((String) c13003.m3826(), ((C3494) c13004.m3826()).f13442, date5, map2);
                        return c3025M8964.f11643 != 0 ? ᵥˈᵛ.ᵸˆˁ(c3025M8964) : c41072.f15730.m2562(c3025M8964.f11641).m3824(c41072.f15729, new ᵳᵢᵺ(6, c3025M8964));
                    } catch (FirebaseRemoteConfigException e) {
                        return ᵥˈᵛ.ˌᵨʠᵫ(e);
                    }
                }
            });
        }
        return c1300M3821.m3821(executor, new C0732(c4107, 3, date));
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final C1300 m8963(int i) {
        HashMap map = new HashMap(this.f15728);
        map.put("X-Firebase-RC-Fetch-Type", "REALTIME/" + i);
        return this.f15730.m2560().m3821(this.f15729, new C0732(this, 2, map));
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final C3025 m8964(String str, String str2, Date date, HashMap map) throws FirebaseRemoteConfigFetchThrottledException, FirebaseRemoteConfigClientException, FirebaseRemoteConfigServerException {
        String str3;
        try {
            HttpURLConnection httpURLConnectionM1003 = this.f15734.m1003();
            ConfigFetchHttpClient configFetchHttpClient = this.f15734;
            HashMap mapM8965 = m8965();
            String string = this.f15732.f16493.getString("last_fetch_etag", null);
            InterfaceC5129 interfaceC5129 = (InterfaceC5129) this.f15727.get();
            C3025 c3025Fetch = configFetchHttpClient.fetch(httpURLConnectionM1003, str, str2, mapM8965, string, map, interfaceC5129 != null ? (Long) ((C2342) interfaceC5129).f9202.f1029.m7047(null, null, true).get("_fot") : null, date, this.f15732.m9310());
            C5235 c5235 = c3025Fetch.f11641;
            if (c5235 != null) {
                C4331 c4331 = this.f15732;
                long j = c5235.f19742;
                synchronized (c4331.f16490) {
                    c4331.f16493.edit().putLong("last_template_version", j).apply();
                }
            }
            String str4 = c3025Fetch.f11642;
            if (str4 != null) {
                this.f15732.m9313(str4);
            }
            this.f15732.m9316(0, C4331.f16488);
            return c3025Fetch;
        } catch (FirebaseRemoteConfigServerException e) {
            int i = e.f1128;
            C4331 c43312 = this.f15732;
            if (i == 429 || i == 502 || i == 503 || i == 504) {
                int i2 = c43312.m9314().f5712 + 1;
                long millis = TimeUnit.MINUTES.toMillis(f15726[Math.min(i2, 8) - 1]);
                c43312.m9316(i2, new Date(date.getTime() + (millis / 2) + ((long) this.f15733.nextInt((int) millis))));
            }
            C1377 c1377M9314 = c43312.m9314();
            int i3 = e.f1128;
            if (c1377M9314.f5712 > 1 || i3 == 429) {
                c1377M9314.f5711.getTime();
                throw new FirebaseRemoteConfigFetchThrottledException("Fetch was throttled.");
            }
            if (i3 == 401) {
                str3 = "The request did not have the required credentials. Please make sure your google-services.json is valid.";
            } else if (i3 == 403) {
                str3 = "The user is not authorized to access the project. Please make sure you are using the API key that corresponds to your Firebase project.";
            } else {
                if (i3 == 429) {
                    throw new FirebaseRemoteConfigClientException("The throttled response from the server was not handled correctly by the FRC SDK.");
                }
                if (i3 != 500) {
                    switch (i3) {
                        case 502:
                        case 503:
                        case 504:
                            str3 = "The server is unavailable. Please try again later.";
                            break;
                        default:
                            str3 = "The server returned an unexpected error.";
                            break;
                    }
                } else {
                    str3 = "There was an internal server error.";
                }
            }
            throw new FirebaseRemoteConfigServerException(e.f1128, "Fetch failed: ".concat(str3), e);
        }
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final HashMap m8965() {
        HashMap map = new HashMap();
        InterfaceC5129 interfaceC5129 = (InterfaceC5129) this.f15727.get();
        if (interfaceC5129 != null) {
            for (Map.Entry entry : ((C2342) interfaceC5129).f9202.f1029.m7047(null, null, false).entrySet()) {
                map.put((String) entry.getKey(), entry.getValue().toString());
            }
        }
        return map;
    }
}
