// Planu — Skill & Rules Generator Engine
// Orchestrator: imports from subdirectory modules and re-exports public API
import { loadPlatforms } from './skill-generator/detect.js';
import { buildProjectOverviewSection, buildArchitectureSection, buildConventionsSection, buildQualitySection, buildWorkflowSection, buildConstitutionSection, buildSessionOptimizationSection as _buildSessionOptimizationSection, buildRecommendedSkillsSection, } from './skill-generator/sections.js';
import { formatRulesForPlatform } from './skill-generator/format.js';
import { generateProjectRulesSkill, generateDomainSkill, generateWorkflowSkill, generateConventionSkill, generateSddWorkflowSkillContent, buildInstallInstructions, generateOrchestrationScript as _generateOrchestrationScript, } from './skill-generator/skills.js';
// Re-exports for consumers who import from skill-generator.js
export { getAgentConfig, detectAgentPlatform, detectAgentPlatformFromFiles, } from './skill-generator/detect.js';
export { buildSessionOptimizationSection } from './skill-generator/sections.js';
export { injectManagedSkillsBlock } from './skill-generator/sections.js';
export { generateOrchestrationScript } from './skill-generator/skills.js';
// === Public API ===
/**
 * Generate a skill file for a specific AI agent platform.
 */
export async function generateSkill(platform, skillType, name, description, knowledge, specContent) {
    const platforms = await loadPlatforms();
    const config = platforms.platforms[platform];
    if (!config) {
        return {
            platform,
            files: [],
            installInstructions: `Platform "${platform}" is not yet supported.`,
            message: `Unknown platform: ${platform}`,
        };
    }
    const files = [];
    switch (skillType) {
        case 'project-rules':
            files.push(generateProjectRulesSkill(config, name, description, knowledge));
            break;
        case 'domain-skill': {
            let docSection = '';
            try {
                const { detectTechsFromText } = await import('./web-fetcher/stack-detector.js');
                const { fetchDocsBatch } = await import('./web-fetcher/docs-intelligence.js');
                const { buildDocAwareSections } = await import('./skill-generator/sections-docs.js');
                const detected = detectTechsFromText(`${name} ${description}`);
                const techNames = detected
                    .filter((t) => t.confidence === 'high' || t.confidence === 'medium')
                    .map((t) => t.name)
                    .slice(0, 4);
                if (techNames.length > 0) {
                    const docsMap = await fetchDocsBatch(techNames, { timeout: 3_000, concurrency: 2 });
                    docSection = buildDocAwareSections(docsMap);
                }
            }
            catch {
                // Silent fallback — doc enrichment is best-effort
            }
            files.push(generateDomainSkill(config, name, description, knowledge, specContent, docSection || undefined));
            // 3-layer automation: include orchestration script for implementation skills
            files.push(_generateOrchestrationScript(name, knowledge));
            break;
        }
        case 'workflow':
            files.push(generateWorkflowSkill(config, name, description, knowledge));
            break;
        case 'convention':
            files.push(generateConventionSkill(config, name, description, knowledge));
            break;
    }
    const installInstructions = buildInstallInstructions(platform, config, files, skillType);
    return {
        platform,
        files,
        installInstructions,
        message: `Generated ${skillType} skill "${name}" for ${config.name}.`,
    };
}
/**
 * Generate platform-specific rules from project knowledge and constitution.
 */
export async function generateRules(platform, knowledge, constitution, options = {}) {
    const platforms = await loadPlatforms();
    const config = platforms.platforms[platform];
    if (!config) {
        return {
            platform,
            rulesFile: { path: '', content: 'Platform not supported', format: 'markdown' },
            additionalFiles: [],
            rulesCount: 0,
            categories: [],
            recommendedSkills: [],
        };
    }
    const { includeArchitecture = true, includeConventions = true, includeQuality = true, includeWorkflow = true, customRules = [], recommendedSkills = [], } = options;
    const sections = [];
    const categories = [];
    // Project overview
    sections.push(buildProjectOverviewSection(knowledge));
    categories.push('project');
    // Architecture rules
    if (includeArchitecture) {
        sections.push(buildArchitectureSection(knowledge));
        categories.push('architecture');
    }
    // Conventions
    if (includeConventions) {
        sections.push(buildConventionsSection(knowledge));
        categories.push('conventions');
    }
    // Quality rules
    if (includeQuality) {
        sections.push(buildQualitySection(knowledge));
        categories.push('quality');
    }
    // Workflow rules
    if (includeWorkflow) {
        sections.push(buildWorkflowSection(knowledge));
        categories.push('workflow');
    }
    // Constitution principles
    if (constitution) {
        sections.push(buildConstitutionSection(constitution));
        categories.push('constitution');
    }
    // Session optimization (platform-specific — idiomatic per platform)
    const sessionSection = _buildSessionOptimizationSection(platform);
    if (sessionSection) {
        sections.push(sessionSection);
        categories.push('session-optimization');
    }
    // Recommended skills (auto-fetched from skills.sh)
    const skillsSection = buildRecommendedSkillsSection(recommendedSkills);
    if (skillsSection) {
        sections.push(skillsSection);
        categories.push('recommended-skills');
    }
    // Custom rules
    if (customRules.length > 0) {
        sections.push({
            title: 'Custom Rules',
            rules: customRules.map((r) => ({ rule: r, enforcement: 'strict' })),
        });
        categories.push('custom');
    }
    const rulesContent = formatRulesForPlatform(platform, config, sections);
    const rulesCount = sections.reduce((sum, s) => sum + s.rules.length, 0);
    const rulesFile = {
        path: config.rulesFile,
        content: rulesContent,
        format: config.format,
    };
    const additionalFiles = [];
    // For Claude Code, generate an additional skills directory if needed
    if (platform === 'claude-code' && config.skillsDir) {
        additionalFiles.push({
            path: `${config.skillsDir}sdd-workflow.md`,
            content: generateSddWorkflowSkillContent(knowledge),
            format: 'markdown',
        });
    }
    return {
        platform,
        rulesFile,
        additionalFiles,
        rulesCount,
        categories,
        recommendedSkills,
    };
}
//# sourceMappingURL=skill-generator.js.map