import { readJson, writeJson, hashProjectPath } from './base-store.js';
import { withFileLock } from './file-mutex.js';
function debtFile(projectPath) {
    const hash = hashProjectPath(projectPath);
    return `data/projects/${hash}/tech-debt.json`;
}
function configFile(projectPath) {
    const hash = hashProjectPath(projectPath);
    return `data/projects/${hash}/tech-debt-config.json`;
}
/** Load all tech debt items for a project. */
export async function loadTechDebt(projectPath) {
    return readJson(debtFile(projectPath), []);
}
/** Save a new tech debt item (appends). */
export async function saveTechDebtEntry(projectPath, item) {
    const file = debtFile(projectPath);
    await withFileLock(file, async () => {
        const existing = await readJson(file, []);
        existing.push(item);
        await writeJson(file, existing);
    });
}
/** Update a single tech debt item by ID. Returns updated item or null. */
export async function updateTechDebtEntry(projectPath, id, patch) {
    const file = debtFile(projectPath);
    let result = null;
    await withFileLock(file, async () => {
        const existing = await readJson(file, []);
        const idx = existing.findIndex((i) => i.id === id);
        if (idx === -1) {
            return;
        }
        existing[idx] = { ...existing[idx], ...patch };
        await writeJson(file, existing);
        result = existing[idx] ?? null;
    });
    return result;
}
/** Load tech debt config. */
export async function loadTechDebtConfig(projectPath) {
    return readJson(configFile(projectPath), {});
}
/** Save tech debt config. */
export async function saveTechDebtConfig(projectPath, config) {
    const file = configFile(projectPath);
    await withFileLock(file, async () => {
        await writeJson(file, config);
    });
}
//# sourceMappingURL=tech-debt-store.js.map