import type { VarCounts } from "./indicatorKpis.js";

export function applyCompletionAttendanceThreshold(
  completionCounts: VarCounts,
  attendancePresentCounts: VarCounts,
  attendancePossibleCounts: VarCounts,
  thresholdPct: number | null,
): VarCounts {
  if (thresholdPct == null || !Number.isFinite(thresholdPct)) return completionCounts;
  const byTp = new Map<string, number>();
  let project = 0;
  for (const [tpId, completion] of completionCounts.byTp.entries()) {
    const present = attendancePresentCounts.byTp.get(tpId) ?? 0;
    const possible = attendancePossibleCounts.byTp.get(tpId) ?? 0;
    const attendancePct = possible > 0 ? (present / possible) * 100 : 0;
    const gated = attendancePct >= thresholdPct ? completion : 0;
    byTp.set(tpId, gated);
    project += gated;
  }
  return { project, byTp };
}

