import hooks from "../../node_modules/moment/dist/moment.mjs";
import { parseDateFormatter } from "./dateUtil.mjs";
import XEUtils from "xe-utils";
const selectFormatter = (value, options = []) => {
  const item = options.find((item2) => item2.value === value);
  return item ? item.label : value;
};
const multipleFormatter = (value, options = []) => {
  const values = value == null ? void 0 : value.split("|");
  const labels = [];
  values == null ? void 0 : values.forEach((val) => {
    const option = options.find((opt) => opt.value == val);
    if (option) {
      labels.push(option.label);
    }
  });
  return labels.join(", ");
};
const cascadeFormatter = (value, options = []) => {
  const values = value == null ? void 0 : value.split("/");
  const labels = [];
  let currentOptions = options;
  for (let i = 0; i < (values == null ? void 0 : values.length); i++) {
    const currentValue = values[i];
    const option = currentOptions.find((opt) => opt.value === currentValue);
    if (option) {
      labels.push(option.label);
      if (option.children && i < values.length - 1) {
        currentOptions = option.children;
      }
    } else {
      break;
    }
  }
  return labels.join("/");
};
const dateFormatter = (value, dateType, formatterType) => {
  let dateValue;
  debugger;
  if (!isNaN(value) && (value + "").length == 13) {
    dateValue = hooks(Number(value));
  } else {
    dateValue = hooks(value, "YYYYMMDD");
  }
  const textFormat = parseDateFormatter(formatterType, dateType);
  return dateValue.format(textFormat);
};
const textFormatter = (value, dataFormatter) => {
  switch (dataFormatter) {
    case "money(yuan)":
    case "money(wan)":
    case "money(yi)":
      return moneyFormatter(value, dataFormatter);
    case "bankCard":
      value = XEUtils.toValueString(value);
      value = value.replace(/\s+/g, "");
      return XEUtils.commafy(value, {
        spaceNumber: 4,
        separator: " "
      });
    case "percent":
      value = XEUtils.multiply(XEUtils.toNumber(value), 100);
      return value;
    case "thousand":
      value = XEUtils.multiply(XEUtils.toNumber(value), 1e3);
      return value;
    case "tenThousand":
      value = XEUtils.multiply(XEUtils.toNumber(value), 1e4);
      return value;
    default:
      return value;
  }
};
const moneyFormatter = (value, dataFormatter) => {
  let moneyValue;
  switch (dataFormatter) {
    case "money(yuan)":
      moneyValue = XEUtils.toNumber(value);
      break;
    case "money(wan)":
      moneyValue = XEUtils.divide(XEUtils.toNumber(value), 1e4);
      break;
    case "money(yi)":
      moneyValue = XEUtils.divide(XEUtils.toNumber(value), 1e8);
      break;
    default:
      moneyValue = XEUtils.toNumber(value);
      break;
  }
  return XEUtils.commafy(moneyValue, { digits: 2 });
};
const textFormatter4Input = (value, dataFormatter) => {
  if (!value) {
    return value;
  }
  switch (dataFormatter) {
    case "money(yuan)":
    case "money(wan)":
    case "money(yi)":
      return XEUtils.toNumberString(XEUtils.toNumber(value)).replace(
        /\B(?=(\d{3})+(?!\d))/g,
        ","
      );
    case "bankCard":
      return XEUtils.toValueString(value.replace(/\s+/g, "")).replace(
        /\B(?=(\d{4})+(?!\d))/g,
        " "
      );
    default:
      return value;
  }
};
const reverseTextFormatter = (value, dataFormatter) => {
  if (!value) {
    return value;
  }
  switch (dataFormatter) {
    case "money(yuan)":
    case "money(wan)":
    case "money(yi)":
      return reverseMoneyFormatter(value, dataFormatter);
    case "bankCard":
      return value.replace(/\s+/g, "");
    case "percent":
      value = XEUtils.divide(XEUtils.toNumber(value), 100);
      return value;
    case "thousand":
      value = XEUtils.divide(XEUtils.toNumber(value), 1e3);
      return value;
    case "tenThousand":
      value = XEUtils.divide(XEUtils.toNumber(value), 1e4);
      return value;
    default:
      return value;
  }
};
const reverseMoneyFormatter = (value, dataFormatter) => {
  let moneyValue;
  value = value.replace(/,/gi, "");
  switch (dataFormatter) {
    case "money(yuan)":
      moneyValue = XEUtils.toNumber(value);
      break;
    case "money(wan)":
      moneyValue = XEUtils.multiply(XEUtils.toNumber(value), 1e4);
      break;
    case "money(yi)":
      moneyValue = XEUtils.multiply(XEUtils.toNumber(value), 1e8);
      break;
    default:
      moneyValue = XEUtils.toNumber(value);
      break;
  }
  return XEUtils.toNumberString(moneyValue);
};
const getUnit = (dataFormatter) => {
  switch (dataFormatter) {
    case "money(yuan)":
      return "元";
    case "money(wan)":
      return "万";
    case "money(yi)":
      return "亿";
    case "percent":
      return "%";
    case "thousand":
      return "‰";
    case "tenThousand":
      return "‱";
    default:
      return "";
  }
};
export {
  cascadeFormatter,
  dateFormatter,
  getUnit,
  moneyFormatter,
  multipleFormatter,
  reverseMoneyFormatter,
  reverseTextFormatter,
  selectFormatter,
  textFormatter,
  textFormatter4Input
};
