import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useDeployCreate2WizardStore } from '../deployCreate2WizardStore.js';
import { Box, Text } from 'ink';
import { Spinner } from '@inkjs/ui';
import { useUpdateUserContext, useUserContext } from '../../../../queries/userContext.js';
import { PathInput } from '../../../../components/path-input/PathInput.js';
import { findFoundryRootDown } from '../../../../util/forge/foundryProject.js';
import { useState } from 'react';
export const EnterFoundryProjectPath = () => {
    const { submitEnterFoundryProjectPath } = useDeployCreate2WizardStore();
    const [errorMessage, setErrorMessage] = useState(null);
    const [foundRoot, setFoundRoot] = useState(null);
    const { data: userContext, isLoading: isUserContextLoading } = useUserContext();
    const { mutate: updateUserContext } = useUpdateUserContext();
    // if (wizardState.stepId !== 'enter-foundry-project-path') {
    // 	console.log('Invalid state', wizardState.stepId);
    // 	throw new Error('Invalid state');
    // }
    if (isUserContextLoading || !userContext) {
        return _jsx(Spinner, {});
    }
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { children: [_jsx(Text, { children: "Enter the path to your " }), _jsx(Text, { color: "cyan", bold: true, children: "Foundry" }), _jsx(Text, { children: " project " }), _jsx(Text, { children: "(press " }), _jsx(Text, { bold: true, children: "Tab" }), _jsx(Text, { children: " for autocomplete, " }), _jsx(Text, { bold: true, children: "Ctrl+U" }), _jsx(Text, { children: " to clear, default: " }), _jsx(Text, { color: "green", bold: true, children: "." }), _jsx(Text, { children: ")" }), _jsx(Text, { children: ":" })] }), errorMessage && (_jsxs(Box, { children: [_jsxs(Text, { color: "yellow", children: [errorMessage, " "] }), foundRoot && (_jsx(Text, { color: "cyan", bold: true, children: foundRoot }))] })), _jsx(PathInput, { defaultValue: userContext.forgeProjectPath ?? '', onSubmit: async (foundryProjectPath) => {
                    const projectPath = foundryProjectPath.trim();
                    // setErrorMessage(null);
                    // setFoundRoot(null);
                    let foundryRoot;
                    try {
                        foundryRoot = await findFoundryRootDown(projectPath, 4);
                        if (foundryRoot && foundryRoot !== projectPath) {
                            setErrorMessage('No foundry.toml found here, but one was found at:');
                            setFoundRoot(foundryRoot);
                            return;
                        }
                        if (projectPath !== '') {
                            updateUserContext({
                                forgeProjectPath: projectPath,
                            });
                        }
                        submitEnterFoundryProjectPath({
                            foundryProjectPath: projectPath === '' ? '.' : projectPath,
                        });
                    }
                    catch (e) {
                        setErrorMessage('Could not find a Foundry project (foundry.toml)');
                    }
                } })] }));
};
