import { join } from 'node:path';
import { getDataDir, ensureDir, readJsonFile, writeJsonFile } from './json-store.js';
function worktreesDir(workspaceId) {
    return join(getDataDir(), 'workspaces', workspaceId, 'worktrees');
}
function worktreesIndex(workspaceId) {
    return join(worktreesDir(workspaceId), 'index.json');
}
export function listWorktrees(workspaceId) {
    return readJsonFile(worktreesIndex(workspaceId)) || [];
}
export function getWorktree(workspaceId, worktreeId) {
    const list = listWorktrees(workspaceId);
    return list.find(wt => wt.id === worktreeId) ?? null;
}
export function createWorktree(workspaceId, info) {
    ensureDir(worktreesDir(workspaceId));
    const list = listWorktrees(workspaceId);
    list.push(info);
    writeJsonFile(worktreesIndex(workspaceId), list);
}
export function updateWorktree(workspaceId, info) {
    const list = listWorktrees(workspaceId);
    const idx = list.findIndex(wt => wt.id === info.id);
    if (idx >= 0)
        list[idx] = info;
    writeJsonFile(worktreesIndex(workspaceId), list);
}
export function deleteWorktreeFromIndex(workspaceId, worktreeId) {
    const list = listWorktrees(workspaceId).filter(wt => wt.id !== worktreeId);
    writeJsonFile(worktreesIndex(workspaceId), list);
}
//# sourceMappingURL=worktree-store.js.map