import { inToCm, toAxelWholeInches } from "@/modules/quote/unit-converter";

export type MotherShipDimUnit = "in" | "cm";

/** 从 placeholder/label 推断尺寸字段单位 */
export function inferDimUnitFromFieldHint(hint: string): MotherShipDimUnit {
  const text = hint.trim();
  if (!text) {
    return "in";
  }
  const hasCm = /(?:\bcm\b|厘米|公分)/i.test(text);
  const hasIn = /(?:\bin\b|英寸|inch|inches)/i.test(text);
  if (hasCm && !hasIn) {
    return "cm";
  }
  return "in";
}

/** 内部英寸 → MotherShip 页面实际单位填入值 */
export function buildDimDisplayForUnit(
  inches: number,
  unit: MotherShipDimUnit,
): string {
  if (unit === "cm") {
    return formatDimDisplay(inToCm(inches));
  }
  return String(toAxelWholeInches(inches));
}

function formatDimDisplay(value: number): string {
  const rounded = Math.round(value * 100) / 100;
  return String(rounded);
}
