// engine/hooks/profile-generator.ts — Claude Code profile generator (SPEC-265)
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
// ---------------------------------------------------------------------------
// Profile builders
// ---------------------------------------------------------------------------
/**
 * Generates the "planu-safe" profile:
 * - Approval on-request for mutations
 * - Medium effort default
 * - Allows common read/edit tools; denies destructive git ops without spec
 */
export function generatePlanuSafeProfile() {
    return {
        name: 'planu-safe',
        permissions: {
            allow: [
                'mcp__planu__*',
                'Read',
                'Glob',
                'Grep',
                'Edit',
                'Write',
                'Bash(git status:*)',
                'Bash(git log:*)',
                'Bash(git diff:*)',
                'Bash(pnpm typecheck:*)',
                'Bash(pnpm lint:*)',
                'Bash(pnpm test:*)',
            ],
            deny: ['Bash(git push --force:*)', 'Bash(git reset --hard:*)', 'Bash(git clean -f:*)'],
        },
        env: {
            PLANU_EFFORT_DEFAULT: 'medium',
        },
    };
}
/**
 * Generates the "planu-fast" profile:
 * - Auto-approve all Planu tools and read ops
 * - Low effort for reads, medium for writes
 * - Allows all Planu tools without interruption
 */
export function generatePlanuFastProfile() {
    return {
        name: 'planu-fast',
        permissions: {
            allow: ['mcp__planu__*', 'Read', 'Glob', 'Grep', 'Edit', 'Write', 'Bash(*)'],
            deny: [],
        },
        env: {
            PLANU_EFFORT_DEFAULT: 'low',
            PLANU_AUTO_APPROVE: '1',
        },
    };
}
// ---------------------------------------------------------------------------
// Settings merge
// ---------------------------------------------------------------------------
/**
 * Reads .claude/settings.json at settingsPath, merges profiles into it,
 * and writes it back. Idempotent: profiles with the same name are replaced.
 */
export function mergeProfilesIntoSettings(settingsPath, profiles) {
    let settings = {};
    /* v8 ignore start */
    if (existsSync(settingsPath)) {
        try {
            const raw = readFileSync(settingsPath, 'utf8');
            settings = JSON.parse(raw);
        }
        catch {
            settings = {};
        }
    }
    /* v8 ignore stop */
    const existing = (settings.profiles ?? []);
    const merged = [...existing];
    for (const profile of profiles) {
        const idx = merged.findIndex((p) => p.name === profile.name);
        if (idx >= 0) {
            merged[idx] = profile;
        }
        else {
            merged.push(profile);
        }
    }
    settings.profiles = merged;
    writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf8');
}
//# sourceMappingURL=profile-generator.js.map