export function formatNano(nano?: number) {
  if (!nano) return "尚未設定";
  return new Date(Math.floor(nano / 1_000_000)).toLocaleString("zh-TW", {
    hour12: false,
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit"
  });
}

export function inputDateTimeToNano(value: string) {
  if (!value) return undefined;
  const ms = new Date(value).getTime();
  if (Number.isNaN(ms)) return undefined;
  return ms * 1_000_000;
}

export function nanoToInputDateTime(nano?: number) {
  if (!nano) return "";
  const date = new Date(Math.floor(nano / 1_000_000));
  const offset = date.getTimezoneOffset() * 60_000;
  return new Date(date.getTime() - offset).toISOString().slice(0, 16);
}

export function shortText(text?: string, size = 90) {
  if (!text) return "";
  return text.length > size ? `${text.slice(0, size)}...` : text;
}
