import { readRegistry } from "./registry.js";
export async function listWorktrees(cwd, registryFile, deps) {
    const registry = await readRegistry(registryFile, deps.fs);
    const gitOutput = await deps.exec("git worktree list --porcelain", {
        cwd
    });
    const gitPaths = parseGitWorktreeList(gitOutput.stdout);
    return registry.worktrees.map((entry) => ({
        ...entry,
        gitExists: gitPaths.has(entry.path)
    }));
}
function parseGitWorktreeList(output) {
    const paths = new Set();
    for (const line of output.split("\n")) {
        if (line.startsWith("worktree ")) {
            paths.add(line.slice("worktree ".length));
        }
    }
    return paths;
}
