package com.debridstream.tv;

import A4.d;
import B4.a;
import N2.q;
import V2.A0;
import V2.AbstractC0714r1;
import V2.B0;
import V2.B3;
import V2.C0;
import V2.C0732w;
import V2.C0741y0;
import V2.C0745z0;
import V2.C3;
import V2.D0;
import V2.E0;
import V2.F0;
import V2.G0;
import V2.H0;
import V2.I0;
import V2.J0;
import V2.K0;
import V2.N0;
import V2.w3;
import V2.z3;
import V4.E;
import V4.InterfaceC0765i0;
import Y4.H;
import Y4.O;
import Y4.a0;
import Y4.c0;
import android.app.Application;
import android.content.Intent;
import androidx.compose.runtime.internal.StabilityInferred;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.X;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.firebase.auth.FirebaseAuth;
import java.util.List;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
import w4.C3137A;
import w4.m;
import w4.n;
import x4.y;
import x4.z;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class AuthViewModel extends AndroidViewModel {
    public static final int $stable = 8;
    private static final B0 Companion = new B0();

    @Deprecated
    public static final String TAG = "AuthViewModel";
    private final H _uiState;
    private FirebaseAuth.AuthStateListener authListener;
    private boolean authObserverStarted;
    private InterfaceC0765i0 observerJob;
    private final a0 uiState;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public AuthViewModel(Application application) {
        super(application);
        o.h(application, "application");
        c0 c0VarC = O.c(new C0745z0(null, y.f26065a, false, false, true, "Starting app...", null, null, false, 0));
        this._uiState = c0VarC;
        this.uiState = c0VarC;
        startAuthObserver();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x0079, code lost:
    
        r3 = r21;
     */
    /* JADX WARN: Code restructure failed: missing block: B:28:0x007b, code lost:
    
        r0 = th;
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0018  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object refreshStartupStateForAuthenticatedUser(d dVar) {
        D0 d02;
        c0 c0Var;
        Object value;
        AuthViewModel authViewModel;
        Object objD;
        c0 c0Var2;
        Object value2;
        c0 c0Var3;
        Object value3;
        int i6 = 1;
        if (dVar instanceof D0) {
            d02 = (D0) dVar;
            int i7 = d02.f7289d;
            if ((i7 & Integer.MIN_VALUE) != 0) {
                d02.f7289d = i7 - Integer.MIN_VALUE;
            } else {
                d02 = new D0(this, dVar);
            }
        }
        Object objA = d02.f7287b;
        a aVar = a.f492a;
        int i8 = d02.f7289d;
        if (i8 == 0) {
            AbstractC2467b.l(objA);
            H h5 = this._uiState;
            do {
                c0Var = (c0) h5;
                value = c0Var.getValue();
            } while (!c0Var.i(value, C0745z0.a((C0745z0) value, null, null, false, false, true, "Checking authentication...", null, null, false, 0, 907)));
            C0741y0 c0741y0 = C0741y0.f8184a;
            Application application = getApplication();
            C0732w c0732w = new C0732w(this, i6);
            d02.f7286a = this;
            d02.f7289d = 1;
            objA = c0741y0.a(application, c0732w, d02);
            if (objA == aVar) {
                return aVar;
            }
            authViewModel = this;
        } else {
            if (i8 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            authViewModel = d02.f7286a;
            try {
                AbstractC2467b.l(objA);
            } catch (Throwable th) {
                th = th;
                objD = AbstractC2467b.d(th);
            }
        }
        objD = (w3) objA;
        if (!(objD instanceof m)) {
            w3 w3Var = (w3) objD;
            B3 b32 = w3Var.f8146a;
            if (b32 == null || !b32.f7266d) {
                q.m(z.f26066a);
                c0 c0Var4 = AbstractC0714r1.f8036a;
                y yVar = y.f26065a;
                c0 c0Var5 = AbstractC0714r1.f8036a;
                c0Var5.getClass();
                c0Var5.k(null, yVar);
                C3.a();
            } else {
                q.m(w3Var.f8148c);
                c0 c0Var6 = AbstractC0714r1.f8036a;
                List catalogs = w3Var.f8149d;
                o.h(catalogs, "catalogs");
                c0 c0Var7 = AbstractC0714r1.f8036a;
                c0Var7.getClass();
                c0Var7.k(null, catalogs);
            }
            H h6 = authViewModel._uiState;
            do {
                c0Var3 = (c0) h6;
                value3 = c0Var3.getValue();
            } while (!c0Var3.i(value3, C0745z0.a((C0745z0) value3, b32 != null ? new A0(b32.f7263a, b32.f7264b, b32.f7265c, b32.f7266d) : null, w3Var.f8147b, true, b32 != null ? b32.f7266d : false, false, "", null, null, false, 0, 768)));
        }
        Throwable thA = n.a(objD);
        if (thA != null) {
            H h7 = authViewModel._uiState;
            do {
                c0Var2 = (c0) h7;
                value2 = c0Var2.getValue();
            } while (!c0Var2.i(value2, C0745z0.a((C0745z0) value2, null, null, true, false, false, "", thA.getMessage(), null, false, 0, 907)));
        }
        return C3137A.f25453a;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final C3137A refreshStartupStateForAuthenticatedUser$lambda$3$lambda$2(AuthViewModel authViewModel, String stage) {
        o.h(stage, "stage");
        H h5 = authViewModel._uiState;
        while (true) {
            c0 c0Var = (c0) h5;
            Object value = c0Var.getValue();
            String str = stage;
            if (c0Var.i(value, C0745z0.a((C0745z0) value, null, null, false, false, false, str, null, null, false, 0, 991))) {
                return C3137A.f25453a;
            }
            stage = str;
        }
    }

    private final String resolveWebClientId(Application application) {
        int identifier = application.getResources().getIdentifier("default_web_client_id", "string", application.getPackageName());
        if (identifier == 0) {
            return null;
        }
        return application.getString(identifier);
    }

    private final void startAuthObserver() {
        if (this.authObserverStarted) {
            return;
        }
        this.authObserverStarted = true;
        InterfaceC0765i0 interfaceC0765i0 = this.observerJob;
        if (interfaceC0765i0 != null) {
            interfaceC0765i0.cancel(null);
        }
        this.observerJob = E.y(X.h(this), null, null, new N0(this, null), 3);
    }

    public final Intent buildGoogleSignInIntent() {
        c0 c0Var;
        Object value;
        Application application = getApplication();
        String strResolveWebClientId = resolveWebClientId(application);
        if (strResolveWebClientId != null) {
            GoogleSignInOptions googleSignInOptionsBuild = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(strResolveWebClientId).requestEmail().build();
            o.g(googleSignInOptionsBuild, "build(...)");
            return GoogleSignIn.getClient(application, googleSignInOptionsBuild).getSignInIntent();
        }
        H h5 = this._uiState;
        do {
            c0Var = (c0) h5;
            value = c0Var.getValue();
        } while (!c0Var.i(value, C0745z0.a((C0745z0) value, null, null, false, false, false, null, "Missing default_web_client_id in Firebase config", null, false, 0, 959)));
        return null;
    }

    public final void continueWithoutSubscription() {
        c0 c0Var;
        Object value;
        C0745z0 c0745z0;
        c0 c0Var2 = z3.f8212a;
        Application context = getApplication();
        o.h(context, "context");
        z3.f8214c = true;
        z3.b(context);
        H h5 = this._uiState;
        do {
            c0Var = (c0) h5;
            value = c0Var.getValue();
            c0745z0 = (C0745z0) value;
        } while (!c0Var.i(value, C0745z0.a(c0745z0, null, null, false, false, false, null, null, null, false, c0745z0.f8208j + 1, 511)));
    }

    public final void createAccountWithEmailPassword(String email, String password) {
        o.h(email, "email");
        o.h(password, "password");
        E.y(X.h(this), null, null, new C0(this, email, password, null), 3);
    }

    public final a0 getUiState() {
        return this.uiState;
    }

    @Override // androidx.lifecycle.b0
    public void onCleared() {
        FirebaseAuth.AuthStateListener authStateListener = this.authListener;
        if (authStateListener != null) {
            FirebaseAuth.getInstance().removeAuthStateListener(authStateListener);
        }
        this.authListener = null;
        InterfaceC0765i0 interfaceC0765i0 = this.observerJob;
        if (interfaceC0765i0 != null) {
            interfaceC0765i0.cancel(null);
        }
        super.onCleared();
    }

    public final void refreshSubscriptionAndStartup() {
        E.y(X.h(this), null, null, new E0(this, null), 3);
    }

    public final void reloadFirebaseUserAndRefreshStartup() {
        E.y(X.h(this), null, null, new F0(this, null), 3);
    }

    public final void restorePurchasesAndStartup() {
        E.y(X.h(this), null, null, new G0(this, null), 3);
    }

    public final void sendVerificationEmail() {
        E.y(X.h(this), null, null, new H0(this, null), 3);
    }

    public final void signInWithEmailPassword(String email, String password) {
        o.h(email, "email");
        o.h(password, "password");
        E.y(X.h(this), null, null, new I0(this, email, password, null), 3);
    }

    public final void signInWithGoogleIntentResult(Intent intent) {
        E.y(X.h(this), null, null, new J0(this, intent, null), 3);
    }

    public final void signOut() {
        E.y(X.h(this), null, null, new K0(this, null), 3);
    }
}
