/**
 * Unit names as one short label: consecutive numbers as a range, the rest as
 * they are, in natural order — "614", "617–622", "628–629, 631", "5 樓 A 室, 5 樓 B 室".
 */
export function unitsLabel(names: readonly string[]): string {
  const sorted = [...names].sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
  const isNumber = (s: string) => /^\d+$/.test(s);
  const out: string[] = [];
  for (let i = 0; i < sorted.length; ) {
    let j = i;
    while (j + 1 < sorted.length && isNumber(sorted[j]) && isNumber(sorted[j + 1]) && Number(sorted[j + 1]) === Number(sorted[j]) + 1) j++;
    out.push(j > i ? `${sorted[i]}–${sorted[j]}` : sorted[i]);
    i = j + 1;
  }
  return out.join(', ');
}
