import { getCodeIndex } from "./index-tools.js";
/** Resolve a `workspace=` argument against the active CodeIndex.
 *  - When `workspace` is provided: look up by exact name or id; return
 *    { error: "unknown_workspace" } if not found.
 *  - When `workspace` is omitted AND `framework` is supplied: smart-default
 *    to the workspaces whose `detected_frameworks` includes the framework.
 *    If none, return { rootPaths: [] } (caller falls back to whole-repo scan).
 *  - On flat repo (no index.workspaces): return { rootPaths: [] }. */
export async function resolveWorkspaceScope(repo, workspace, framework) {
    let index;
    try {
        index = await getCodeIndex(repo, { skipFreshness: true });
    }
    catch {
        index = null;
    }
    const workspaces = index?.workspaces;
    if (!workspaces || workspaces.length === 0) {
        return { rootPaths: [], workspaces: [] };
    }
    if (workspace) {
        const ws = workspaces.find((w) => w.name === workspace || w.id === workspace);
        if (!ws) {
            const available = workspaces.map((w) => w.name ?? w.id);
            return { error: "unknown_workspace", input: workspace, available };
        }
        const rel = relRoot(ws.root, index.root);
        return { rootPaths: rel ? [rel] : [], workspaces: [ws] };
    }
    // Smart-default by framework
    if (framework) {
        const matched = workspaces.filter((w) => w.detected_frameworks.includes(framework));
        if (matched.length > 0) {
            const rootPaths = [];
            for (const ws of matched) {
                const rel = relRoot(ws.root, index.root);
                if (rel)
                    rootPaths.push(rel);
            }
            return { rootPaths, workspaces: matched };
        }
    }
    return { rootPaths: [], workspaces: [] };
}
function relRoot(absPath, indexRoot) {
    if (!absPath.startsWith(indexRoot))
        return null;
    return absPath.slice(indexRoot.length).replace(/^[\\/]+/, "");
}
//# sourceMappingURL=workspace-scope-helper.js.map