/**
 * computeRoi — pure client-side ROI computation from a set of Outcome rows.
 *
 * No network calls. Safe to run in browser or server contexts.
 * All monetary values are integer cents.
 */
/**
 * Compute ROI metrics from a set of Outcome rows.
 *
 * Cost inference:
 * - If `costsCentsByOutcomeId` is provided: cost = map lookup (0 if absent).
 * - Otherwise: cost = abs(value_cents) for rows where value_cents < 0.
 *   Positive value_cents contribute to totalValueCents.
 *
 * Divide-by-zero is handled: ratios are null when totalCostCents == 0.
 */
export function computeRoi(input) {
    const { outcomes, costsCentsByOutcomeId } = input;
    let totalValueCents = 0;
    let totalCostCents = 0;
    let pendingCount = 0;
    for (const outcome of outcomes) {
        if (outcome.is_pending_backfill) {
            pendingCount++;
        }
        if (costsCentsByOutcomeId !== undefined) {
            // External cost map mode: value and cost are tracked independently.
            // value_cents may still be signed; positive rows contribute value.
            if (outcome.value_cents > 0) {
                totalValueCents += outcome.value_cents;
            }
            const externalCost = costsCentsByOutcomeId.get(outcome.id) ?? 0;
            totalCostCents += externalCost;
        }
        else {
            // Inferred mode: sign of value_cents determines value vs cost.
            if (outcome.value_cents >= 0) {
                totalValueCents += outcome.value_cents;
            }
            else {
                totalCostCents += Math.abs(outcome.value_cents);
            }
        }
    }
    const valueToCostRatio = totalCostCents === 0 ? null : totalValueCents / totalCostCents;
    const netRoiPct = totalCostCents === 0
        ? null
        : ((totalValueCents - totalCostCents) / totalCostCents) * 100;
    return {
        totalValueCents,
        totalCostCents,
        valueToCostRatio,
        netRoiPct,
        outcomeCount: outcomes.length,
        pendingCount,
    };
}
//# sourceMappingURL=compute-roi.js.map