// tools/init-project/hooks-configurator.ts — Write Planu hooks to .claude.json
// SPEC-263: Autonomous CLAUDE.md that makes Planu mandatory
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
const PLANU_PRE_PLAN_HOOK = {
    matcher: 'EnterPlanMode',
    hooks: [
        {
            type: 'command',
            command: 'bash .claude/hooks/planu-pre-plan.sh 2>/dev/null || true',
        },
    ],
};
const PLANU_POST_PLAN_HOOK = {
    matcher: 'ExitPlanMode',
    hooks: [
        {
            type: 'command',
            command: 'bash .claude/hooks/planu-post-plan.sh 2>/dev/null || true',
        },
    ],
};
/**
 * Read .claude.json from projectPath. Returns an empty object if file is missing.
 */
async function readClaudeJson(projectPath) {
    const filePath = join(projectPath, '.claude.json');
    try {
        const raw = await readFile(filePath, 'utf-8');
        return JSON.parse(raw);
    }
    catch {
        return {};
    }
}
/**
 * Write .claude.json to disk.
 */
async function writeClaudeJson(projectPath, data) {
    const filePath = join(projectPath, '.claude.json');
    await writeFile(filePath, JSON.stringify(data, null, 2) + '\n', 'utf-8');
}
/**
 * Check whether a hook matcher entry (identified by matcher string) already exists
 * in the given array. Returns true if found.
 */
function hookAlreadyExists(hooks, matcher) {
    return hooks.some((h) => h.matcher === matcher);
}
/**
 * Add PreToolUse EnterPlanMode hook to .claude.json.
 * Idempotent — skips if the entry already exists.
 */
export async function configurePlanModeHooks(projectPath) {
    const config = await readClaudeJson(projectPath);
    config.hooks ??= {};
    config.hooks.PreToolUse ??= [];
    if (!hookAlreadyExists(config.hooks.PreToolUse, PLANU_PRE_PLAN_HOOK.matcher)) {
        config.hooks.PreToolUse.push(PLANU_PRE_PLAN_HOOK);
        await writeClaudeJson(projectPath, config);
    }
}
/**
 * Add PostToolUse ExitPlanMode hook to .claude.json.
 * Idempotent — skips if the entry already exists.
 */
export async function configureSessionHooks(projectPath) {
    const config = await readClaudeJson(projectPath);
    config.hooks ??= {};
    config.hooks.PostToolUse ??= [];
    if (!hookAlreadyExists(config.hooks.PostToolUse, PLANU_POST_PLAN_HOOK.matcher)) {
        config.hooks.PostToolUse.push(PLANU_POST_PLAN_HOOK);
        await writeClaudeJson(projectPath, config);
    }
}
/**
 * Configure all Planu hooks in .claude.json (plan mode + session).
 * Idempotent — safe to call multiple times.
 * @returns true if any hook was added (i.e. file was modified).
 */
export async function configurePlanuHooks(projectPath) {
    const config = await readClaudeJson(projectPath);
    const original = JSON.stringify(config);
    config.hooks ??= {};
    config.hooks.PreToolUse ??= [];
    config.hooks.PostToolUse ??= [];
    let modified = false;
    if (!hookAlreadyExists(config.hooks.PreToolUse, PLANU_PRE_PLAN_HOOK.matcher)) {
        config.hooks.PreToolUse.push(PLANU_PRE_PLAN_HOOK);
        modified = true;
    }
    if (!hookAlreadyExists(config.hooks.PostToolUse, PLANU_POST_PLAN_HOOK.matcher)) {
        config.hooks.PostToolUse.push(PLANU_POST_PLAN_HOOK);
        modified = true;
    }
    if (modified || JSON.stringify(config) !== original) {
        await writeClaudeJson(projectPath, config);
        return true;
    }
    return false;
}
//# sourceMappingURL=hooks-configurator.js.map