import type { MeasurementType, WorkoutSet } from "../../types";

export function formatDuration(seconds?: number | null) {
  const value = seconds ?? 0;
  if (value < 60) return `${value} сек`;
  const minutes = Math.floor(value / 60);
  const rest = value % 60;
  return rest ? `${minutes} мин ${rest} сек` : `${minutes} мин`;
}

export function formatSet(set: WorkoutSet, measurementType: MeasurementType) {
  if (measurementType === "reps_only") return `${set.reps} повторов`;
  if (measurementType === "duration") return formatDuration(set.duration_seconds);
  if (measurementType === "distance_duration") return `${set.distance_km ?? 0} км · ${formatDuration(set.duration_seconds)}`;
  if (measurementType === "duration_calories") return `${formatDuration(set.duration_seconds)} · ${Math.round(set.calories ?? 0)} ккал`;
  return `${set.weight} кг × ${set.reps}`;
}

export function volumeForSet(set: WorkoutSet, measurementType: MeasurementType) {
  if (measurementType === "distance_duration") return set.distance_km ?? 0;
  if (measurementType === "duration" || measurementType === "duration_calories") return set.duration_seconds ?? 0;
  return set.weight * set.reps;
}
