import { findWorkspaceRoot, listKnownWorkspaceEntries, readWorkspaceViewState, } from '../../core/workspace/index.js';
import { FileSystemUtils } from '../../utils/file-system.js';
import { isInteractive, resolveNoInteractive } from '../../utils/interactive.js';
import { validateWorkspaceNameForSetup } from './operations.js';
import { WorkspaceCliError, makeStatus, } from './types.js';
function normalizeRegistryRootForComparison(workspaceRoot) {
    try {
        return FileSystemUtils.canonicalizeExistingPath(workspaceRoot);
    }
    catch {
        return workspaceRoot;
    }
}
function workspaceNotInKnownViewsWarning() {
    return makeStatus('warning', 'workspace_not_in_known_views', 'This workspace is not in the managed local workspace views list.', {
        target: 'workspace.root',
        fix: 'Use projectx workspace list to inspect managed workspace views.',
    });
}
function sameWorkspaceRoot(knownRoot, currentWorkspaceRoot) {
    return (knownRoot !== undefined &&
        normalizeRegistryRootForComparison(knownRoot) ===
            normalizeRegistryRootForComparison(currentWorkspaceRoot));
}
function findKnownWorkspaceByName(entries, workspaceName) {
    return entries.find((entry) => entry.name === workspaceName);
}
export function selectedWorkspaceFromEntry(entry) {
    return {
        name: entry.name,
        root: entry.workspaceRoot,
        status: [],
        unregisteredCurrentWorkspace: false,
    };
}
export async function selectedWorkspaceFromRoot(currentWorkspaceRoot, entries) {
    const viewState = await readWorkspaceViewState(currentWorkspaceRoot);
    const knownRoot = findKnownWorkspaceByName(entries, viewState.name)?.workspaceRoot;
    const isKnown = sameWorkspaceRoot(knownRoot, currentWorkspaceRoot);
    return {
        name: viewState.name,
        root: currentWorkspaceRoot,
        status: isKnown ? [] : [workspaceNotInKnownViewsWarning()],
        unregisteredCurrentWorkspace: !isKnown,
    };
}
export async function selectWorkspaceForCommand(options, commandName, selectionOptions = {}) {
    const entries = await listKnownWorkspaceEntries();
    if (options.workspace) {
        const workspaceName = validateWorkspaceNameForSetup(options.workspace);
        const entry = findKnownWorkspaceByName(entries, workspaceName);
        if (!entry) {
            throw new WorkspaceCliError(`Unknown ProjectX workspace '${workspaceName}'.`, 'workspace_not_found', {
                target: 'workspace.name',
                fix: 'Run projectx workspace list to see known workspaces.',
            });
        }
        return selectedWorkspaceFromEntry(entry);
    }
    const currentWorkspaceRoot = await findWorkspaceRoot(process.cwd());
    if (currentWorkspaceRoot) {
        return selectedWorkspaceFromRoot(currentWorkspaceRoot, entries);
    }
    if (entries.length === 0) {
        throw new WorkspaceCliError("No known ProjectX workspaces. Run 'projectx workspace setup' first.\nAfter at least one workspace is known locally, you can also pass --workspace <name>.", 'no_known_workspaces', {
            target: 'workspace.name',
            fix: 'projectx workspace setup',
        });
    }
    if (entries.length === 1) {
        const [entry] = entries;
        return selectedWorkspaceFromEntry(entry);
    }
    if (options.json || resolveNoInteractive(options) || !isInteractive(options)) {
        const knownNames = entries.map((entry) => entry.name).join(', ');
        const usesPositionalName = selectionOptions.preferPositionalName;
        const fix = usesPositionalName
            ? `projectx workspace ${commandName} <name>`
            : `projectx workspace ${commandName} --workspace <name>`;
        throw new WorkspaceCliError(usesPositionalName
            ? `Multiple ProjectX workspaces are known. Known workspaces: ${knownNames}. Pass a workspace name.`
            : `Multiple ProjectX workspaces are known. Known workspaces: ${knownNames}. Pass --workspace <name>.`, 'workspace_selection_ambiguous', {
            target: 'workspace.name',
            fix,
        });
    }
    const { select } = await import('@inquirer/prompts');
    const selectedName = await select({
        message: 'Select workspace:',
        choices: entries.map((entry) => ({
            name: `${entry.name} (${entry.workspaceRoot})`,
            value: entry.name,
        })),
    });
    const selectedEntry = findKnownWorkspaceByName(entries, selectedName);
    if (!selectedEntry) {
        throw new WorkspaceCliError(`Unknown ProjectX workspace '${selectedName}'.`, 'workspace_not_found', {
            target: 'workspace.name',
            fix: 'Run projectx workspace list to see known workspaces.',
        });
    }
    return selectedWorkspaceFromEntry(selectedEntry);
}
//# sourceMappingURL=selection.js.map