/**
 * Auto-configure Claude Code hooks in ~/.claude/settings.json.
 */
import fs from 'fs';
import path from 'path';
import os from 'os';
const SETTINGS_PATH = path.join(os.homedir(), '.claude', 'settings.json');
const CORTEX_HOOKS = {
    PreCompact: {
        hooks: [{ type: 'command', command: 'npx shieldcortex hook pre-compact', timeout: 10 }],
    },
    SessionStart: {
        hooks: [{ type: 'command', command: 'npx shieldcortex hook session-start', timeout: 5 }],
    },
    SessionEnd: {
        hooks: [{ type: 'command', command: 'npx shieldcortex hook session-end', timeout: 10 }],
    },
};
const STOP_HOOK = {
    hooks: [{ type: 'command', command: 'npx shieldcortex hook stop', timeout: 10 }],
};
function hasCortexHook(entries) {
    return entries.some((e) => e.hooks?.some((h) => typeof h.command === 'string' && h.command.includes('shieldcortex')));
}
function readSettings() {
    if (!fs.existsSync(SETTINGS_PATH)) {
        return {};
    }
    try {
        return JSON.parse(fs.readFileSync(SETTINGS_PATH, 'utf-8'));
    }
    catch {
        return {};
    }
}
function writeSettings(settings) {
    const dir = path.dirname(SETTINGS_PATH);
    fs.mkdirSync(dir, { recursive: true });
    fs.writeFileSync(SETTINGS_PATH, JSON.stringify(settings, null, 2) + '\n', 'utf-8');
}
export function setupHooks(options) {
    const settings = readSettings();
    if (!settings.hooks) {
        settings.hooks = {};
    }
    let added = 0;
    // Install command hooks (PreCompact, SessionStart, SessionEnd)
    for (const [name, entry] of Object.entries(CORTEX_HOOKS)) {
        if (!Array.isArray(settings.hooks[name])) {
            settings.hooks[name] = [];
        }
        if (!hasCortexHook(settings.hooks[name])) {
            settings.hooks[name].push(entry);
            added++;
            console.log(`  + Hook: ${name}`);
        }
        else {
            console.log(`  = Hook: ${name} (already configured)`);
        }
    }
    // Optionally install Stop hook
    if (options?.stopHook) {
        if (!Array.isArray(settings.hooks.Stop)) {
            settings.hooks.Stop = [];
        }
        if (!hasCortexHook(settings.hooks.Stop)) {
            settings.hooks.Stop.push(STOP_HOOK);
            added++;
            console.log(`  + Hook: Stop (opt-in)`);
        }
        else {
            console.log(`  = Hook: Stop (already configured)`);
        }
    }
    if (added > 0) {
        writeSettings(settings);
        console.log(`Hooks: ${added} hook(s) added to ~/.claude/settings.json`);
    }
    else {
        console.log('Hooks: all hooks already configured in ~/.claude/settings.json');
    }
}
//# sourceMappingURL=settings-hooks.js.map