/**
 * Plan how to distribute specs across a team of Claude Code agents.
 * Detects file conflicts between specs and produces phased execution order.
 */
export function planTeamDistribution(specs, fileOwnership, template) {
    const templateName = template?.id ?? 'parallel-specs';
    if (specs.length === 0) {
        return {
            teamName: `team-${templateName}`,
            teamSize: 0,
            teammates: [],
            executionOrder: [],
            estimatedDuration: '0h',
            warnings: [],
        };
    }
    const teamSize = Math.min(specs.length, 5);
    const teammates = specs.slice(0, teamSize).map((s) => ({
        name: `${s.id.toLowerCase()}-impl`,
        role: 'implementer',
        model: 'sonnet',
        specs: [s.id],
        ownedFiles: fileOwnership[s.id] ?? [],
        prompt: '',
    }));
    // Detect file conflicts: specs sharing files go into sequential phases
    const fileToSpecs = new Map();
    for (const s of specs) {
        const files = fileOwnership[s.id] ?? [];
        for (const f of files) {
            const existing = fileToSpecs.get(f) ?? [];
            existing.push(s.id);
            fileToSpecs.set(f, existing);
        }
    }
    const conflicts = [...fileToSpecs.entries()].filter(([, sIds]) => sIds.length > 1);
    const conflictingSpecs = new Set(conflicts.flatMap(([, sIds]) => sIds));
    const warnings = conflicts.map(([file, sIds]) => `File conflict: ${file} shared by ${sIds.join(', ')}`);
    // Build phases: non-conflicting in phase 1, conflicting in phase 2+
    const phases = [];
    const phase1Names = teammates
        .filter((t) => !t.specs.some((id) => conflictingSpecs.has(id)))
        .map((t) => t.name);
    const phase2Names = teammates
        .filter((t) => t.specs.some((id) => conflictingSpecs.has(id)))
        .map((t) => t.name);
    if (phase1Names.length > 0) {
        phases.push({
            phase: 1,
            teammates: phase1Names,
            reason: 'No file conflicts — safe for parallel execution',
        });
    }
    if (phase2Names.length > 0) {
        phases.push({
            phase: phases.length + 1,
            teammates: phase2Names,
            reason: 'File conflicts detected — sequential execution required',
        });
    }
    // If all are conflict-free and phases is empty, put everything in phase 1
    if (phases.length === 0 && teammates.length > 0) {
        phases.push({
            phase: 1,
            teammates: teammates.map((t) => t.name),
            reason: 'No file conflicts — safe for parallel execution',
        });
    }
    return {
        teamName: `team-${templateName}`,
        teamSize,
        teammates,
        executionOrder: phases,
        estimatedDuration: `${String(teamSize * 2)}h`,
        warnings,
    };
}
//# sourceMappingURL=distribution-planner.js.map