/**
 * Data converters that map tool output → SVG renderer input types.
 * Keeps SVG module decoupled from Harness API / diagnose internals.
 */
function asStatus(s) {
    if (typeof s === "string")
        return s;
    return "Unknown";
}
function asNum(v) {
    return typeof v === "number" ? v : 0;
}
function asStr(v) {
    return typeof v === "string" ? v : "";
}
/**
 * Maps the diagnose pipeline handler's result → ExecutionSummaryData.
 * Returns null if the data doesn't contain the expected structure.
 */
export function toExecutionSummaryData(diagnoseResult) {
    const exec = diagnoseResult.execution;
    if (!exec)
        return null;
    const pipelineName = exec.pipeline?.name ?? exec.pipeline?.identifier ?? "Pipeline";
    const executionId = asStr(exec.execution?.id);
    const status = asStatus(exec.execution?.status);
    const totalDurationMs = asNum(exec.timing?.duration_ms);
    if (!executionId)
        return null;
    const rawStages = exec.stages;
    if (!Array.isArray(rawStages)) {
        return { pipelineName, executionId, status, totalDurationMs, stages: [] };
    }
    // Compute base start time from earliest stage
    let baseStartMs = Infinity;
    for (const s of rawStages) {
        if (s.started_at) {
            const t = new Date(s.started_at).getTime();
            if (t < baseStartMs)
                baseStartMs = t;
        }
    }
    if (!isFinite(baseStartMs))
        baseStartMs = 0;
    const stages = rawStages.map((s) => {
        const startMs = s.started_at ? new Date(s.started_at).getTime() : baseStartMs;
        const durationMs = asNum(s.duration_ms);
        const steps = (s.steps ?? []).map((step) => ({
            name: step.name ?? "Step",
            status: asStatus(step.status),
            durationMs: asNum(step.duration_ms),
            failureMessage: step.failure_message,
        }));
        return {
            name: s.name ?? s.identifier ?? "Stage",
            status: asStatus(s.status),
            startMs,
            durationMs,
            steps,
        };
    });
    return { pipelineName, executionId, status, totalDurationMs, stages };
}
/**
 * Maps the harness_status tool's output → ProjectHealthData.
 * Returns null if the data doesn't contain the expected structure.
 */
export function toProjectHealthData(statusResult, orgId, projectId) {
    const sr = statusResult;
    const summary = sr.summary;
    if (!summary)
        return null;
    const health = (summary.health === "healthy" || summary.health === "degraded" || summary.health === "failing")
        ? summary.health
        : "healthy";
    const recentExecutions = (sr.recent_activity ?? []).map((e) => ({
        id: asStr(e.execution_id),
        pipeline: asStr(e.pipeline),
        status: asStatus(e.status),
    }));
    return {
        orgId,
        projectId,
        health,
        counts: {
            failed: asNum(summary.total_failed),
            running: asNum(summary.total_running),
            recent: asNum(summary.total_recent),
        },
        recentExecutions,
    };
}
//# sourceMappingURL=mappers.js.map