"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getCouponDiscountDesc = exports.currencyToFont = exports.numberToFixed = exports.calcFloatCount = exports.formatCurrencyWithSymbol = exports.formatCurrency = exports.getPrecision = exports.getPricePrefix = exports.addSeparators = void 0;

var _divide = _interopRequireDefault(require("lodash/divide"));

var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));

var _commonUtil = require("./commonUtil");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

/* eslint-disable */
var precision = (0, _commonUtil.ensure)(function () {
  return window.ecommerce.precision;
}, '0'); // 千分位展示

var addSeparators = function addSeparators(nStr, thousandsSep, decimalSep) {
  if (!Number(nStr)) {
    return nStr;
  }

  var x1;
  var x2;
  nStr += '';
  var x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? decimalSep + x[1] : '';
  var rgx = /(\d+)(\d{3})/;

  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, "$1".concat(thousandsSep, "$2"));
  }

  return x1 + x2;
};

exports.addSeparators = addSeparators;

var getPricePrefix = function getPricePrefix() {
  var pricePrefix = (0, _commonUtil.ensure)(function () {
    return window.ecommerce.prefix;
  }, 'Rp');
  return pricePrefix;
};

exports.getPricePrefix = getPricePrefix;

var getPrecision = function getPrecision() {
  var ret = (0, _commonUtil.ensure)(function () {
    return window.ecommerce.precision;
  }, '0');
  return Number(ret);
}; // 格式化金额


exports.getPrecision = getPrecision;

var formatCurrency = function formatCurrency(num) {
  var displayPrecision = (0, _commonUtil.ensure)(function () {
    return window.ecommerce.displayCurrencyPrecision;
  }, '0');
  var precision = (0, _commonUtil.ensure)(function () {
    return window.ecommerce.precision;
  }, '0');
  var pricePrefix = (0, _commonUtil.ensure)(function () {
    return window.ecommerce.prefix;
  }, 'Rp');
  var numVal = num;

  if (Number.isNaN(num) || !Number.isFinite(num)) {
    numVal = 0;
  }

  var displayNum = displayPrecision != null && displayPrecision !== '' ? displayPrecision : precision;
  var ret = numVal / Math.pow(10, precision);
  return addSeparators(ret.toFixed(displayNum), pricePrefix === "IDR" || pricePrefix === "Rp" ? '.' : ',', '.');
};

exports.formatCurrency = formatCurrency;

var formatCurrencyWithSymbol = function formatCurrencyWithSymbol(num) {
  var ret = formatCurrency(num);
  return "".concat(getPricePrefix()).concat(ret);
};
/**
 * TODO 未兼容科学计数法（小数点后7位）
 * 计算浮点位数
 * @param {*} number 数字
 * @returns count
 */


exports.formatCurrencyWithSymbol = formatCurrencyWithSymbol;

var calcFloatCount = function calcFloatCount(number) {
  var numbString = String(number);
  var findIndex = numbString.indexOf('.');
  if (findIndex === -1) return 0;
  return numbString.length - findIndex - 1;
};
/**
 * 保留小数位
 * @param {number} number 数字
 * @param {int?} precision 位数 默认：2
 */


exports.calcFloatCount = calcFloatCount;

var numberToFixed = function numberToFixed(numb) {
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
  var multiple = Math.pow(10, precision);
  var floatCount = calcFloatCount(numb);

  if (floatCount <= precision) {
    if (floatCount === 0) {
      if (precision <= 0) {
        return "".concat(String(~~numb));
      }

      return "".concat(String(~~numb), ".").concat(new Array(precision - floatCount).fill('0').join(''));
    }

    return String(numb) + new Array(precision - floatCount).fill('0').join('');
  }

  var bigMultiple = multiple * 10;
  var tempNumb = ~~(numb * bigMultiple);
  var lastNumber = String(tempNumb).slice(-1);
  var resultNumber = ~~(numb * multiple);
  if (~~lastNumber >= 5) resultNumber += 1;
  var result = resultNumber / multiple;
  var resultFloatCount = calcFloatCount(result);
  return result + new Array(precision - resultFloatCount).fill('0').join('');
};
/**
 * 格式化数值,不加前缀 (前台使用) 例如:缩小100倍
 * @param {*} value 数值
 * @param {*} customPrecision 精度转换,默认读配置项
 * @param {*} customDisplayPrecision 保留小数位数,默认读配置项(传null,表示不作处理只转换精度)
 * @returns 格式化后的数值字符串
 */


exports.numberToFixed = numberToFixed;

var currencyToFont = function currencyToFont(value, customPrecision, customDisplayPrecision) {
  if (value === null || value === undefined || value === '' || Number.isNaN(Number(value))) {
    return value;
  }

  var precision; // 优先使用参数上的精度

  if (!(0, _isEmpty["default"])(customPrecision) && !Number.isNaN(Number(customPrecision))) {
    precision = Number(customPrecision);
  } else {
    precision = (0, _commonUtil.ensure)(function () {
      return window.ecommerce.precision;
    }, '0');
  }

  var number = Number(value);
  var temp = (0, _divide["default"])(number, Math.pow(10, precision)); // 特殊处理,不需要格式化小数位

  if (customDisplayPrecision === null) {
    return "".concat(temp);
  } // 精度格式化


  var displayPrecision; // 优先使用参数上的精度

  if (!(0, _isEmpty["default"])(customDisplayPrecision) && !Number.isNaN(Number(customDisplayPrecision))) {
    displayPrecision = Number(customDisplayPrecision);
  } else {
    displayPrecision = (0, _commonUtil.ensure)(function () {
      return window.ecommerce.displayCurrencyPrecision;
    }, 'Rp');
  }

  return temp.toFixed(Number(displayPrecision));
}; // 获取优惠券折扣力度描述


exports.currencyToFont = currencyToFont;

var getCouponDiscountDesc = function getCouponDiscountDesc(coupon) {
  if (coupon.thAmount) {
    return coupon.discountMethod === 'AMT' ? "Over ".concat(currencyToFont(coupon.thAmount), " Minus ").concat(currencyToFont(coupon.discountAmount)) : "".concat(Number(coupon.discountAmount) / 100, "% off ").concat(currencyToFont(coupon.thAmount));
  }

  return coupon.discountMethod === 'AMT' ? "Minus ".concat(currencyToFont(coupon.discountAmount)) : "".concat(Number(coupon.discountAmount) / 100, "% off");
};

exports.getCouponDiscountDesc = getCouponDiscountDesc;