// storage/spec-hooks-store.ts — SPEC-368: Persists spec lifecycle hooks
// Layout: data/projects/{hash}/spec-hooks.json
import { readJson, writeJson, projectDataDir, hashProjectPath } from './base-store.js';
function hooksFile(projectPath) {
    const projectId = hashProjectPath(projectPath);
    return `${projectDataDir(projectId)}/spec-hooks.json`;
}
export async function loadSpecHooks(projectPath) {
    return readJson(hooksFile(projectPath), {
        hooks: [],
        updatedAt: new Date().toISOString(),
    });
}
export async function saveSpecHooks(projectPath, config) {
    await writeJson(hooksFile(projectPath), { ...config, updatedAt: new Date().toISOString() });
}
export async function addSpecHook(projectPath, hook) {
    const config = await loadSpecHooks(projectPath);
    const hooks = [...config.hooks];
    const existing = hooks.findIndex((h) => h.name === hook.name);
    if (existing >= 0) {
        hooks[existing] = hook;
    }
    else {
        hooks.push(hook);
    }
    await saveSpecHooks(projectPath, { ...config, hooks });
}
export async function getSpecHook(projectPath, hookName) {
    const config = await loadSpecHooks(projectPath);
    return config.hooks.find((h) => h.name === hookName) ?? null;
}
export async function updateSpecHookEnabled(projectPath, hookName, enabled) {
    const config = await loadSpecHooks(projectPath);
    const hook = config.hooks.find((h) => h.name === hookName);
    if (!hook) {
        return false;
    }
    hook.enabled = enabled;
    await saveSpecHooks(projectPath, config);
    return true;
}
export async function updateSpecHookStats(projectPath, hookName, success) {
    const config = await loadSpecHooks(projectPath);
    const hook = config.hooks.find((h) => h.name === hookName);
    if (!hook) {
        return;
    }
    hook.runCount += 1;
    if (!success) {
        hook.failCount += 1;
    }
    hook.lastRunAt = new Date().toISOString();
    await saveSpecHooks(projectPath, config);
}
//# sourceMappingURL=spec-hooks-store.js.map