package com.google.firebase.firestore.remote;

import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApiNotAvailableException;
import com.google.firebase.firestore.auth.CredentialsProvider;
import com.google.firebase.firestore.auth.User;
import com.google.firebase.firestore.util.Executors;
import com.google.firebase.firestore.util.Logger;
import com.google.firebase.internal.api.FirebaseNoSignedInUserException;
import java.util.BitSet;
import java.util.concurrent.Executor;
import k4.AbstractC2390c;
import k4.AbstractC2391d;
import k4.AbstractC2392e;
import k4.C2401n;
import k4.b0;
import k4.d0;
import k4.g0;
import k4.r0;

/* JADX INFO: loaded from: classes3.dex */
final class FirestoreCallCredentials extends AbstractC2392e {
    private static final d0 AUTHORIZATION_HEADER;
    private static final String LOG_TAG = "FirestoreCallCredentials";
    private static final d0 X_FIREBASE_APPCHECK;
    private final CredentialsProvider<String> appCheckProvider;
    private final CredentialsProvider<User> authProvider;

    static {
        C2401n c2401n = g0.f19867d;
        BitSet bitSet = d0.f19844d;
        AUTHORIZATION_HEADER = new b0("Authorization", c2401n);
        X_FIREBASE_APPCHECK = new b0("x-firebase-appcheck", c2401n);
    }

    public FirestoreCallCredentials(CredentialsProvider<User> credentialsProvider, CredentialsProvider<String> credentialsProvider2) {
        this.authProvider = credentialsProvider;
        this.appCheckProvider = credentialsProvider2;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void lambda$applyRequestMetadata$0(Task task, AbstractC2390c abstractC2390c, Task task2, Task task3) {
        g0 g0Var = new g0();
        if (task.isSuccessful()) {
            String str = (String) task.getResult();
            Logger.debug(LOG_TAG, "Successfully fetched auth token.", new Object[0]);
            if (str != null) {
                g0Var.f(AUTHORIZATION_HEADER, "Bearer ".concat(str));
            }
        } else {
            Exception exception = task.getException();
            if (exception instanceof FirebaseApiNotAvailableException) {
                Logger.debug(LOG_TAG, "Firebase Auth API not available, not using authentication.", new Object[0]);
            } else {
                if (!(exception instanceof FirebaseNoSignedInUserException)) {
                    Logger.warn(LOG_TAG, "Failed to get auth token: %s.", exception);
                    abstractC2390c.b(r0.f19943j.f(exception));
                    return;
                }
                Logger.debug(LOG_TAG, "No user signed in, not using authentication.", new Object[0]);
            }
        }
        if (task2.isSuccessful()) {
            String str2 = (String) task2.getResult();
            if (str2 != null && !str2.isEmpty()) {
                Logger.debug(LOG_TAG, "Successfully fetched AppCheck token.", new Object[0]);
                g0Var.f(X_FIREBASE_APPCHECK, str2);
            }
        } else {
            Exception exception2 = task2.getException();
            if (!(exception2 instanceof FirebaseApiNotAvailableException)) {
                Logger.warn(LOG_TAG, "Failed to get AppCheck token: %s.", exception2);
                abstractC2390c.b(r0.f19943j.f(exception2));
                return;
            }
            Logger.debug(LOG_TAG, "Firebase AppCheck API not available.", new Object[0]);
        }
        abstractC2390c.a(g0Var);
    }

    @Override // k4.AbstractC2392e
    public void applyRequestMetadata(AbstractC2391d abstractC2391d, Executor executor, AbstractC2390c abstractC2390c) {
        Task<String> token = this.authProvider.getToken();
        Task<String> token2 = this.appCheckProvider.getToken();
        Tasks.whenAll((Task<?>[]) new Task[]{token, token2}).addOnCompleteListener(Executors.DIRECT_EXECUTOR, new f(token, abstractC2390c, token2));
    }

    public void thisUsesUnstableApi() {
    }
}
