'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var React = require('react');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs/sdk-api-core');
require('eventemitter3');
require('@dynamic-labs/utils');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
var ViewContext = require('../../../context/ViewContext/ViewContext.cjs');
require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../constants/colors.cjs');
require('../../constants/values.cjs');
require('../../../shared/consts/index.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs-wallet/browser-wallet-client');
require('@dynamic-labs/locale');
var dynamicContextProps = require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.cjs');
var dynamicEvents = require('../../../events/dynamicEvents.cjs');
var useProjectSettings = require('../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.cjs');
require('../../../context/DynamicContext/useDynamicContext/useDynamicContext.cjs');
var useInternalDynamicContext = require('../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');

const PASSWORD_SETUP_CANCELLED_ERROR = 'Password setup cancelled';
/**
 * Hook to handle password setup flow before wallet creation.
 *
 * This hook checks if passcodeRequired is enabled in project settings,
 * and if so, prompts the user to set up a password before creating wallets.
 *
 * If a custom setupWalletPassword handler is provided via DynamicContextProvider settings,
 * it will be used instead of the default UI modal.
 *
 * @returns An object containing:
 * - isPasscodeRequired: boolean indicating if password is required
 * - setupPassword: function that shows the password setup flow and returns the password
 */
const useSetupPassword = () => {
    var _a, _b;
    const projectSettings = useProjectSettings.useProjectSettings();
    const { setShowAuthFlow } = useInternalDynamicContext.useInternalDynamicContext();
    const { pushView } = ViewContext.useViewContext();
    const { handlers } = dynamicContextProps.useDynamicSettings();
    // Use ref pattern to avoid callback recreation when handlers change
    const setupWalletPasswordHandlerRef = React.useRef(handlers === null || handlers === void 0 ? void 0 : handlers.setupWalletPassword);
    setupWalletPasswordHandlerRef.current = handlers === null || handlers === void 0 ? void 0 : handlers.setupWalletPassword;
    const isPasscodeRequired = (_b = (_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.waas) === null || _a === void 0 ? void 0 : _a.passcodeRequired) !== null && _b !== void 0 ? _b : false;
    const setupPassword = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        if (!isPasscodeRequired) {
            return undefined;
        }
        // If custom handler provided, use it
        if (setupWalletPasswordHandlerRef.current) {
            return setupWalletPasswordHandlerRef.current();
        }
        setShowAuthFlow(true, {
            ignoreIfIsEmbeddedWidget: false,
            performMultiWalletChecks: false,
        });
        pushView('setup-password-for-wallet-creation');
        return new Promise((resolve, reject) => {
            const handleComplete = (password) => {
                cleanup();
                resolve(password);
            };
            const handleCancelled = () => {
                cleanup();
                reject(new Error(PASSWORD_SETUP_CANCELLED_ERROR));
            };
            const cleanup = () => {
                dynamicEvents.dynamicEvents.off('embeddedWalletPasswordSetupCompleted', handleComplete);
                dynamicEvents.dynamicEvents.off('embeddedWalletPasswordSetupCancelled', handleCancelled);
                dynamicEvents.dynamicEvents.off('authFlowCancelled', handleCancelled);
            };
            dynamicEvents.dynamicEvents.once('embeddedWalletPasswordSetupCompleted', handleComplete);
            dynamicEvents.dynamicEvents.once('embeddedWalletPasswordSetupCancelled', handleCancelled);
            // Also listen for auth flow cancellation (overlay close)
            dynamicEvents.dynamicEvents.once('authFlowCancelled', handleCancelled);
        });
    }), [isPasscodeRequired, setShowAuthFlow, pushView]);
    return { isPasscodeRequired, setupPassword };
};

exports.PASSWORD_SETUP_CANCELLED_ERROR = PASSWORD_SETUP_CANCELLED_ERROR;
exports.useSetupPassword = useSetupPassword;
