import { join } from 'node:path';
import { readdirSync } from 'node:fs';
import { getDataDir, ensureDir, readJsonFile, writeJsonFile, deleteFile } from './json-store.js';
function workspaceDir(id) {
    return join(getDataDir(), 'workspaces', id);
}
function hooksDir(wsId) {
    return join(workspaceDir(wsId), 'hooks');
}
function hookPath(wsId, name) {
    return join(hooksDir(wsId), `${name}.hook.json`);
}
export function listHooks(wsId) {
    const dir = hooksDir(wsId);
    try {
        const files = readdirSync(dir);
        return files
            .filter(f => f.endsWith('.hook.json'))
            .map(f => readJsonFile(join(dir, f)))
            .filter((h) => h !== null);
    }
    catch {
        return [];
    }
}
export function getHook(wsId, name) {
    return readJsonFile(hookPath(wsId, name));
}
export function saveHook(wsId, config) {
    ensureDir(hooksDir(wsId));
    writeJsonFile(hookPath(wsId, config.name), config);
}
export function deleteHook(wsId, name) {
    deleteFile(hookPath(wsId, name));
}
export function uploadHook(wsId, jsonString) {
    const parsed = JSON.parse(jsonString);
    if (!parsed.name || typeof parsed.name !== 'string') {
        throw new Error('Hook must have a "name" string field');
    }
    if (!parsed.hooks || typeof parsed.hooks !== 'object') {
        parsed.hooks = { PreToolUse: [], PostToolUse: [] };
    }
    if (parsed.enabled === undefined)
        parsed.enabled = true;
    saveHook(wsId, parsed);
    return parsed;
}
export function applyToWorkspace(sourceWsId, name, targetWsId) {
    const config = getHook(sourceWsId, name);
    if (!config)
        throw new Error(`Hook "${name}" not found in workspace ${sourceWsId}`);
    saveHook(targetWsId, config);
}
//# sourceMappingURL=hook-store.js.map