'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { useRef, useCallback } from 'react';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import 'eventemitter3';
import '@dynamic-labs/utils';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import { useViewContext } from '../../../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 { useDynamicSettings } from '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';
import { dynamicEvents } from '../../../events/dynamicEvents.js';
import { useProjectSettings } from '../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.js';
import '../../../context/DynamicContext/useDynamicContext/useDynamicContext.js';
import { useInternalDynamicContext } from '../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.js';

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();
    const { setShowAuthFlow } = useInternalDynamicContext();
    const { pushView } = useViewContext();
    const { handlers } = useDynamicSettings();
    // Use ref pattern to avoid callback recreation when handlers change
    const setupWalletPasswordHandlerRef = 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 = useCallback(() => __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.off('embeddedWalletPasswordSetupCompleted', handleComplete);
                dynamicEvents.off('embeddedWalletPasswordSetupCancelled', handleCancelled);
                dynamicEvents.off('authFlowCancelled', handleCancelled);
            };
            dynamicEvents.once('embeddedWalletPasswordSetupCompleted', handleComplete);
            dynamicEvents.once('embeddedWalletPasswordSetupCancelled', handleCancelled);
            // Also listen for auth flow cancellation (overlay close)
            dynamicEvents.once('authFlowCancelled', handleCancelled);
        });
    }), [isPasscodeRequired, setShowAuthFlow, pushView]);
    return { isPasscodeRequired, setupPassword };
};

export { PASSWORD_SETUP_CANCELLED_ERROR, useSetupPassword };
