import { join } from 'node:path';
import { unlink } from 'node:fs/promises';
import { getDataDir, ensureDir, readJsonFile, writeJsonFile } from './json-store.js';
function issuesDir(workspaceId) {
    return join(getDataDir(), 'workspaces', workspaceId, 'issues');
}
function issuesIndex(workspaceId) {
    return join(issuesDir(workspaceId), 'index.json');
}
function issueFile(workspaceId, issueId) {
    return join(issuesDir(workspaceId), `${issueId}.json`);
}
export function listIssues(workspaceId) {
    return readJsonFile(issuesIndex(workspaceId)) || [];
}
export function getIssue(workspaceId, issueId) {
    return readJsonFile(issueFile(workspaceId, issueId));
}
export function createIssue(issue) {
    ensureDir(issuesDir(issue.workspaceId));
    writeJsonFile(issueFile(issue.workspaceId, issue.id), issue);
    const list = listIssues(issue.workspaceId);
    list.push(issue);
    writeJsonFile(issuesIndex(issue.workspaceId), list);
}
export function updateIssue(issue) {
    writeJsonFile(issueFile(issue.workspaceId, issue.id), issue);
    const list = listIssues(issue.workspaceId);
    const idx = list.findIndex((i) => i.id === issue.id);
    if (idx >= 0)
        list[idx] = issue;
    writeJsonFile(issuesIndex(issue.workspaceId), list);
}
export function deleteIssue(workspaceId, issueId) {
    const list = listIssues(workspaceId).filter((i) => i.id !== issueId);
    writeJsonFile(issuesIndex(workspaceId), list);
    unlink(issueFile(workspaceId, issueId)).catch(() => { });
}
//# sourceMappingURL=issue-store.js.map