import { join } from 'node:path';
import { getDataDir, ensureDir, readJsonFile, writeJsonFile, deleteFile } from './json-store.js';
function tasksDir(workspaceId) {
    return join(getDataDir(), 'workspaces', workspaceId, 'tasks');
}
function tasksIndex(workspaceId) {
    return join(tasksDir(workspaceId), 'index.json');
}
function taskFile(workspaceId, taskId) {
    return join(tasksDir(workspaceId), `${taskId}.json`);
}
export function listTasks(workspaceId) {
    return readJsonFile(tasksIndex(workspaceId)) || [];
}
export function getTask(workspaceId, taskId) {
    return readJsonFile(taskFile(workspaceId, taskId));
}
export function createTask(task) {
    ensureDir(tasksDir(task.workspaceId));
    writeJsonFile(taskFile(task.workspaceId, task.id), task);
    const list = listTasks(task.workspaceId);
    list.push(task);
    writeJsonFile(tasksIndex(task.workspaceId), list);
}
export function updateTask(task) {
    writeJsonFile(taskFile(task.workspaceId, task.id), task);
    const list = listTasks(task.workspaceId);
    const idx = list.findIndex((t) => t.id === task.id);
    if (idx >= 0)
        list[idx] = task;
    writeJsonFile(tasksIndex(task.workspaceId), list);
}
export function deleteTask(workspaceId, taskId) {
    deleteFile(taskFile(workspaceId, taskId));
    const list = listTasks(workspaceId).filter((t) => t.id !== taskId);
    writeJsonFile(tasksIndex(workspaceId), list);
}
//# sourceMappingURL=task-store.js.map