const PHASE_COLORS = [
    '#7c3aed',
    '#0891b2',
    '#059669',
    '#d97706',
    '#e94560',
    '#6366f1',
    '#0f766e',
    '#b45309',
];
/**
 * Assign specs to weeks using a simple sequential schedule with parallelism.
 * Each spec duration = ceil(totalHours / 40) weeks (1 week = 40 dev hours).
 */
function assignWeeks(specs, phases) {
    if (specs.length === 0) {
        return [];
    }
    const phaseColorMap = new Map();
    if (phases && phases.length > 0) {
        phases.forEach((p, i) => {
            phaseColorMap.set(p.name, PHASE_COLORS[i % PHASE_COLORS.length] ?? '#7c3aed');
        });
    }
    let currentWeek = 1;
    const rows = [];
    const specWeekMap = new Map();
    // Build dependency map
    const specMap = new Map(specs.map((s) => [s.id, s]));
    for (const spec of specs) {
        const totalHours = spec.estimation.devHours + spec.estimation.reviewHours;
        const durationWeeks = Math.max(1, Math.ceil(totalHours / 40));
        // Determine start: after all dependencies
        let startWeek = currentWeek;
        for (const dep of spec.dependencies) {
            const depEnd = specWeekMap.get(dep)?.end ?? 0;
            startWeek = Math.max(startWeek, depEnd + 1);
        }
        if (!spec.dependencies.length) {
            startWeek = currentWeek;
        }
        specWeekMap.set(spec.id, { start: startWeek, end: startWeek + durationWeeks - 1 });
        // Find phase name
        let phaseName = 'Default';
        let color = PHASE_COLORS[0] ?? '#7c3aed';
        if (phases && phases.length > 0) {
            const foundPhase = phases.find((p) => p.specIds.includes(spec.id));
            if (foundPhase) {
                phaseName = foundPhase.name;
                const phaseIdx = phases.indexOf(foundPhase);
                color = foundPhase.color ?? PHASE_COLORS[phaseIdx % PHASE_COLORS.length] ?? '#7c3aed';
                phaseColorMap.set(phaseName, color);
            }
        }
        else {
            // Color by index
            const idx = rows.length;
            color = PHASE_COLORS[idx % PHASE_COLORS.length] ?? '#7c3aed';
        }
        rows.push({
            id: spec.id,
            label: `${spec.id}: ${spec.title.length > 30 ? spec.title.slice(0, 30) + '…' : spec.title}`,
            color,
            startWeek,
            durationWeeks,
            phase: phaseName,
        });
        // Advance current week only when no dependencies (sequential default)
        if (!spec.dependencies.length) {
            currentWeek = startWeek + durationWeeks;
        }
        // Cross-dep: track if we need to reset
        const depSpec = specMap.get(spec.id);
        if (!depSpec?.dependencies.length) {
            currentWeek = startWeek + durationWeeks;
        }
    }
    return rows;
}
/**
 * Build a CSS Grid Gantt chart from specs and optional phases.
 * Returns an HTML string with the full Gantt visualization.
 */
export function buildGanttChart(specs, phases, _startDate) {
    if (specs.length === 0) {
        return '';
    }
    const rows = assignWeeks(specs, phases);
    if (rows.length === 0) {
        return '';
    }
    const totalWeeks = Math.max(...rows.map((r) => r.startWeek + r.durationWeeks - 1), 4);
    // Week headers
    const weekHeaders = Array.from({ length: totalWeeks }, (_, i) => `<div class="gantt-week">W${String(i + 1)}</div>`).join('');
    // Gantt rows
    const ganttRows = rows.map((row) => {
        const startPct = ((row.startWeek - 1) / totalWeeks) * 100;
        const widthPct = (row.durationWeeks / totalWeeks) * 100;
        return `<div class="gantt-row">
  <div class="gantt-label" title="${escapeHtml(row.label)}">${escapeHtml(row.label)}</div>
  <div class="gantt-track">
    <div class="gantt-bar" style="left:${startPct.toFixed(1)}%;width:${widthPct.toFixed(1)}%;background:${escapeHtml(row.color)};">
      ${String(row.durationWeeks)}w
    </div>
  </div>
</div>`;
    });
    return `<div class="gantt-container">
  <div class="gantt-header">
    <div class="gantt-header-label"></div>
    <div class="gantt-header-weeks">${weekHeaders}</div>
  </div>
  <div class="gantt-grid">
${ganttRows.join('\n')}
  </div>
</div>`;
}
// Internal helper
function escapeHtml(str) {
    return str
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
}
//# sourceMappingURL=proposal-gantt-builder.js.map