import { join } from 'node:path';
import { getDataDir, ensureDir, readJsonFile, writeJsonFile } from './json-store.js';
function workspacesIndex() {
    return join(getDataDir(), 'workspaces', 'index.json');
}
function workspaceDir(id) {
    return join(getDataDir(), 'workspaces', id);
}
export function listWorkspaces() {
    return readJsonFile(workspacesIndex()) || [];
}
export function getWorkspace(id) {
    return readJsonFile(join(workspaceDir(id), 'workspace.json'));
}
export function createWorkspace(ws) {
    const dir = workspaceDir(ws.id);
    ensureDir(dir);
    writeJsonFile(join(dir, 'workspace.json'), ws);
    const list = listWorkspaces();
    list.push(ws);
    writeJsonFile(workspacesIndex(), list);
}
export function updateWorkspace(ws) {
    writeJsonFile(join(workspaceDir(ws.id), 'workspace.json'), ws);
    const list = listWorkspaces();
    const idx = list.findIndex((w) => w.id === ws.id);
    if (idx >= 0)
        list[idx] = ws;
    writeJsonFile(workspacesIndex(), list);
}
export function deleteWorkspace(id) {
    const list = listWorkspaces().filter((w) => w.id !== id);
    writeJsonFile(workspacesIndex(), list);
}
//# sourceMappingURL=workspace-store.js.map