'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { useCallback } from 'react';
import { authenticatePasskeyMFA } from '@dynamic-labs-sdk/client';
import { dynamicEvents } from '../../../events/dynamicEvents.js';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import 'eventemitter3';
import '@dynamic-labs/utils';
import '../../../config/ApiEndpoint.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import '../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../constants/colors.js';
import '../../constants/values.js';
import '../../../shared/consts/index.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../store/state/nonce/nonce.js';
import '@dynamic-labs-wallet/browser-wallet-client';
import '@dynamic-labs/locale';
import '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';
import { storeTokenAndUser } from '../../../client/extension/storeTokenAndUser/storeTokenAndUser.js';
import { useUserAuth } from '../useUserAuth/useUserAuth.js';

/**
 * Authenticate passkey for MFA
 *
 * @returns Function to authenticate with a passkey for MFA
 *
 * @example
 * ```tsx
 * const App = () => {
 *   const authenticatePasskeyMFA = useAuthenticatePasskeyMFA();
 *
 *   // Optional: creates a single-use MFA token
 *   const createMfaToken = { singleUse: true };
 *
 *   return (
 *     <button
 *       onClick={() => authenticatePasskeyMFA({ createMfaToken })}
 *     >
 *       Authenticate passkey
 *     </button>
 *  );
 * }
 */
const useAuthenticatePasskeyMFA = () => {
    const { completeAuth } = useUserAuth({});
    return useCallback((props) => __awaiter(void 0, void 0, void 0, function* () {
        let mfaToken = undefined;
        yield completeAuth({
            updateJwtFunction: () => __awaiter(void 0, void 0, void 0, function* () {
                var _a;
                const response = yield authenticatePasskeyMFA(props);
                // When requestedScopes is provided the response contains an elevated
                // access token. storeTokenAndUser routes through updateAuthFromVerifyResponse
                // which stores it in client state so getElevatedAccessToken can retrieve it.
                if ((_a = props === null || props === void 0 ? void 0 : props.requestedScopes) === null || _a === void 0 ? void 0 : _a.length) {
                    storeTokenAndUser(response);
                }
                // eslint-disable-next-line prefer-destructuring
                mfaToken = response.mfaToken;
                dynamicEvents.emit('mfaCompletionSuccess', {
                    mfaToken: response.mfaToken,
                });
                return {
                    isEmailVerificationRequired: false,
                    isSmsVerificationRequired: false,
                    missingFields: [],
                    // VerifyResponse is compatible with UpdateSelfResponse for MFA flows:
                    // Both types have: expiresAt, jwt, minifiedJwt, user (used in useUserAuth.ts:329-332)
                    updateUserProfileResponse: response,
                };
            }),
        });
        return mfaToken;
    }), [completeAuth]);
};

export { useAuthenticatePasskeyMFA };
