// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
//
// SPDX-License-Identifier: GPL-2.0-or-later

export function formatDuration(milliseconds: number): string {
  const seconds = Math.floor(milliseconds / 1000);
  const days = Math.floor(seconds / 86400);
  const hours = Math.floor((seconds % 86400) / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const remainder = seconds % 60;
  const parts: string[] = [];
  if (days > 0) parts.push(`${days}d`);
  if (hours > 0 || days > 0) parts.push(`${hours}h`);
  parts.push(`${minutes}m`, `${remainder}s`);
  return parts.join(" ");
}

export function formatFrequency(frequencyHz: number): string {
  if (!Number.isFinite(frequencyHz)) return "--";
  if (frequencyHz >= 1_000_000_000) return `${(frequencyHz / 1_000_000_000).toFixed(3)} GHz`;
  if (frequencyHz >= 10_000_000) return `${(frequencyHz / 1_000_000).toFixed(3)} MHz`;
  return `${(frequencyHz / 1_000).toFixed(1)} kHz`;
}

export function formatFrequencyForStep(frequencyHz: number, stepHz: number): string {
  if (!Number.isFinite(frequencyHz)) return "--";
  if (stepHz >= 1_000_000) return (frequencyHz / 1_000_000).toFixed(6);
  if (stepHz >= 1_000) return (frequencyHz / 1_000).toFixed(3);
  if (stepHz >= 1) return String(Math.round(frequencyHz));
  return formatFrequency(frequencyHz);
}

export function formatFrequencyForHumans(frequencyHz: number): string {
  if (!Number.isFinite(frequencyHz)) return "--";
  if (frequencyHz >= 1_000_000_000) return `${(frequencyHz / 1_000_000_000).toFixed(3)} GHz`;
  if (frequencyHz >= 1_000_000) return `${(frequencyHz / 1_000_000).toFixed(3)} MHz`;
  if (frequencyHz >= 1_000) return `${(frequencyHz / 1_000).toFixed(3)} kHz`;
  return `${Math.round(frequencyHz)} Hz`;
}

export function formatWavelength(frequencyHz: number): string {
  if (!Number.isFinite(frequencyHz) || frequencyHz <= 0) return "--";
  const meters = 299_792_458 / frequencyHz;
  return meters >= 1 ? `${Math.round(meters)} m` : `${Math.round(meters * 100)} cm`;
}

export function parseFrequencyInput(
  value: string,
  defaultStepHz: number,
  mode: string,
): number | null {
  if (!value) return null;
  const match = /^([0-9]+(?:[.,][0-9]+)?)\s*([kmg]hz|[kmg]|hz)?$/
    .exec(value.trim().toLowerCase());
  if (!match?.[1]) return null;
  const rawNumber = match[1];
  let frequency = Number.parseFloat(rawNumber.replace(",", "."));
  const unit = match[2] ?? "";
  if (Number.isNaN(frequency)) return null;

  if (unit.startsWith("gh") || unit === "g") frequency *= 1_000_000_000;
  else if (unit.startsWith("mh") || unit === "m") frequency *= 1_000_000;
  else if (unit.startsWith("kh") || unit === "k") frequency *= 1_000;
  else if (!unit) {
    const hasDecimalSeparator = rawNumber.includes(".") || rawNumber.includes(",");
    if (mode.toUpperCase() === "WFM") {
      if (hasDecimalSeparator && frequency >= 50 && frequency < 200) {
        return Math.round(frequency * 1_000_000);
      }
      if (!hasDecimalSeparator && frequency >= 875 && frequency <= 1080) {
        return Math.round((frequency / 10) * 1_000_000);
      }
    }
    if (defaultStepHz >= 1_000_000) frequency *= 1_000_000;
    else if (defaultStepHz >= 1_000) frequency *= 1_000;
    else if (defaultStepHz < 1) {
      if (frequency < 1_000) frequency *= 1_000_000;
      else if (frequency < 1_000_000) frequency *= 1_000;
    }
  }
  return Math.round(frequency);
}

export function formatByteSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1_048_576) return `${(bytes / 1024).toFixed(1)} KB`;
  return `${(bytes / 1_048_576).toFixed(1)} MB`;
}
