/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var isUndefined = require('lodash/isUndefined');
var isNil = require('lodash/isNil');
var isString = require('lodash/isString');
var isNumber = require('lodash/isNumber');
var _common_js_log_log = require('../log/log.js');
var _common_js_inputNumber_largeNumber = require('./large-number.js');
require('@babel/runtime/helpers/slicedToArray');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var isUndefined__default = /*#__PURE__*/_interopDefaultLegacy(isUndefined);
var isNil__default = /*#__PURE__*/_interopDefaultLegacy(isNil);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);

function canAddNumber(num, max) {
  var largeNumber = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  if (!num && num !== 0) return true;
  if (largeNumber && isString__default["default"](num)) return _common_js_inputNumber_largeNumber.compareNumber(num, max, largeNumber) < 0;
  return num < max;
}
function canReduceNumber(num, min) {
  var largeNumber = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  if (!num && num !== 0) return true;
  if (largeNumber && isString__default["default"](num)) return _common_js_inputNumber_largeNumber.compareNumber(num, min, largeNumber) > 0;
  return num > min;
}
function formatToNumber(num, extra) {
  if (isNil__default["default"](num) || num === "") return num;
  if (num === "-") return 0;
  if (num[num.length - 1] === ".") return extra !== null && extra !== void 0 && extra.largeNumber ? num.slice(0, -1) : Number(num.slice(0, -1));
  var isLargeNumber = (extra === null || extra === void 0 ? void 0 : extra.largeNumber) && isString__default["default"](num);
  var newNumber = num;
  if (isString__default["default"](num) && num.includes("e") || isNumber__default["default"](num)) newNumber = isLargeNumber ? _common_js_inputNumber_largeNumber.formatENumber(num) : Number(num);
  if (!isUndefined__default["default"](extra === null || extra === void 0 ? void 0 : extra.decimalPlaces)) {
    newNumber = _common_js_inputNumber_largeNumber.largeNumberToFixed(newNumber, extra.decimalPlaces, extra.largeNumber);
  }
  var val = isLargeNumber || !isUndefined__default["default"](extra === null || extra === void 0 ? void 0 : extra.decimalPlaces) ? newNumber : Number(newNumber);
  if (String(val) === "NaN") return void 0;
  return val;
}
function putInRangeNumber(val, params) {
  if (val === "") return void 0;
  var max = params.max,
    min = params.min,
    lastValue = params.lastValue,
    largeNumber = params.largeNumber;
  if (!_common_js_inputNumber_largeNumber.isInputNumber(val)) return lastValue;
  if (largeNumber && (isString__default["default"](max) || max === Infinity) && (isString__default["default"](min) || min === -Infinity)) {
    if (_common_js_inputNumber_largeNumber.compareNumber(max, val, largeNumber) < 0) return max;
    if (_common_js_inputNumber_largeNumber.compareNumber(min, val, largeNumber) > 0) return min;
    return val;
  }
  return Math.max(Number(min), Math.min(Number(max), Number(val)));
}
function positiveAdd(num1, num2) {
  var _num1$toString$split$, _num2$toString$split$;
  if (!num1 || !num2) return (num1 || 0) + (num2 || 0);
  var r1 = ((_num1$toString$split$ = num1.toString().split(".")[1]) === null || _num1$toString$split$ === void 0 ? void 0 : _num1$toString$split$.length) || 0;
  var r2 = ((_num2$toString$split$ = num2.toString().split(".")[1]) === null || _num2$toString$split$ === void 0 ? void 0 : _num2$toString$split$.length) || 0;
  if (!r1 && !r2) return num1 + num2;
  var newNumber1 = num1;
  var newNumber2 = num2;
  var diff = Math.abs(r1 - r2);
  var digit = Math.pow(10, Math.max(r1, r2));
  if (diff > 0) {
    var cm = Math.pow(10, diff);
    if (r1 > r2) {
      newNumber1 = Number(num1.toString().replace(".", ""));
      newNumber2 = Number(num2.toString().replace(".", "")) * cm;
    } else {
      newNumber1 = Number(num1.toString().replace(".", "")) * cm;
      newNumber2 = Number(num2.toString().replace(".", ""));
    }
  } else {
    newNumber1 = Number(num1.toString().replace(".", ""));
    newNumber2 = Number(num2.toString().replace(".", ""));
  }
  return (newNumber1 + newNumber2) / digit;
}
function positiveSubtract(num1, num2) {
  var _num1$toString$split$2, _num2$toString$split$2;
  if (!num1 || !num2) return (num1 || 0) - (num2 || 0);
  var r1 = ((_num1$toString$split$2 = num1.toString().split(".")[1]) === null || _num1$toString$split$2 === void 0 ? void 0 : _num1$toString$split$2.length) || 0;
  var r2 = ((_num2$toString$split$2 = num2.toString().split(".")[1]) === null || _num2$toString$split$2 === void 0 ? void 0 : _num2$toString$split$2.length) || 0;
  var digit = Math.pow(10, Math.max(r1, r2));
  var n = r1 >= r2 ? r1 : r2;
  return Number(((num1 * digit - num2 * digit) / digit).toFixed(n));
}
function add(num1, num2) {
  if (num1 < 0 && num2 > 0) return positiveSubtract(num2, Math.abs(num1));
  if (num1 < 0 && num2 < 0) return positiveAdd(Math.abs(num1), Math.abs(num2)) * -1;
  if (num1 > 0 && num2 < 0) return positiveSubtract(num1, Math.abs(num2));
  return positiveAdd(num1, num2);
}
function subtract(num1, num2) {
  if (num1 < 0 && num2 > 0) return positiveAdd(Math.abs(num1), num2) * -1;
  if (num1 < 0 && num2 < 0) return positiveSubtract(Math.abs(num2), Math.abs(num1));
  if (num1 > 0 && num2 < 0) return positiveAdd(num1, Math.abs(num2));
  return positiveSubtract(num1, num2);
}
function getStepValue(p) {
  var op = p.op,
    step = p.step,
    lastValue = p.lastValue,
    max = p.max,
    min = p.min,
    largeNumber = p.largeNumber;
  if (step <= 0) {
    _common_js_log_log["default"].error("InputNumber", "step must be larger than 0.");
    return lastValue;
  }
  var tStep = isNumber__default["default"](step) ? String(step) : step;
  var newVal;
  if (op === "add") {
    if (largeNumber && isString__default["default"](lastValue)) newVal = _common_js_inputNumber_largeNumber.largeNumberAdd(String(lastValue), String(tStep));else newVal = add(Number(lastValue || 0), Number(step));
  } else if (op === "reduce") {
    if (largeNumber && isString__default["default"](lastValue)) newVal = _common_js_inputNumber_largeNumber.largeNumberSubtract(String(lastValue), String(tStep));else newVal = subtract(Number(lastValue || 0), Number(step));
  }
  if (isUndefined__default["default"](lastValue)) newVal = putInRangeNumber(newVal, {
    max: max,
    min: min,
    lastValue: lastValue,
    largeNumber: largeNumber
  });
  return largeNumber ? newVal : Number(newVal);
}
function getMaxOrMinValidateResult(p) {
  var largeNumber = p.largeNumber,
    value = p.value,
    max = p.max,
    min = p.min;
  if (isUndefined__default["default"](largeNumber)) return void 0;
  if (largeNumber && isNumber__default["default"](value)) _common_js_log_log["default"].warn("InputNumber", "largeNumber value must be a string.");
  var error;
  if (_common_js_inputNumber_largeNumber.compareNumber(value, max, largeNumber) > 0) error = "exceed-maximum";else if (_common_js_inputNumber_largeNumber.compareNumber(value, min, largeNumber) < 0) error = "below-minimum";else error = void 0;
  return error;
}
var specialCode = ["-", ".", "e", "E"];
function canInputNumber(number, largeNumber) {
  var _String$match, _String$match2;
  if (!number && isString__default["default"](number)) return true;
  var isNumber2 = largeNumber && _common_js_inputNumber_largeNumber.isInputNumber(number) || !Number.isNaN(Number(number));
  if (!isNumber2 && !["-", ".", "e", "E"].includes(number.slice(-1))) return false;
  if (((_String$match = String(number).match(/\./g)) === null || _String$match === void 0 ? void 0 : _String$match.length) > 1) return false;
  if (((_String$match2 = String(number).match(/-/g)) === null || _String$match2 === void 0 ? void 0 : _String$match2.length) > 1) return false;
  return true;
}
function formatThousandths(number) {
  var thousandthsRegExp = /^[-+]?\d{1,3}(,\d{3})*(\.(\d*))?$/;
  if (thousandthsRegExp.test(number)) return number.replace(/,/g, "");
  return number;
}

exports.compareLargeNumber = _common_js_inputNumber_largeNumber.compareLargeNumber;
exports.compareNumber = _common_js_inputNumber_largeNumber.compareNumber;
exports.fillZero = _common_js_inputNumber_largeNumber.fillZero;
exports.formatENumber = _common_js_inputNumber_largeNumber.formatENumber;
exports.isInfinity = _common_js_inputNumber_largeNumber.isInfinity;
exports.isInputNumber = _common_js_inputNumber_largeNumber.isInputNumber;
exports.isSafeNumber = _common_js_inputNumber_largeNumber.isSafeNumber;
exports.largeIntNumberAdd = _common_js_inputNumber_largeNumber.largeIntNumberAdd;
exports.largeIntegerNumberSubtract = _common_js_inputNumber_largeNumber.largeIntegerNumberSubtract;
exports.largeNumberAdd = _common_js_inputNumber_largeNumber.largeNumberAdd;
exports.largeNumberSubtract = _common_js_inputNumber_largeNumber.largeNumberSubtract;
exports.largeNumberToFixed = _common_js_inputNumber_largeNumber.largeNumberToFixed;
exports.largePositiveNumberAdd = _common_js_inputNumber_largeNumber.largePositiveNumberAdd;
exports.largePositiveNumberSubtract = _common_js_inputNumber_largeNumber.largePositiveNumberSubtract;
exports.removeInvalidZero = _common_js_inputNumber_largeNumber.removeInvalidZero;
exports.add = add;
exports.canAddNumber = canAddNumber;
exports.canInputNumber = canInputNumber;
exports.canReduceNumber = canReduceNumber;
exports.formatThousandths = formatThousandths;
exports.formatToNumber = formatToNumber;
exports.getMaxOrMinValidateResult = getMaxOrMinValidateResult;
exports.getStepValue = getStepValue;
exports.positiveAdd = positiveAdd;
exports.positiveSubtract = positiveSubtract;
exports.putInRangeNumber = putInRangeNumber;
exports.specialCode = specialCode;
exports.subtract = subtract;
//# sourceMappingURL=number.js.map
