'use strict';

"use strict";
function getAmountChinese(val) {
  if (!val && val !== 0) return "";
  const amount = +val;
  if (Number.isNaN(amount) || amount < 0) return "";
  const NUMBER = ["\u96F6", "\u58F9", "\u8D30", "\u53C1", "\u8086", "\u4F0D", "\u9646", "\u67D2", "\u634C", "\u7396"];
  const N_UNIT1 = ["", "\u62FE", "\u4F70", "\u4EDF"];
  const N_UNIT2 = ["", "\u4E07", "\u4EBF", "\u5146"];
  const D_UNIT = ["\u89D2", "\u5206", "\u5398", "\u6BEB"];
  const [integer, decimal] = amount.toString().split(".");
  if (integer && integer.length > 15) return "\u6570\u5B57\u8F83\u5927\u6EA2\u51FA";
  let res = "";
  if (integer) {
    for (let i = 0, len = integer.length; i < len; i++) {
      const num = integer.charAt(i);
      const pos = len - i - 1;
      if (num === "0") {
        if (i === len - 1) {
          if (integer.length === 1) res += "\u96F6";
          break;
        }
        if (integer.charAt(i + 1) === "0") continue;
      }
      res += NUMBER[num];
      if (parseInt(num)) res += N_UNIT1[pos % 4];
      if (pos % 4 === 0) res += N_UNIT2[Math.floor(pos / 4)];
    }
  }
  res += "\u5143";
  if (parseInt(decimal)) {
    for (let i = 0; i < 4; i++) {
      const num = decimal.charAt(i);
      if (parseInt(num)) res += NUMBER[num] + D_UNIT[i];
    }
  } else {
    res += "\u6574";
  }
  return res;
}
function thousandsFormat(num) {
  if (num === 0) return "0";
  if (!num) return "";
  const numArr = num.toString().split(".");
  numArr[0] = numArr[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  return numArr.join(".");
}
function getDateTimeUnit(format) {
  if (format == "YYYY") return "year";
  if (format == "YYYY-MM") return "month";
  if (format == "YYYY-MM-DD") return "day";
  if (format == "YYYY-MM-DD HH:mm") return "minute";
  return "day";
}
const mergeNumberOfExps = (expressions) => {
  const res = [];
  const isNumChar = (n) => /^[\d.]$/.test(n);
  for (let i = 0; i < expressions.length; i++) {
    if (i > 0 && isNumChar(expressions[i - 1]) && isNumChar(expressions[i])) {
      res[res.length - 1] += expressions[i];
      continue;
    }
    res.push(expressions[i]);
  }
  return res;
};
const toRPN = (exps) => {
  const s1 = [];
  const s2 = [];
  const getTopVal = (stack) => stack.length > 0 ? stack[stack.length - 1] : null;
  const levelCompare = (c1, c2) => {
    const getIndex = (c) => ["+-", "\xD7\xF7", "()"].findIndex((t) => t.includes(c));
    return getIndex(c1) - getIndex(c2);
  };
  exps.forEach((t) => {
    if (typeof t === "string" && Number.isNaN(Number(t))) {
      if (t === "(") {
        s1.push(t);
      } else if (t === ")") {
        let popVal;
        do {
          popVal = s1.pop();
          popVal !== "(" && s2.push(popVal);
        } while (s1.length && popVal !== "(");
      } else {
        let topVal = getTopVal(s1);
        if (!topVal) {
          s1.push(t);
        } else {
          while (topVal && topVal !== "(" && levelCompare(topVal, t) >= 0) {
            s2.push(s1.pop());
            topVal = getTopVal(s1);
          }
          s1.push(t);
        }
      }
      return;
    }
    s2.push(t);
  });
  while (s1.length) {
    s2.push(s1.pop());
  }
  return s2;
};
const calcRPN = (rpnExps) => {
  rpnExps = rpnExps.concat();
  const calc = (x, y, type) => {
    const a1 = Number(x), a2 = Number(y);
    switch (type) {
      case "+":
        return a1 + a2;
      case "-":
        return a1 - a2;
      case "\xD7":
        return a1 * a2;
      case "\xF7":
        return a1 / a2;
    }
  };
  for (let i = 2; i < rpnExps.length; i++) {
    if ("+-\xD7\xF7".includes(rpnExps[i])) {
      const val = calc(rpnExps[i - 2], rpnExps[i - 1], rpnExps[i]);
      rpnExps.splice(i - 2, 3, val);
      i = i - 2;
    }
  }
  return rpnExps[0];
};

exports.calcRPN = calcRPN;
exports.getAmountChinese = getAmountChinese;
exports.getDateTimeUnit = getDateTimeUnit;
exports.mergeNumberOfExps = mergeNumberOfExps;
exports.thousandsFormat = thousandsFormat;
exports.toRPN = toRPN;
//# sourceMappingURL=number.js.map
