import { v4 as uuid } from 'uuid';
import { join } from 'node:path';
import { getDataDir, readJsonFile, writeJsonFile } from '../storage/json-store.js';
import { getIssue } from '../storage/issue-store.js';
function issueCommentsFilePath(workspaceId, issueId) {
    return join(getDataDir(), 'workspaces', workspaceId, 'issues', `${issueId}.comments.json`);
}
export function listIssueComments(workspaceId, issueId) {
    return readJsonFile(issueCommentsFilePath(workspaceId, issueId)) || [];
}
export function createIssueComment(workspaceId, issueId, data) {
    if (!getIssue(workspaceId, issueId))
        return null;
    const now = new Date().toISOString();
    const comments = listIssueComments(workspaceId, issueId);
    const comment = {
        id: uuid(),
        workspaceId,
        issueId,
        senderId: data.senderId,
        senderRole: data.senderRole,
        content: data.content,
        source: data.source,
        metadata: data.metadata,
        createdAt: now,
    };
    comments.push(comment);
    writeJsonFile(issueCommentsFilePath(workspaceId, issueId), comments);
    return comment;
}
export function updateIssueComment(workspaceId, issueId, commentId, data) {
    const comments = listIssueComments(workspaceId, issueId);
    const index = comments.findIndex((comment) => comment.id === commentId);
    if (index === -1)
        return null;
    const updated = {
        ...comments[index],
        ...data,
        updatedAt: new Date().toISOString(),
    };
    comments[index] = updated;
    writeJsonFile(issueCommentsFilePath(workspaceId, issueId), comments);
    return updated;
}
export function deleteIssueComment(workspaceId, issueId, commentId) {
    const comments = listIssueComments(workspaceId, issueId);
    const index = comments.findIndex((comment) => comment.id === commentId);
    if (index === -1)
        return false;
    comments.splice(index, 1);
    writeJsonFile(issueCommentsFilePath(workspaceId, issueId), comments);
    return true;
}
//# sourceMappingURL=issue-comment.js.map