// storage/hooks-store.ts — Persistence layer for agent hooks (SPEC-071)
// Files: data/projects/{projectId}/hooks.json + hooks-log.json
import { readJson, writeJson, projectDataDir } from './base-store.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const MAX_LOG_ENTRIES = 1_000;
// ---------------------------------------------------------------------------
// File paths
// ---------------------------------------------------------------------------
function hooksFile(projectId) {
    return `${projectDataDir(projectId)}/hooks.json`;
}
function hooksLogFile(projectId) {
    return `${projectDataDir(projectId)}/hooks-log.json`;
}
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
async function loadConfig(projectId) {
    return readJson(hooksFile(projectId), {
        hooks: [],
        lastUpdated: new Date().toISOString(),
    });
}
async function saveConfig(projectId, config) {
    await writeJson(hooksFile(projectId), config);
}
async function loadLogData(projectId) {
    return readJson(hooksLogFile(projectId), {
        entries: [],
        lastUpdated: new Date().toISOString(),
    });
}
// ---------------------------------------------------------------------------
// Hook CRUD
// ---------------------------------------------------------------------------
/**
 * Return all hook definitions for a project.
 */
export async function getHooks(projectId) {
    const config = await loadConfig(projectId);
    return config.hooks;
}
/**
 * Return a single hook by ID, or undefined if not found.
 */
export async function getHookById(projectId, hookId) {
    const config = await loadConfig(projectId);
    return config.hooks.find((h) => h.id === hookId);
}
/**
 * Persist a new hook definition.
 */
export async function saveHook(projectId, hook) {
    const config = await loadConfig(projectId);
    config.hooks.push(hook);
    config.lastUpdated = new Date().toISOString();
    await saveConfig(projectId, config);
}
/**
 * Apply partial updates to an existing hook. Throws if not found.
 */
export async function updateHook(projectId, hookId, updates) {
    const config = await loadConfig(projectId);
    const idx = config.hooks.findIndex((h) => h.id === hookId);
    if (idx === -1) {
        throw new Error(`Hook "${hookId}" not found in project "${projectId}".`);
    }
    const existing = config.hooks[idx];
    /* v8 ignore next 3 */
    if (!existing) {
        throw new Error(`Hook "${hookId}" not found in project "${projectId}".`);
    }
    const updated = {
        ...existing,
        ...updates,
        id: hookId,
        updatedAt: new Date().toISOString(),
    };
    config.hooks[idx] = updated;
    config.lastUpdated = new Date().toISOString();
    await saveConfig(projectId, config);
    return updated;
}
/**
 * Delete a hook by ID. Throws if not found.
 */
export async function deleteHook(projectId, hookId) {
    const config = await loadConfig(projectId);
    const idx = config.hooks.findIndex((h) => h.id === hookId);
    if (idx === -1) {
        throw new Error(`Hook "${hookId}" not found in project "${projectId}".`);
    }
    config.hooks.splice(idx, 1);
    config.lastUpdated = new Date().toISOString();
    await saveConfig(projectId, config);
}
/**
 * Return enabled hooks matching the given trigger.
 */
export async function getHooksByTrigger(projectId, trigger) {
    const config = await loadConfig(projectId);
    return config.hooks.filter((h) => h.trigger === trigger && h.enabled);
}
// ---------------------------------------------------------------------------
// Execution log
// ---------------------------------------------------------------------------
/**
 * Append an execution log entry. Trims entries beyond MAX_LOG_ENTRIES.
 */
export async function appendHookLog(projectId, entry) {
    const data = await loadLogData(projectId);
    data.entries.push(entry);
    if (data.entries.length > MAX_LOG_ENTRIES) {
        data.entries = data.entries.slice(data.entries.length - MAX_LOG_ENTRIES);
    }
    data.lastUpdated = new Date().toISOString();
    await writeJson(hooksLogFile(projectId), data);
}
/**
 * Return recent execution log entries for a project.
 */
export async function getHookLogs(projectId, limit = 50) {
    const data = await loadLogData(projectId);
    return data.entries.slice(-limit);
}
//# sourceMappingURL=hooks-store.js.map