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;

/* renamed from: ᵸᵮ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C4995 {

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public static final int[] f18535 = {2, 4, 8, 16, 32, 64, 128, 256};

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final C0382 f18536;

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public final C4093 f18537;

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public final ConfigFetchHttpClient f18538;

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final Executor f18539;

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final InterfaceC0466 f18540;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final InterfaceC3290 f18541;

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public final Map f18542;

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final Random f18543;

    public C4995(InterfaceC0466 interfaceC0466, InterfaceC3290 interfaceC3290, Executor executor, Random random, C0382 c0382, ConfigFetchHttpClient configFetchHttpClient, C4093 c4093, HashMap map) {
        this.f18540 = interfaceC0466;
        this.f18541 = interfaceC3290;
        this.f18539 = executor;
        this.f18543 = random;
        this.f18536 = c0382;
        this.f18538 = configFetchHttpClient;
        this.f18537 = c4093;
        this.f18542 = map;
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final C3921 m10447(int i) {
        HashMap map = new HashMap(this.f18542);
        map.put("X-Firebase-RC-Fetch-Type", "REALTIME/" + i);
        return this.f18536.m1831().m8703(this.f18539, new C3376(this, 2, map));
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final C0063 m10448(String str, String str2, Date date, HashMap map) throws FirebaseRemoteConfigFetchThrottledException, FirebaseRemoteConfigClientException, FirebaseRemoteConfigServerException {
        String str3;
        try {
            HttpURLConnection httpURLConnectionM1003 = this.f18538.m1003();
            ConfigFetchHttpClient configFetchHttpClient = this.f18538;
            HashMap mapM10450 = m10450();
            String string = this.f18537.f15298.getString("last_fetch_etag", null);
            InterfaceC2211 interfaceC2211 = (InterfaceC2211) this.f18541.get();
            C0063 c0063Fetch = configFetchHttpClient.fetch(httpURLConnectionM1003, str, str2, mapM10450, string, map, interfaceC2211 != null ? (Long) ((C0818) interfaceC2211).f3785.f1029.m4514(null, null, true).get("_fot") : null, date, this.f18537.m8975());
            C2275 c2275 = c0063Fetch.f1283;
            if (c2275 != null) {
                C4093 c4093 = this.f18537;
                long j = c2275.f8893;
                synchronized (c4093.f15299) {
                    c4093.f15298.edit().putLong("last_template_version", j).apply();
                }
            }
            String str4 = c0063Fetch.f1281;
            if (str4 != null) {
                this.f18537.m8969(str4);
            }
            this.f18537.m8977(0, C4093.f15294);
            return c0063Fetch;
        } catch (FirebaseRemoteConfigServerException e) {
            int i = e.f1128;
            C4093 c40932 = this.f18537;
            if (i == 429 || i == 502 || i == 503 || i == 504) {
                c40932.m8977(c40932.m8974().f15648 + 1, new Date(date.getTime() + (TimeUnit.MINUTES.toMillis(f18535[Math.min(r14, 8) - 1]) / 2) + this.f18543.nextInt((int) r3)));
            }
            C4182 c4182M8974 = c40932.m8974();
            int i2 = e.f1128;
            if (c4182M8974.f15648 > 1 || i2 == 429) {
                c4182M8974.f15649.getTime();
                throw new FirebaseRemoteConfigFetchThrottledException("Fetch was throttled.");
            }
            if (i2 == 401) {
                str3 = "The request did not have the required credentials. Please make sure your google-services.json is valid.";
            } else if (i2 == 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 (i2 == 429) {
                    throw new FirebaseRemoteConfigClientException("The throttled response from the server was not handled correctly by the FRC SDK.");
                }
                if (i2 != 500) {
                    switch (i2) {
                        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);
        }
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final C3921 m10449(C3921 c3921, long j, final HashMap map) {
        final C4995 c4995;
        C3921 c3921M8703;
        final Date date = new Date(System.currentTimeMillis());
        boolean zM8709 = c3921.m8709();
        C4093 c4093 = this.f18537;
        if (zM8709) {
            Date date2 = new Date(c4093.f15298.getLong("last_fetch_time_in_millis", -1L));
            if (date2.equals(C4093.f15295) ? false : date.before(new Date(TimeUnit.SECONDS.toMillis(j) + date2.getTime()))) {
                return ʼٴ.ˑᵥˁ(new C0063(2, null, null));
            }
        }
        Date date3 = c4093.m8974().f15649;
        Date date4 = date.before(date3) ? date3 : null;
        Executor executor = this.f18539;
        if (date4 != null) {
            String str = "Fetch is throttled. Please wait before calling fetch again: " + DateUtils.formatElapsedTime((date4.getTime() - date.getTime()) / 1000);
            date4.getTime();
            c3921M8703 = ʼٴ.ᵡˎ(new FirebaseRemoteConfigFetchThrottledException(str));
            c4995 = this;
        } else {
            C2005 c2005 = (C2005) this.f18540;
            final C3921 c3921M5536 = c2005.m5536();
            final C3921 c3921M5524 = c2005.m5524();
            c4995 = this;
            c3921M8703 = ʼٴ.ﹶـ(new C3921[]{c3921M5536, c3921M5524}).m8703(executor, new InterfaceC3610() { // from class: ﹶˑ
                @Override // defpackage.InterfaceC3610
                /* renamed from: ᵝᵵᵺ */
                public final Object mo1888(C3921 c39212) {
                    C4995 c49952 = this.f20884;
                    Date date5 = date;
                    HashMap map2 = map;
                    C3921 c39213 = c3921M5536;
                    if (!c39213.m8709()) {
                        return ʼٴ.ᵡˎ(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation ID for fetch.", c39213.m8707()));
                    }
                    C3921 c39214 = c3921M5524;
                    if (!c39214.m8709()) {
                        return ʼٴ.ᵡˎ(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation auth token for fetch.", c39214.m8707()));
                    }
                    try {
                        C0063 c0063M10448 = c49952.m10448((String) c39213.m8706(), ((C4444) c39214.m8706()).f16596, date5, map2);
                        return c0063M10448.f1282 != 0 ? ʼٴ.ˑᵥˁ(c0063M10448) : c49952.f18536.m1829(c0063M10448.f1283).m8715(c49952.f18539, new ʾᵛ(6, c0063M10448));
                    } catch (FirebaseRemoteConfigException e) {
                        return ʼٴ.ᵡˎ(e);
                    }
                }
            });
        }
        return c3921M8703.m8703(executor, new C3376(c4995, 3, date));
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final HashMap m10450() {
        HashMap map = new HashMap();
        InterfaceC2211 interfaceC2211 = (InterfaceC2211) this.f18541.get();
        if (interfaceC2211 != null) {
            for (Map.Entry entry : ((C0818) interfaceC2211).f3785.f1029.m4514(null, null, false).entrySet()) {
                map.put((String) entry.getKey(), entry.getValue().toString());
            }
        }
        return map;
    }
}
