function wrap(name, content) {
    return `<!-- planu:${name} -->\n${content}\n<!-- planu:${name} -->`;
}
const SERVERLESS_PLATFORMS = ['vercel', 'netlify', 'lambda', 'cloudflare-workers', 'deno-deploy'];
function isServerlessPlatform(knowledge) {
    const services = knowledge.externalServices.map((s) => s.toLowerCase());
    const deployPlatform = (knowledge.conventions['deploy-platform'] ?? '').toLowerCase();
    return SERVERLESS_PLATFORMS.some((p) => services.some((s) => s.includes(p)) || deployPlatform.includes(p));
}
function buildCodingConventions(knowledge) {
    const lines = [
        '## Coding Conventions',
        '- **Code language**: All variables, functions, comments, docstrings, database tables/columns, and API endpoints MUST be in English',
        '- **Naming**: camelCase for variables/functions, PascalCase for types/classes, kebab-case for files',
        '- **Database naming**: snake_case for tables and columns (e.g., user_profiles, created_at)',
        '- **Constants**: SCREAMING_SNAKE_CASE',
    ];
    if (isServerlessPlatform(knowledge)) {
        lines.push('- **Serverless rule**: No module-level mutable state for cross-request data — use Redis/database instead (cold starts reset module state)');
    }
    return wrap('coding-conventions', lines.join('\n'));
}
function buildStack(knowledge) {
    const items = [];
    const langLabel = knowledge.languageVersion
        ? `${knowledge.language} ${knowledge.languageVersion}`
        : knowledge.language;
    items.push(`- **Language**: ${langLabel}`);
    if (knowledge.framework) {
        const fw = knowledge.frameworkVersion
            ? `${knowledge.framework} ${knowledge.frameworkVersion}`
            : knowledge.framework;
        items.push(`- **Framework**: ${fw}`);
    }
    if (knowledge.database !== 'unknown') {
        const db = knowledge.orm ? `${knowledge.database} (${knowledge.orm})` : knowledge.database;
        items.push(`- **Database**: ${db}`);
    }
    if (knowledge.packageManager) {
        items.push(`- **Package Manager**: ${knowledge.packageManager}`);
    }
    if (knowledge.runtime) {
        const runtimeLabel = { node: 'Node.js', bun: 'Bun', deno: 'Deno' };
        items.push(`- **Runtime**: ${runtimeLabel[knowledge.runtime] ?? knowledge.runtime}`);
    }
    if (items.length === 1) {
        return null;
    } // only language is never "nothing"
    return wrap('stack', `## Stack\n${items.join('\n')}`);
}
const DEV_PATTERNS = ['next dev', 'vite', 'webpack serve', 'astro dev', 'nuxt dev', 'remix dev'];
const START_PATTERNS = ['node ', 'ts-node', 'bun run', 'deno run'];
function detectDevCommand(knowledge) {
    const pm = knowledge.packageManager ?? 'npm';
    const run = `${pm} run`;
    const build = knowledge.buildCommand ?? '';
    if (DEV_PATTERNS.some((p) => build.includes(p))) {
        return null;
    } // build is already dev-like
    return `${run} dev`;
}
function detectStartCommand(knowledge) {
    const pm = knowledge.packageManager ?? 'npm';
    if (START_PATTERNS.some((p) => (knowledge.buildCommand ?? '').includes(p))) {
        return null;
    }
    return `${pm} run start`;
}
function buildCommands(knowledge) {
    const pm = knowledge.packageManager ?? 'npm';
    const install = `${pm} install`;
    const lines = [`${install.padEnd(16)}# Install dependencies`];
    if (knowledge.buildCommand) {
        lines.push(`${knowledge.buildCommand.padEnd(16)}# Build project`);
    }
    const dev = detectDevCommand(knowledge);
    if (dev) {
        lines.push(`${dev.padEnd(16)}# Dev server`);
    }
    const start = detectStartCommand(knowledge);
    if (start) {
        lines.push(`${start.padEnd(16)}# Start server`);
    }
    if (knowledge.testCommand) {
        lines.push(`${knowledge.testCommand.padEnd(16)}# Run tests`);
    }
    if (knowledge.lintCommand) {
        lines.push(`${knowledge.lintCommand.padEnd(16)}# Lint code`);
    }
    if (lines.length === 1) {
        return null;
    }
    const block = `## Commands\n\`\`\`bash\n${lines.join('\n')}\n\`\`\``;
    return wrap('commands', block);
}
function buildArchitecture(knowledge) {
    const items = [];
    items.push(`- **Pattern**: ${knowledge.architecture.primary}`);
    items.push(`- **Monorepo**: ${knowledge.monorepo ? 'Yes' : 'No'}`);
    if (knowledge.layers.length > 0) {
        items.push(`- **Layers**: ${knowledge.layers.join(' → ')}`);
    }
    if (knowledge.apps.length > 1) {
        const appNames = knowledge.apps.map((a) => `${a.name} (${a.type})`).join(', ');
        items.push(`- **Apps**: ${appNames}`);
    }
    if (items.length <= 1) {
        return null;
    } // only monorepo=No is not useful alone
    return wrap('architecture', `## Architecture\n${items.join('\n')}`);
}
function detectTestFramework(testCommand) {
    if (testCommand.includes('vitest')) {
        return 'Vitest';
    }
    if (testCommand.includes('jest')) {
        return 'Jest';
    }
    if (testCommand.includes('pytest')) {
        return 'pytest';
    }
    if (testCommand.includes('go test')) {
        return 'go test';
    }
    if (testCommand.includes('cargo test')) {
        return 'cargo test';
    }
    if (testCommand.includes('mocha')) {
        return 'Mocha';
    }
    if (testCommand.includes('jasmine')) {
        return 'Jasmine';
    }
    if (testCommand.includes('rspec')) {
        return 'RSpec';
    }
    return 'Unknown';
}
function buildTesting(knowledge) {
    if (!knowledge.testCommand) {
        return null;
    }
    const framework = detectTestFramework(knowledge.testCommand);
    const items = [
        `- **Framework**: ${framework}`,
        `- **Command**: \`${knowledge.testCommand}\``,
    ];
    return wrap('testing', `## Testing\n${items.join('\n')}`);
}
const CI_PLATFORM_MAP = {
    'github-actions': 'GitHub Actions',
    'gitlab-ci': 'GitLab CI',
    circleci: 'CircleCI',
    travis: 'Travis CI',
    jenkins: 'Jenkins',
    bitbucket: 'Bitbucket Pipelines',
    'azure-devops': 'Azure DevOps',
};
function buildCiCd(knowledge) {
    const platform = knowledge.conventions['ci-platform'] ?? knowledge.tooling.ci ?? null;
    if (!platform) {
        return null;
    }
    const label = CI_PLATFORM_MAP[platform.toLowerCase()] ?? platform;
    return wrap('cicd', `## CI/CD\n- **Platform**: ${label}`);
}
const DEPLOY_PLATFORM_MAP = {
    vercel: 'Vercel',
    netlify: 'Netlify',
    railway: 'Railway',
    heroku: 'Heroku',
    'aws-lambda': 'AWS Lambda',
    'aws-ecs': 'AWS ECS',
    'google-cloud-run': 'Google Cloud Run',
    'azure-app-service': 'Azure App Service',
    fly: 'Fly.io',
    render: 'Render',
    cloudflare: 'Cloudflare Pages/Workers',
    docker: 'Docker',
    kubernetes: 'Kubernetes',
};
function buildDeployment(knowledge) {
    const fromConventions = knowledge.conventions['deploy-platform'] ?? null;
    const fromServices = knowledge.externalServices.find((s) => Object.keys(DEPLOY_PLATFORM_MAP).some((k) => s.toLowerCase().includes(k)));
    const raw = fromConventions ?? fromServices ?? null;
    if (!raw) {
        return null;
    }
    const key = Object.keys(DEPLOY_PLATFORM_MAP).find((k) => raw.toLowerCase().includes(k));
    const label = key ? DEPLOY_PLATFORM_MAP[key] : raw;
    return wrap('deployment', `## Deployment\n- **Platform**: ${label}`);
}
export function buildTeamCoordinationSection(knowledge) {
    const isMultiLayer = knowledge.layers.length > 2;
    const isMonorepo = knowledge.monorepo;
    if (!isMultiLayer && !isMonorepo) {
        return null;
    }
    const patternList = [
        '- `implement-and-test` — one agent implements, another writes tests (no shared files)',
        '- `full-stack` — frontend/backend split with clear API boundary',
        '- `research-then-build` — researcher gathers context, builder implements',
        '- `parallel-specs` — N agents implement N independent specs simultaneously',
    ];
    const lines = [
        '## Agent Teams (for 3+ specs in parallel)',
        '',
        'When implementing multiple independent specs simultaneously, use Agent Teams:',
        '',
        '1. Call `plan_team_distribution` with spec IDs → assigns exclusive file ownership per agent',
        '2. Call `generate_teammate_prompt` for each teammate → generates isolated prompt with context',
        '3. Spawn N Claude Code agents with the generated prompts (`claude -p "<prompt>"`)',
        '4. Call `validate_team_results` after completion → verifies merged code integrity',
        '',
        '**When to use teams**: 3+ independent specs ready, estimated effort >8h, clear module boundaries.',
        '',
        '**Available patterns:**',
        ...patternList,
        '',
        '> MANDATORY: two teammates NEVER modify the same file — `plan_team_distribution` enforces this.',
    ];
    return wrap('team-coordination', lines.join('\n'));
}
function buildGitWorkflow(knowledge) {
    const hasConventional = 'commitlint' in knowledge.conventions ||
        (knowledge.conventions['commit-style']?.toLowerCase().includes('conventional') ?? false) ||
        knowledge.tooling.commitlint !== undefined;
    if (!hasConventional) {
        return null;
    }
    const items = [
        '- **Commits**: Conventional Commits (commitlint)',
        '- **Branch naming**: feat/*, fix/*, chore/*',
    ];
    return wrap('git-workflow', `## Git Workflow\n${items.join('\n')}`);
}
export function buildProjectSections(knowledge) {
    const sections = [buildCodingConventions(knowledge)];
    const stack = buildStack(knowledge);
    if (stack) {
        sections.push(stack);
    }
    const commands = buildCommands(knowledge);
    if (commands) {
        sections.push(commands);
    }
    const architecture = buildArchitecture(knowledge);
    if (architecture) {
        sections.push(architecture);
    }
    const testing = buildTesting(knowledge);
    if (testing) {
        sections.push(testing);
    }
    const cicd = buildCiCd(knowledge);
    if (cicd) {
        sections.push(cicd);
    }
    const deployment = buildDeployment(knowledge);
    if (deployment) {
        sections.push(deployment);
    }
    const gitWorkflow = buildGitWorkflow(knowledge);
    if (gitWorkflow) {
        sections.push(gitWorkflow);
    }
    const teamCoordination = buildTeamCoordinationSection(knowledge);
    if (teamCoordination) {
        sections.push(teamCoordination);
    }
    // SPEC-655: Single hardcoded skill entry point — all others discovered via find-skills
    sections.push(wrap('find-skills', '## Skill Discovery\nUse find-skills to discover available workflows.'));
    // SPEC-755: Spec location preference — overrides brainstorming skill default (docs/superpowers/specs/)
    sections.push(wrap('spec-location', [
        '## Spec Location (User Preference — auto-generated by Planu)',
        'All specs go to Planu via `create_spec` tool. NEVER write design docs to docs/superpowers/specs/ or any docs/ directory. Planu is the single source of truth for all specs.',
    ].join('\n')));
    return sections.join('\n\n');
}
//# sourceMappingURL=claude-md-generator.js.map