const clamp = (value: number, min: number, max: number) =>
  Math.min(max, Math.max(min, value));

/**
 * Normalize a probability-like input to a percent scale (0..100).
 * Accepts values like "70", "70%", 70, or 0.7.
 */
export const normalizeProbabilityPercent = (value: unknown): number => {
  const raw =
    typeof value === "string"
      ? Number.parseFloat(value.replace(/%/g, "").trim())
      : Number(value);

  if (!Number.isFinite(raw)) return 0;

  const scaled = raw <= 1 ? raw * 100 : raw;
  return clamp(scaled, 0, 100);
};

/**
 * Normalize a probability-like input to a ratio scale (0..1).
 */
export const normalizeProbabilityRatio = (value: unknown): number =>
  normalizeProbabilityPercent(value) / 100;
