/*! @azure/msal-browser v4.24.1 2025-09-30 */
'use strict';
import { CustomAuthAccountData } from '../../../get_account/auth_flow/CustomAuthAccountData.mjs';
import { SignInResendCodeResult } from '../result/SignInResendCodeResult.mjs';
import { SignInSubmitCodeResult } from '../result/SignInSubmitCodeResult.mjs';
import { SignInState } from './SignInState.mjs';
import { SignInCompletedState } from './SignInCompletedState.mjs';
import { SIGN_IN_COMPLETED_RESULT_TYPE } from '../../interaction_client/result/SignInActionResult.mjs';

/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
/*
 * Sign-in code required state.
 */
class SignInCodeRequiredState extends SignInState {
    /**
     * Once user configures email one-time passcode as a authentication method in Microsoft Entra, a one-time passcode will be sent to the user’s email.
     * Submit this one-time passcode to continue sign-in flow.
     * @param {string} code - The code to submit.
     * @returns {Promise<SignInSubmitCodeResult>} The result of the operation.
     */
    async submitCode(code) {
        try {
            this.ensureCodeIsValid(code, this.stateParameters.codeLength);
            const submitCodeParams = {
                clientId: this.stateParameters.config.auth.clientId,
                correlationId: this.stateParameters.correlationId,
                challengeType: this.stateParameters.config.customAuth.challengeTypes ?? [],
                scopes: this.stateParameters.scopes ?? [],
                continuationToken: this.stateParameters.continuationToken ?? "",
                code: code,
                username: this.stateParameters.username,
                claims: this.stateParameters.claims,
            };
            this.stateParameters.logger.verbose("Submitting code for sign-in.", this.stateParameters.correlationId);
            const submitCodeResult = await this.stateParameters.signInClient.submitCode(submitCodeParams);
            this.stateParameters.logger.verbose("Code submitted for sign-in.", this.stateParameters.correlationId);
            if (submitCodeResult.type === SIGN_IN_COMPLETED_RESULT_TYPE) {
                const accountInfo = new CustomAuthAccountData(submitCodeResult.authenticationResult.account, this.stateParameters.config, this.stateParameters.cacheClient, this.stateParameters.logger, this.stateParameters.correlationId);
                return new SignInSubmitCodeResult(new SignInCompletedState(), accountInfo);
            }
            else {
                // Unexpected result type
                const result = submitCodeResult;
                const error = new Error(`Unexpected result type: ${result.type}`);
                return SignInSubmitCodeResult.createWithError(error);
            }
        }
        catch (error) {
            this.stateParameters.logger.errorPii(`Failed to submit code for sign-in. Error: ${error}.`, this.stateParameters.correlationId);
            return SignInSubmitCodeResult.createWithError(error);
        }
    }
    /**
     * Resends the another one-time passcode for sign-in flow if the previous one hasn't been verified.
     * @returns {Promise<SignInResendCodeResult>} The result of the operation.
     */
    async resendCode() {
        try {
            const submitCodeParams = {
                clientId: this.stateParameters.config.auth.clientId,
                correlationId: this.stateParameters.correlationId,
                challengeType: this.stateParameters.config.customAuth.challengeTypes ?? [],
                continuationToken: this.stateParameters.continuationToken ?? "",
                username: this.stateParameters.username,
            };
            this.stateParameters.logger.verbose("Resending code for sign-in.", this.stateParameters.correlationId);
            const result = await this.stateParameters.signInClient.resendCode(submitCodeParams);
            this.stateParameters.logger.verbose("Code resent for sign-in.", this.stateParameters.correlationId);
            return new SignInResendCodeResult(new SignInCodeRequiredState({
                correlationId: result.correlationId,
                continuationToken: result.continuationToken,
                logger: this.stateParameters.logger,
                config: this.stateParameters.config,
                signInClient: this.stateParameters.signInClient,
                cacheClient: this.stateParameters.cacheClient,
                jitClient: this.stateParameters.jitClient,
                username: this.stateParameters.username,
                codeLength: result.codeLength,
                scopes: this.stateParameters.scopes,
            }));
        }
        catch (error) {
            return SignInResendCodeResult.createWithError(error);
        }
    }
    /**
     * Gets the sent code length.
     * @returns {number} The length of the code.
     */
    getCodeLength() {
        return this.stateParameters.codeLength;
    }
    /**
     * Gets the scopes to request.
     * @returns {string[] | undefined} The scopes to request.
     */
    getScopes() {
        return this.stateParameters.scopes;
    }
}

export { SignInCodeRequiredState };
//# sourceMappingURL=SignInCodeRequiredState.mjs.map
