<script setup>
import { computed } from "vue";
import { JUSTIFY_CLASS } from "../../../utils/runtime";
defineOptions({ name: "ShwfedTableNumberRendererRuntime" });
const props = defineProps({
  column: { type: null, required: true },
  ctx: { type: Object, required: true }
});
const CHINESE_DIGITS = ["\u96F6", "\u58F9", "\u8D30", "\u53C1", "\u8086", "\u4F0D", "\u9646", "\u67D2", "\u634C", "\u7396"];
const CHINESE_UNITS = ["", "\u62FE", "\u4F70", "\u4EDF"];
const CHINESE_GROUP_UNITS = ["", "\u4E07", "\u4EBF", "\u5146"];
function toChineseUppercase(n) {
  if (Number.isNaN(n)) return "-";
  if (n === 0) return "\u96F6";
  const isNegative = n < 0;
  const absN = Math.abs(n);
  const [intPart, decPart] = String(absN).split(".");
  let result = "";
  const digits = intPart.split("").reverse();
  let zeroFlag = false;
  for (let i = 0; i < digits.length; i++) {
    const d = Number(digits[i]);
    const groupIndex = Math.floor(i / 4);
    const posInGroup = i % 4;
    if (d === 0) {
      zeroFlag = true;
    } else {
      if (zeroFlag) {
        result = CHINESE_DIGITS[0] + result;
        zeroFlag = false;
      }
      result = CHINESE_DIGITS[d] + CHINESE_UNITS[posInGroup] + result;
    }
    if (posInGroup === 0 && groupIndex > 0 && !zeroFlag) {
      result = (CHINESE_GROUP_UNITS[groupIndex] ?? "") + result;
    }
  }
  if (decPart) {
    result += "\u70B9";
    for (const ch of decPart) {
      result += CHINESE_DIGITS[Number(ch)];
    }
  }
  return isNegative ? `\u8D1F${result}` : result;
}
function formatNumber(value, column) {
  if (value === void 0 || value === null) return "-";
  const n = Number(value);
  if (Number.isNaN(n)) return "-";
  const displayMode = column.displayMode ?? "plain";
  if (displayMode === "uppercaseChinese") {
    return toChineseUppercase(n);
  }
  let result;
  if (column.preserveDigits !== void 0) {
    const mode = column.preserveMode ?? "round";
    const digits = column.preserveDigits;
    const factor = 10 ** digits;
    let processed;
    switch (mode) {
      case "floor":
        processed = Math.floor(n * factor) / factor;
        break;
      case "ceil":
        processed = Math.ceil(n * factor) / factor;
        break;
      default:
        processed = Math.round(n * factor) / factor;
    }
    result = processed.toFixed(digits);
  } else {
    result = String(n);
  }
  if (displayMode === "monoThousand") {
    const [integer, decimal] = result.split(".");
    const formattedInt = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    result = decimal !== void 0 ? `${formattedInt}.${decimal}` : formattedInt;
  }
  return result;
}
const formatted = computed(() => formatNumber(props.ctx.cell.getValue(), props.column));
const isNan = computed(() => formatted.value === "-");
const align = computed(() => isNan.value ? "center" : props.column.align ?? "right");
const justifyClass = computed(() => JUSTIFY_CLASS[align.value] ?? JUSTIFY_CLASS.right);
</script>

<template>
  <div
    :class="[
  'relative w-full py-2 px-1 flex items-center text-xs',
  justifyClass,
  column.displayMode === 'monoThousand' && 'font-mono',
  isNan ? 'text-zinc-300 select-none' : 'text-zinc-600'
]"
  >
    {{ formatted }}
  </div>
</template>
