// Write ~/.claude/settings.json from template + user choices
// Does NOT overwrite existing settings — merges instead
import { writeFileSync, readFileSync, existsSync } from 'node:fs';
import { ensureDir, SETTINGS_FILE, CLAUDE_DIR } from '../utils/paths.js';
const DEFAULT_SETTINGS = {
    $comment: 'Generated by ai-bootstrap. Edit anytime.',
    permissions: {
        allow: [
            'Bash(git status)',
            'Bash(git diff)',
            'Bash(git log *)',
            'Bash(pnpm *)',
            'Bash(npm *)',
            'Bash(npx *)',
            'Bash(ls *)',
            'Bash(cat *)',
            'Bash(head *)',
            'Bash(tail *)',
            'Bash(grep *)',
            'Bash(find . *)',
            'Read',
            'Edit',
            'Write',
        ],
        deny: [
            'Bash(rm -rf *)',
            'Bash(sudo *)',
            'Bash(curl * | sh)',
            'Bash(wget * | sh)',
        ],
        defaultMode: 'default',
    },
    model: 'opus',
    alwaysThinkingEnabled: false,
    env: {
        AI_BOOTSTRAP_VERSION: '0.0.1',
    },
    attribution: {
        commit: '🤖 Co-Authored-By: Claude (via ai-bootstrap)',
    },
};
export function writeSettings(state) {
    ensureDir(CLAUDE_DIR);
    // Merge with existing settings (don't overwrite user customizations)
    let merged = { ...DEFAULT_SETTINGS };
    if (existsSync(SETTINGS_FILE)) {
        try {
            const raw = readFileSync(SETTINGS_FILE, 'utf-8');
            const existing = JSON.parse(raw);
            merged = { ...merged, ...existing };
            // Merge permissions arrays
            if (existing.permissions?.allow) {
                const allowSet = new Set([
                    ...(DEFAULT_SETTINGS.permissions?.allow ?? []),
                    ...existing.permissions.allow,
                ]);
                merged.permissions = { ...merged.permissions, allow: Array.from(allowSet) };
            }
        }
        catch {
            // Corrupt file, use defaults
        }
    }
    // Apply wizard-specific values
    if (state.profile?.primaryLanguage) {
        merged.env = {
            ...merged.env,
            AI_BOOTSTRAP_PRIMARY_LANG: state.profile.primaryLanguage,
        };
    }
    writeFileSync(SETTINGS_FILE, JSON.stringify(merged, null, 2), 'utf-8');
    return SETTINGS_FILE;
}
//# sourceMappingURL=settings-writer.js.map