package com.amplifyframework.auth;

import com.amplifyframework.annotations.InternalAmplifyApi;
import com.amplifyframework.auth.result.AuthSessionResult;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.core.Consumer;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.h;
import kotlin.jvm.internal.t;
import rm.n0;
import rm.x;
import rm.y;
import vm.d;
import vm.i;
import wm.c;

@Metadata(d1 = {"\u0000<\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0006\b\u0016\u0018\u00002\u00020\u0001B\u0011\b\u0007\u0012\u0006\u0010\u0012\u001a\u00020\u0011¢\u0006\u0004\b\u0014\u0010\u0015B\t\b\u0016¢\u0006\u0004\b\u0014\u0010\u0016J\u001b\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u0003\u001a\u00020\u0002H\u0096@ø\u0001\u0000¢\u0006\u0004\b\u0005\u0010\u0006J\u0013\u0010\b\u001a\u00020\u0007H\u0096@ø\u0001\u0000¢\u0006\u0004\b\b\u0010\tJ(\u0010\u0010\u001a\u00020\u000f2\f\u0010\u000b\u001a\b\u0012\u0004\u0012\u00020\u00070\n2\u0010\u0010\u000e\u001a\f\u0012\b\u0012\u00060\fj\u0002`\r0\nH\u0016R\u0014\u0010\u0012\u001a\u00020\u00118\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0012\u0010\u0013\u0082\u0002\u0004\n\u0002\b\u0019¨\u0006\u0017"}, d2 = {"Lcom/amplifyframework/auth/CognitoCredentialsProvider;", "Lcom/amplifyframework/auth/AuthCredentialsProvider;", "Lp6/b;", "attributes", "Ln6/a;", "resolve", "(Lp6/b;Lvm/d;)Ljava/lang/Object;", "", "getIdentityId", "(Lvm/d;)Ljava/lang/Object;", "Lcom/amplifyframework/core/Consumer;", "onResult", "Ljava/lang/Exception;", "Lkotlin/Exception;", "onFailure", "Lrm/n0;", "getAccessToken", "Lcom/amplifyframework/auth/AuthCategory;", "authCategory", "Lcom/amplifyframework/auth/AuthCategory;", "<init>", "(Lcom/amplifyframework/auth/AuthCategory;)V", "()V", "com.amplifyframework.aws-core_release"}, k = 1, mv = {1, 9, 0})
/* loaded from: classes4.dex */
public class CognitoCredentialsProvider implements AuthCredentialsProvider {
    private final AuthCategory authCategory;

    @InternalAmplifyApi
    public CognitoCredentialsProvider(AuthCategory authCategory) {
        t.g(authCategory, "authCategory");
        this.authCategory = authCategory;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void getAccessToken$lambda$3(Consumer onFailure, Consumer onResult, AuthSession session) {
        AWSAuthSessionBehavior aWSAuthSession;
        t.g(onFailure, "$onFailure");
        t.g(onResult, "$onResult");
        t.g(session, "session");
        aWSAuthSession = CognitoCredentialsProviderKt.toAWSAuthSession(session);
        n0 n0Var = null;
        String accessToken = aWSAuthSession != null ? aWSAuthSession.getAccessToken() : null;
        if (accessToken != null) {
            onResult.accept(accessToken);
            n0Var = n0.f34904a;
        }
        if (n0Var == null) {
            onFailure.accept(new AuthException("Token is null", "Token received but is null. Check if you are signed in", null, 4, null));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void getAccessToken$lambda$4(Consumer onFailure, AuthException it) {
        t.g(onFailure, "$onFailure");
        t.g(it, "it");
        onFailure.accept(it);
    }

    static /* synthetic */ Object getIdentityId$suspendImpl(CognitoCredentialsProvider cognitoCredentialsProvider, d<? super String> dVar) {
        d d10;
        Object f10;
        d10 = c.d(dVar);
        final i iVar = new i(d10);
        cognitoCredentialsProvider.authCategory.fetchAuthSession(new Consumer() { // from class: com.amplifyframework.auth.CognitoCredentialsProvider$getIdentityId$2$1
            @Override // com.amplifyframework.core.Consumer
            public final void accept(AuthSession authSession) {
                AWSAuthSessionBehavior aWSAuthSession;
                n0 n0Var;
                AuthSessionResult<String> identityIdResult;
                String value;
                t.g(authSession, "authSession");
                aWSAuthSession = CognitoCredentialsProviderKt.toAWSAuthSession(authSession);
                if (aWSAuthSession == null || (identityIdResult = aWSAuthSession.getIdentityIdResult()) == null || (value = identityIdResult.getValue()) == null) {
                    n0Var = null;
                } else {
                    iVar.resumeWith(x.a(value));
                    n0Var = n0.f34904a;
                }
                if (n0Var == null) {
                    d<String> dVar2 = iVar;
                    x.a aVar = x.f34910p;
                    dVar2.resumeWith(x.a(y.a(new AuthException("Failed to get identity ID. Check if you are signed in and configured identity pools correctly.", "Sorry, we don’t have a recovery suggestion for this error.", null, 4, null))));
                }
            }
        }, new Consumer() { // from class: com.amplifyframework.auth.CognitoCredentialsProvider$getIdentityId$2$2
            @Override // com.amplifyframework.core.Consumer
            public final void accept(AuthException it) {
                t.g(it, "it");
                d<String> dVar2 = iVar;
                x.a aVar = x.f34910p;
                dVar2.resumeWith(x.a(y.a(it)));
            }
        });
        Object a10 = iVar.a();
        f10 = wm.d.f();
        if (a10 == f10) {
            h.c(dVar);
        }
        return a10;
    }

    static /* synthetic */ Object resolve$suspendImpl(CognitoCredentialsProvider cognitoCredentialsProvider, p6.b bVar, d<? super n6.a> dVar) {
        d d10;
        Object f10;
        d10 = c.d(dVar);
        final i iVar = new i(d10);
        cognitoCredentialsProvider.authCategory.fetchAuthSession(new Consumer() { // from class: com.amplifyframework.auth.CognitoCredentialsProvider$resolve$2$1
            @Override // com.amplifyframework.core.Consumer
            public final void accept(AuthSession authSession) {
                AWSAuthSessionBehavior aWSAuthSession;
                n0 n0Var;
                AWSAuthSessionBehavior aWSAuthSession2;
                AuthSessionResult<AWSCredentials> awsCredentialsResult;
                AuthSessionResult<AWSCredentials> awsCredentialsResult2;
                AWSCredentials value;
                t.g(authSession, "authSession");
                aWSAuthSession = CognitoCredentialsProviderKt.toAWSAuthSession(authSession);
                AuthException authException = null;
                if (aWSAuthSession == null || (awsCredentialsResult2 = aWSAuthSession.getAwsCredentialsResult()) == null || (value = awsCredentialsResult2.getValue()) == null) {
                    n0Var = null;
                } else {
                    d<n6.a> dVar2 = iVar;
                    x.a aVar = x.f34910p;
                    dVar2.resumeWith(x.a(AWSCredentialsKt.toSdkCredentials(value)));
                    n0Var = n0.f34904a;
                }
                if (n0Var == null) {
                    d<n6.a> dVar3 = iVar;
                    aWSAuthSession2 = CognitoCredentialsProviderKt.toAWSAuthSession(authSession);
                    if (aWSAuthSession2 != null && (awsCredentialsResult = aWSAuthSession2.getAwsCredentialsResult()) != null) {
                        authException = awsCredentialsResult.getError();
                    }
                    AuthException authException2 = new AuthException("Failed to get credentials. Check if you are signed in and configured identity pools correctly.", "Sorry, we don’t have a recovery suggestion for this error.", authException);
                    x.a aVar2 = x.f34910p;
                    dVar3.resumeWith(x.a(y.a(authException2)));
                }
            }
        }, new Consumer() { // from class: com.amplifyframework.auth.CognitoCredentialsProvider$resolve$2$2
            @Override // com.amplifyframework.core.Consumer
            public final void accept(AuthException it) {
                t.g(it, "it");
                d<n6.a> dVar2 = iVar;
                x.a aVar = x.f34910p;
                dVar2.resumeWith(x.a(y.a(it)));
            }
        });
        Object a10 = iVar.a();
        f10 = wm.d.f();
        if (a10 == f10) {
            h.c(dVar);
        }
        return a10;
    }

    @Override // com.amplifyframework.auth.AuthCredentialsProvider
    public void getAccessToken(final Consumer<String> onResult, final Consumer<Exception> onFailure) {
        t.g(onResult, "onResult");
        t.g(onFailure, "onFailure");
        this.authCategory.fetchAuthSession(new Consumer() { // from class: com.amplifyframework.auth.a
            @Override // com.amplifyframework.core.Consumer
            public final void accept(Object obj) {
                CognitoCredentialsProvider.getAccessToken$lambda$3(Consumer.this, onResult, (AuthSession) obj);
            }
        }, new Consumer() { // from class: com.amplifyframework.auth.b
            @Override // com.amplifyframework.core.Consumer
            public final void accept(Object obj) {
                CognitoCredentialsProvider.getAccessToken$lambda$4(Consumer.this, (AuthException) obj);
            }
        });
    }

    @Override // com.amplifyframework.auth.AuthCredentialsProvider
    public Object getIdentityId(d<? super String> dVar) {
        return getIdentityId$suspendImpl(this, dVar);
    }

    @Override // com.amplifyframework.auth.AuthCredentialsProvider, v6.b
    public Object resolve(p6.b bVar, d<? super n6.a> dVar) {
        return resolve$suspendImpl(this, bVar, dVar);
    }

    /* JADX WARN: Illegal instructions before constructor call */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public CognitoCredentialsProvider() {
        this(Auth);
        AuthCategory Auth = Amplify.Auth;
        t.f(Auth, "Auth");
    }
}
