var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { CognitoUser } from 'amazon-cognito-identity-js';
import _ from 'lodash';
import { Auth } from 'aws-amplify';
/**
 * Check user authentication state
 * @param cognitoAuthResult optional result of authentication operation.
 * Else `Auth.currentSession()`
 * @returns See {@link IAuthenticationResult}
 */
export const OnCheckSession = (cognitoAuthResult) => __awaiter(this, void 0, void 0, function* () {
    let session = cognitoAuthResult;
    if (_.isNil(session)) {
        session = yield Auth.currentSession();
    }
    let user = session instanceof CognitoUser ? session : null;
    if (_.get(session, 'user') && session.user instanceof CognitoUser) {
        user = session.user;
    }
    const isError = session instanceof Error;
    if (__DEV__) {
        console.log('**** OnCheckSession ****');
        console.log('isError? ' + isError);
        console.log(session);
    }
    let result;
    if (_.get(user, 'username') && _.get(session, 'userConfirmed') !== false) {
        result = { state: 'Authenticated', user: session };
        return result;
    }
    const challengeName = _.get(session, 'challengeName') || '';
    // MFA code required
    if (challengeName === 'SMS_MFA' || challengeName === 'SOFTWARE_TOKEN_MFA') {
        result = {
            state: 'ConfirmLoginMFAWaiting',
            error: new Error(_.get(session, 'message') || 'Please enter MFA code'),
        };
        return result;
    }
    // Need to setup MFA
    if (challengeName === 'MFA_SETUP') {
        result = {
            state: 'MFA_SETUP',
            error: new Error(_.get(session, 'message') || 'Must setup MFA'),
        };
        return result;
    }
    if (challengeName === 'NEW_PASSWORD_REQUIRED') {
        result = {
            state: 'NEW_PASSWORD_REQUIRED',
            error: new Error(_.get(session, 'message') || 'New password required'),
        };
        return result;
    }
    const code = _.get(session, 'code');
    // Not confirmed
    if (code === 'UserNotConfirmedException' ||
        code === 'CodeMismatchException' ||
        _.get(session, 'userConfirmed') === false) {
        result = {
            state: 'ConfirmAccountCodeWaiting',
            error: new Error(_.get(session, 'message') || 'User not confirmed'),
        };
        return result;
    }
    if (!_.isNil(code) && _.get(session, 'message')) {
        // Other codes
        console.log('*** Cognito OnCheckSession code: "' + code + '" ***');
        result = {
            state: 'Unauthenticated',
            error: new Error(session.message),
        };
        return result;
    }
    result = {
        state: isError ? 'AuthenticationError' : 'Unauthenticated',
        error: isError ? session : undefined,
    };
    return result;
});
export default OnCheckSession;
//# sourceMappingURL=OnCheckSession.js.map