/** 价格按交易所常见精度：永续/指数 0.01；期权权利金 0.1 */

export type PxKind = "perp" | "option" | "index";

export function fmtExPx(
  kind: PxKind,
  px: number | null | undefined,
): string {
  if (px == null || Number.isNaN(Number(px))) return "—";
  const n = Number(px);
  if (kind === "option") {
    return (Math.round(n * 10) / 10).toFixed(1);
  }
  return (Math.round(n * 100) / 100).toFixed(2);
}

export function fmtTopEx(
  kind: PxKind,
  px: number | null | undefined,
  sz: number | null | undefined,
): string {
  if (px == null || Number.isNaN(Number(px))) return "—";
  const p = fmtExPx(kind, px);
  if (sz == null || Number.isNaN(Number(sz))) return p;
  const n = Number(sz);
  const s =
    n >= 100 ? n.toFixed(0) : n >= 10 ? n.toFixed(1) : n.toFixed(2);
  return `${p}(${s})`;
}

export function fmtBidLiqEx(
  kind: PxKind,
  px: number | null | undefined,
  sz: number | null | undefined,
): string {
  if (px == null || Number.isNaN(Number(px))) return "—";
  const p = fmtExPx(kind, px);
  if (sz == null || Number.isNaN(Number(sz))) return p;
  const n = Number(sz);
  const s =
    n >= 100 ? n.toFixed(0) : n >= 10 ? n.toFixed(1) : n.toFixed(2);
  return `${p}/${s}`;
}
