export const FLOW_DISPLAY_UNIT = "m³/h";
const M3H_FACTOR = 3600;
const LPS_FLOW_PROPERTIES = new Set(["flow", "actual_demand", "actualdemand"]);

export const isLpsFlowProperty = (property: string) =>
  LPS_FLOW_PROPERTIES.has(property);

export const toM3h = (value: number, sourceUnit: string = "m³/s") => {
  if (!Number.isFinite(value)) return Number.NaN;
  const normalizedUnit = sourceUnit.trim().toLowerCase();
  if (normalizedUnit === "m³/h") return value;
  if (normalizedUnit === "lps" || normalizedUnit === "l/s") return value * 3.6;
  if (normalizedUnit === "m³/s") return value * M3H_FACTOR;
  return value * M3H_FACTOR;
};

export const toM3s = (value: number, sourceUnit: string = "m³/h") => {
  if (!Number.isFinite(value)) return Number.NaN;
  const normalizedUnit = sourceUnit.trim().toLowerCase();
  if (normalizedUnit === "m³/s") return value;
  if (normalizedUnit === "lps" || normalizedUnit === "l/s") return value / 1000;
  if (normalizedUnit === "m³/h") return value / M3H_FACTOR;
  return value / M3H_FACTOR;
};

export const toLps = (value: number, sourceUnit: string = "m³/s") => {
  if (!Number.isFinite(value)) return Number.NaN;
  const normalizedUnit = sourceUnit.trim().toLowerCase();
  if (normalizedUnit === "lps" || normalizedUnit === "l/s") return value;
  if (normalizedUnit === "m³/h") return value / 3.6;
  if (normalizedUnit === "m³/s") return value * 1000;
  return value * 1000;
};
