// engine/ai-integration/kiro/hooks-generator.ts — Kiro hooks generator (SPEC-270)
import { writeFileSync, mkdirSync, existsSync } from 'node:fs';
import { join } from 'node:path';
const PROMPT_INJECT_SCRIPT = [
    'SPEC=$(planu list --status=approved --compact 2>/dev/null | head -5 || echo "No approved specs");',
    'BRANCH=$(git branch --show-current 2>/dev/null || echo "unknown");',
    'echo "=== Planu Context ===";',
    'echo "Branch: $BRANCH";',
    'echo "Approved specs: $SPEC"',
].join(' ');
const POST_TOOL_SCRIPT = [
    'if echo "$KIRO_TOOL_NAME" | grep -qE "write|edit|create"; then',
    '  if echo "$KIRO_TOOL_PATH" | grep -qE "^src/"; then',
    '    planu validate --quick 2>/dev/null || echo "Reminder: validate spec compliance after editing src/";',
    '  fi;',
    'fi',
].join(' ');
const STOP_SCRIPT = [
    'planu session_checkpoint --auto 2>/dev/null;',
    'if [ -d "planu/" ]; then',
    '  if [ "$PLANU_ENABLE_AUTOCOMMIT" != "true" ]; then exit 0; fi;',
    '  git add planu/specs/ planu/conventions.json 2>/dev/null;',
    '  git diff --staged --quiet || git commit -m "chore: auto-save planu state [kiro]" 2>/dev/null;',
    'fi',
].join(' ');
/** Returns the standard set of Kiro hooks for Planu SDD workflow. */
export function generateKiroHooks() {
    return [
        {
            id: 'planu-prompt-inject',
            event: 'userPromptSubmit',
            description: 'Inject active spec context into every Kiro prompt',
            script: PROMPT_INJECT_SCRIPT,
        },
        {
            id: 'planu-post-tool',
            event: 'postToolUse',
            description: 'Auto-validate spec compliance after file writes to src/',
            script: POST_TOOL_SCRIPT,
        },
        {
            id: 'planu-stop',
            event: 'stop',
            description: 'Save session checkpoint and optionally auto-commit planu state on session end',
            script: STOP_SCRIPT,
        },
    ];
}
function buildHookYaml(hook) {
    const lines = [
        `id: ${hook.id}`,
        `event: ${hook.event}`,
        `description: "${hook.description}"`,
        'script: |',
    ];
    for (const line of hook.script.split(' ')) {
        lines.push(`  ${line}`);
    }
    return lines.join('\n');
}
/** Writes .kiro/hooks/planu-*.yaml files to the given project path */
export function writeKiroHooks(projectPath) {
    const hooksDir = join(projectPath, '.kiro', 'hooks');
    /* v8 ignore start */
    if (!existsSync(hooksDir)) {
        mkdirSync(hooksDir, { recursive: true });
    }
    /* v8 ignore stop */
    const hooks = generateKiroHooks();
    for (const hook of hooks) {
        const yaml = buildHookYaml(hook);
        writeFileSync(join(hooksDir, `${hook.id}.yaml`), yaml + '\n', 'utf8');
    }
}
//# sourceMappingURL=hooks-generator.js.map