// ── Layer path mappings ────────────────────────────────────────────────────
const LAYER_PATHS = {
    frontend: ['src/components/', 'src/pages/', 'src/app/', 'src/views/', 'src/ui/', 'src/screens/'],
    backend: ['src/api/', 'src/server/', 'src/routes/', 'src/controllers/', 'src/handlers/'],
    engine: ['src/engine/', 'src/domain/', 'src/services/', 'src/core/', 'src/use-cases/'],
    domain: ['src/domain/', 'src/engine/', 'src/services/', 'src/core/'],
    storage: ['src/storage/', 'src/db/', 'src/models/', 'src/repositories/', 'src/data/'],
    types: ['src/types/'],
    tests: ['tests/', '__tests__/', 'spec/', 'test/'],
    config: ['src/config/', 'config/', '.claude/'],
    tools: ['src/tools/', 'src/adapters/', 'src/interfaces/'],
};
const LAYER_ALIASES = {
    domain: 'engine',
    'data-access': 'storage',
    data: 'storage',
    db: 'storage',
    database: 'storage',
    ui: 'frontend',
    views: 'frontend',
    api: 'backend',
    server: 'backend',
    routes: 'backend',
};
// ── Model selection per layer ──────────────────────────────────────────────
function recommendModel(layer) {
    if (layer === 'types' || layer === 'config') {
        return 'haiku';
    }
    if (layer === 'engine' || layer === 'domain') {
        return 'sonnet';
    }
    return 'sonnet';
}
// ── Stack-specific instructions ────────────────────────────────────────────
function stackInstructions(knowledge, layer) {
    const lines = [];
    const { framework, language } = knowledge;
    if (layer === 'frontend') {
        if (framework === 'react' || framework === 'next') {
            lines.push('Use functional components with hooks (no class components).');
            lines.push('JSX files must use .tsx extension. Props typed with interfaces.');
        }
        if (framework === 'vue') {
            lines.push('Use Composition API with <script setup> syntax.');
            lines.push('Props defined with defineProps<T>(). Emits with defineEmits<T>().');
        }
        if (framework === 'svelte') {
            lines.push('Use Svelte 5 runes ($state, $derived, $effect) when available.');
        }
    }
    if (layer === 'backend' || layer === 'engine') {
        if (language === 'typescript') {
            lines.push('Strict TypeScript — no `any`, no non-null assertions without comment.');
            lines.push('Return types must be explicit on all exported functions.');
        }
        if (framework === 'express' || framework === 'fastify') {
            lines.push('Validate all request inputs with Zod before business logic.');
        }
    }
    if (layer === 'storage') {
        lines.push('All database operations must be wrapped in try/catch with typed errors.');
        if (knowledge.orm) {
            lines.push(`ORM in use: ${knowledge.orm}. Follow its transaction patterns.`);
        }
    }
    return lines;
}
// ── Prompt builder ─────────────────────────────────────────────────────────
function buildPrompt(layer, ownedPaths, knowledge) {
    const { language, framework, architecture, conventions, testCommand, lintCommand } = knowledge;
    const stackLines = stackInstructions(knowledge, layer);
    const sections = [
        `# Agent: ${layer}-agent`,
        '',
        '## Identity',
        `You are the **${layer}** specialist agent for this project.`,
        `Your exclusive responsibility is the \`${layer}\` layer.`,
        '',
        '## File Ownership',
        'You have EXCLUSIVE write access to:',
        ...ownedPaths.map((p) => `  - ${p}`),
        '',
        'You must NOT modify files outside your owned paths.',
        'If a change requires touching another layer, coordinate explicitly.',
        '',
        '## Project Context',
        `- Language: ${language}`,
        ...(framework ? [`- Framework: ${framework}`] : []),
        `- Architecture: ${architecture.primary}`,
        ...(architecture.layers.length > 0
            ? [`- Detected layers: ${architecture.layers.map((l) => l.name).join(', ')}`]
            : []),
        '',
    ];
    if (stackLines.length > 0) {
        sections.push('## Stack-Specific Rules');
        sections.push(...stackLines.map((l) => `- ${l}`));
        sections.push('');
    }
    const conventionEntries = Object.entries(conventions);
    if (conventionEntries.length > 0) {
        sections.push('## Project Conventions');
        sections.push(...conventionEntries.map(([k, v]) => `- **${k}**: ${v}`));
        sections.push('');
    }
    sections.push('## Quality Gates');
    sections.push('Before marking any task done, run:');
    if (lintCommand) {
        sections.push(`  1. \`${lintCommand}\` — zero errors`);
    }
    if (testCommand) {
        sections.push(`  2. \`${testCommand}\` — all tests green`);
    }
    sections.push('  - Coverage must remain >= 95%.');
    sections.push('  - No TypeScript errors introduced.');
    sections.push('');
    sections.push('## Commit Convention');
    sections.push('- Use conventional commits: `feat:`, `fix:`, `refactor:`, `test:`, `docs:`.');
    sections.push('- Never commit directly to `main` or `develop`.');
    sections.push('- Each commit should be atomic and independently revertable.');
    return sections.join('\n');
}
// ── Path resolver ──────────────────────────────────────────────────────────
function resolveOwnedPaths(layer, knowledge) {
    const canonical = LAYER_ALIASES[layer] ?? layer;
    const defaults = LAYER_PATHS[canonical] ?? [`src/${layer}/`];
    // Prefer detected directories from architecture if available
    const detected = knowledge.architecture.layers.find((l) => l.name.toLowerCase() === layer.toLowerCase());
    if (detected && detected.directories.length > 0) {
        return detected.directories;
    }
    return defaults;
}
// ── Public API ─────────────────────────────────────────────────────────────
/**
 * Generate layer-specific agent prompts from detected project knowledge.
 * Each prompt defines file ownership, stack conventions, and quality rules
 * for the corresponding architecture layer.
 *
 * Never throws — returns empty array on unexpected errors.
 */
export function generateAgentPrompts(knowledge, filterLayer) {
    try {
        // Determine layers to generate prompts for
        let layers = knowledge.layers.length > 0 ? knowledge.layers : ['engine', 'storage', 'types'];
        if (filterLayer) {
            const canonical = LAYER_ALIASES[filterLayer] ?? filterLayer;
            layers = layers.filter((l) => l.toLowerCase() === filterLayer.toLowerCase() || l.toLowerCase() === canonical);
            // If no match found, generate for the requested layer anyway
            if (layers.length === 0) {
                layers = [filterLayer];
            }
        }
        return layers.map((layer) => {
            const canonical = LAYER_ALIASES[layer] ?? layer;
            const ownedPaths = resolveOwnedPaths(layer, knowledge);
            return {
                name: `${canonical}-agent`,
                layer: canonical,
                ownedPaths,
                prompt: buildPrompt(canonical, ownedPaths, knowledge),
                recommendedModel: recommendModel(canonical),
            };
        });
    }
    catch {
        return [];
    }
}
//# sourceMappingURL=agent-prompt-generator.js.map