export function formatBalance(amount: MaybeRefOrGetter<number | string>, locale: Intl.LocalesArgument = "en-US"): string {
  let value = toValue(amount);
  if (!value) {
    value = 0;
  }
  if (typeof value === "string" && Number.isNaN(Number(value))) {
    value = 0;
  }

  return value.toLocaleString(locale, { minimumFractionDigits: 0, maximumFractionDigits: 2 });
}

export function timeToLocal(originalTime: number) {
  const d = new Date(originalTime * 1000);
  return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()) / 1000;
}

export function formatAmountWithUnit(amount: MaybeRefOrGetter<number | string | null | undefined>): string {
  const value = toValue(amount);
  if (Number.isNaN(Number(value))) {
    return "0";
  }

  const num = Number(value);

  // 不超过1万，原样显示
  if (num < 1e4) {
    return String(num);
  }

  // 1亿以上，显示为xx亿
  if (num >= 1e8) {
    const yi = (num / 1e8).toFixed(1);
    return yi.endsWith(".0") ? `${Number.parseInt(yi)}亿` : `${yi}亿`;
  }

  // 1万到1亿，显示为xx万
  if (num >= 1e4) {
    const wan = (num / 1e4).toFixed(1);
    return wan.endsWith(".0") ? `${Number.parseInt(wan)}万` : `${wan}万`;
  }

  return String(num);
}

export function formatAmountWithSplit(amount: MaybeRefOrGetter<number | string | null | undefined>): string {
  const value = toValue(amount);
  if (Number.isNaN(Number(value))) {
    return "0";
  }

  const num = Number(value);

  return Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(num);
}
