// Planu — Settings and extensions auditor for SPEC-092
import { readFile, access } from 'node:fs/promises';
import { join } from 'node:path';
const DOCS_REF = 'https://code.claude.com/docs/en/settings';
async function readSettings(filePath) {
    try {
        const content = await readFile(filePath, 'utf-8');
        return JSON.parse(content);
    }
    catch {
        return null;
    }
}
async function pathExists(p) {
    try {
        await access(p);
        return true;
    }
    catch {
        return false;
    }
}
export async function auditSettings(projectPath) {
    const findings = [];
    const settingsPath = join(projectPath, '.claude', 'settings.json');
    const localSettingsPath = join(projectPath, '.claude', 'settings.local.json');
    // Check main settings
    const settings = await readSettings(settingsPath);
    const localSettings = await readSettings(localSettingsPath);
    const allSettings = [
        { data: settings, label: 'settings.json' },
        { data: localSettings, label: 'settings.local.json' },
    ];
    for (const { data, label } of allSettings) {
        if (!data) {
            continue;
        }
        // Check for obsolete permission patterns
        const allowList = data.permissions?.allow ?? [];
        const obsoletePatterns = ['mcp__specforge__', 'mcp__sdd__'];
        for (const pattern of obsoletePatterns) {
            const obsolete = allowList.filter((p) => p.includes(pattern));
            if (obsolete.length > 0) {
                findings.push({
                    area: 'settings',
                    severity: 'warn',
                    message: `${label} has ${String(obsolete.length)} obsolete permission(s) matching "${pattern}"`,
                    suggestion: `Remove obsolete entries from permissions.allow: ${obsolete.join(', ')}`,
                    docsRef: DOCS_REF,
                });
            }
        }
        // Check autoMemoryEnabled
        if (data.autoMemoryEnabled === false) {
            findings.push({
                area: 'settings',
                severity: 'info',
                message: `${label} has autoMemoryEnabled: false — auto-memory is disabled`,
                suggestion: 'Consider enabling auto-memory for cross-session learning',
                docsRef: DOCS_REF,
            });
        }
    }
    // Check for skills directory
    const skillsDir = join(projectPath, '.claude', 'skills');
    if (!(await pathExists(skillsDir))) {
        findings.push({
            area: 'settings',
            severity: 'info',
            message: 'No .claude/skills/ directory found',
            suggestion: 'Create skills for repeatable workflows — invoke with /skill-name in Claude Code',
            docsRef: DOCS_REF,
        });
    }
    // Check for hooks configuration
    const hooksDir = join(projectPath, '.claude', 'hooks');
    if (!(await pathExists(hooksDir))) {
        findings.push({
            area: 'settings',
            severity: 'info',
            message: 'No .claude/hooks/ directory found',
            suggestion: 'Add hooks to automate quality gates (e.g., filter test output, truncate long logs)',
            docsRef: DOCS_REF,
        });
    }
    return findings;
}
//# sourceMappingURL=settings-auditor.js.map