package M4;

import A1.G;
import a4.C0400c;
import a4.InterfaceC0399b;
import android.text.format.DateUtils;
import com.google.android.gms.internal.measurement.C0662l0;
import com.google.firebase.remoteconfig.internal.ConfigFetchHttpClient;
import g3.InterfaceC0983a;
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: loaded from: classes.dex */
public final class i {
    public static final long i = TimeUnit.HOURS.toSeconds(12);

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final int[] f4246j = {2, 4, 8, 16, 32, 64, 128, 256};

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final E4.e f4247a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final D4.b f4248b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Executor f4249c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Random f4250d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final d f4251e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final ConfigFetchHttpClient f4252f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final o f4253g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final HashMap f4254h;

    public i(E4.e eVar, D4.b bVar, Executor executor, Random random, d dVar, ConfigFetchHttpClient configFetchHttpClient, o oVar, HashMap map) {
        this.f4247a = eVar;
        this.f4248b = bVar;
        this.f4249c = executor;
        this.f4250d = random;
        this.f4251e = dVar;
        this.f4252f = configFetchHttpClient;
        this.f4253g = oVar;
        this.f4254h = map;
    }

    public final h a(String str, String str2, Date date, HashMap map) throws L4.g, L4.f, L4.d {
        String str3;
        try {
            HttpURLConnection httpURLConnectionB = this.f4252f.b();
            ConfigFetchHttpClient configFetchHttpClient = this.f4252f;
            HashMap mapD = d();
            String string = this.f4253g.f4285a.getString("last_fetch_etag", null);
            InterfaceC0399b interfaceC0399b = (InterfaceC0399b) this.f4248b.get();
            h hVarFetch = configFetchHttpClient.fetch(httpURLConnectionB, str, str2, mapD, string, map, interfaceC0399b != null ? (Long) ((C0662l0) ((C0400c) interfaceC0399b).f7612a.f5246r).f(null, null, true).get("_fot") : null, date, this.f4253g.b());
            f fVar = hVarFetch.f4244b;
            if (fVar != null) {
                o oVar = this.f4253g;
                long j8 = fVar.f4236f;
                synchronized (oVar.f4286b) {
                    oVar.f4285a.edit().putLong("last_template_version", j8).apply();
                }
            }
            String str4 = hVarFetch.f4245c;
            if (str4 != null) {
                this.f4253g.e(str4);
            }
            this.f4253g.d(0, o.f4284f);
            return hVarFetch;
        } catch (L4.g e8) {
            int i5 = e8.f4005q;
            o oVar2 = this.f4253g;
            if (i5 == 429 || i5 == 502 || i5 == 503 || i5 == 504) {
                int i8 = oVar2.a().f4281a + 1;
                TimeUnit timeUnit = TimeUnit.MINUTES;
                int[] iArr = f4246j;
                long millis = timeUnit.toMillis(iArr[Math.min(i8, iArr.length) - 1]);
                oVar2.d(i8, new Date(date.getTime() + (millis / 2) + ((long) this.f4250d.nextInt((int) millis))));
            }
            n nVarA = oVar2.a();
            int i9 = e8.f4005q;
            if (nVarA.f4281a > 1 || i9 == 429) {
                nVarA.f4282b.getTime();
                throw new L4.f("Fetch was throttled.");
            }
            if (i9 == 401) {
                str3 = "The request did not have the required credentials. Please make sure your google-services.json is valid.";
            } else if (i9 == 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 (i9 == 429) {
                    throw new L4.d("The throttled response from the server was not handled correctly by the FRC SDK.");
                }
                if (i9 != 500) {
                    switch (i9) {
                        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 L4.g(e8.f4005q, "Fetch failed: ".concat(str3), e8);
        }
    }

    public final g3.o b(g3.o oVar, long j8, final HashMap map) {
        g3.o oVarE;
        final Date date = new Date(System.currentTimeMillis());
        boolean zI = oVar.i();
        o oVar2 = this.f4253g;
        if (zI) {
            Date date2 = new Date(oVar2.f4285a.getLong("last_fetch_time_in_millis", -1L));
            if (date2.equals(o.f4283e) ? false : date.before(new Date(TimeUnit.SECONDS.toMillis(j8) + date2.getTime()))) {
                return W2.g.p(new h(2, null, null));
            }
        }
        Date date3 = oVar2.a().f4282b;
        Date date4 = date.before(date3) ? date3 : null;
        Executor executor = this.f4249c;
        if (date4 != null) {
            String str = "Fetch is throttled. Please wait before calling fetch again: " + DateUtils.formatElapsedTime(TimeUnit.MILLISECONDS.toSeconds(date4.getTime() - date.getTime()));
            date4.getTime();
            oVarE = W2.g.o(new L4.f(str));
        } else {
            E4.d dVar = (E4.d) this.f4247a;
            final g3.o oVarD = dVar.d();
            final g3.o oVarF = dVar.f();
            oVarE = W2.g.L(oVarD, oVarF).e(executor, new InterfaceC0983a() { // from class: M4.g
                @Override // g3.InterfaceC0983a
                public final Object e(g3.o oVar3) {
                    Date date5 = date;
                    HashMap map2 = map;
                    i iVar = this.f4238q;
                    iVar.getClass();
                    g3.o oVar4 = oVarD;
                    if (!oVar4.i()) {
                        return W2.g.o(new L4.d("Firebase Installations failed to get installation ID for fetch.", oVar4.f()));
                    }
                    g3.o oVar5 = oVarF;
                    if (!oVar5.i()) {
                        return W2.g.o(new L4.d("Firebase Installations failed to get installation auth token for fetch.", oVar5.f()));
                    }
                    try {
                        h hVarA = iVar.a((String) oVar4.g(), ((E4.a) oVar5.g()).f2181a, date5, map2);
                        if (hVarA.f4243a != 0) {
                            return W2.g.p(hVarA);
                        }
                        d dVar2 = iVar.f4251e;
                        f fVar = hVarA.f4244b;
                        dVar2.getClass();
                        L4.b bVar = new L4.b(dVar2, 1, fVar);
                        Executor executor2 = dVar2.f4221a;
                        return W2.g.f(executor2, bVar).j(executor2, new C0.b(dVar2, 8, fVar)).j(iVar.f4249c, new G(18, hVarA));
                    } catch (L4.e e8) {
                        return W2.g.o(e8);
                    }
                }
            });
        }
        return oVarE.e(executor, new C0.b(this, 10, date));
    }

    public final g3.o c(int i5) {
        HashMap map = new HashMap(this.f4254h);
        map.put("X-Firebase-RC-Fetch-Type", "REALTIME/" + i5);
        return this.f4251e.b().e(this.f4249c, new C0.b(this, 9, map));
    }

    public final HashMap d() {
        HashMap map = new HashMap();
        InterfaceC0399b interfaceC0399b = (InterfaceC0399b) this.f4248b.get();
        if (interfaceC0399b != null) {
            for (Map.Entry entry : ((C0662l0) ((C0400c) interfaceC0399b).f7612a.f5246r).f(null, null, false).entrySet()) {
                map.put((String) entry.getKey(), entry.getValue().toString());
            }
        }
        return map;
    }
}
