"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.MIN_SAFE_INTEGER = exports.MAX_SAFE_INTEGER = void 0;
exports.getDateFormatByField = getDateFormatByField;
exports.getDateFormatByFieldType = getDateFormatByFieldType;
exports.getGlobalConfig = getGlobalConfig;
exports.getNearStepMomentValues = getNearStepMomentValues;
exports.getNearStepValues = getNearStepValues;
exports.getNumberFormatOptions = getNumberFormatOptions;
exports.isEmpty = isEmpty;
exports.isSame = isSame;
exports.isSameLike = isSameLike;
exports.normalizeLanguage = normalizeLanguage;
exports.parseBigNumber = parseBigNumber;
exports.parseNumber = parseNumber;
exports.toRangeValue = toRangeValue;
exports.warning = warning;

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _moment = _interopRequireDefault(require("moment"));

var _mobx = require("mobx");

var _warning = _interopRequireDefault(require("warning"));

var _isNumber = _interopRequireDefault(require("lodash/isNumber"));

var _defaultTo = _interopRequireDefault(require("lodash/defaultTo"));

var _isObject = _interopRequireDefault(require("lodash/isObject"));

var _isNil = _interopRequireDefault(require("lodash/isNil"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

var _isString = _interopRequireDefault(require("lodash/isString"));

var _isBoolean = _interopRequireDefault(require("lodash/isBoolean"));

var _bignumber = require("bignumber.js");

var _enum = require("../enum");

var _enum2 = require("../data-set/enum");

var _configure = require("../configure");

var _formatter = require("../formatter");

var _math = _interopRequireDefault(require("../math"));

var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
exports.MAX_SAFE_INTEGER = MAX_SAFE_INTEGER;
var MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || 1 - Math.pow(2, 53);
exports.MIN_SAFE_INTEGER = MIN_SAFE_INTEGER;
var warned = {};

function warning(valid, message) {
  if (!valid && !warned[message]) {
    (0, _warning["default"])(false, message);
    warned[message] = true;
  }
}

function isEmpty(value) {
  return (0, _isNil["default"])(value) || value === '';
}

function isSame(newValue, oldValue) {
  if (newValue === oldValue || isEmpty(newValue) && isEmpty(oldValue)) {
    return true;
  }

  if (_math["default"].isBigNumber(newValue) || _math["default"].isBigNumber(oldValue)) {
    return _math["default"].eq(newValue, oldValue);
  }

  return (0, _isEqual["default"])(newValue, oldValue);
}

function isSameLike(newValue, oldValue) {
  /* eslint-disable-next-line */
  return isSame(newValue, oldValue) || newValue == oldValue;
}

function parseNumber(value, precision, strict) {
  if ((0, _isBoolean["default"])(value)) {
    var _result = Number(value);

    return strict ? new _bignumber.BigNumber(_result) : _result;
  }

  var result = new _bignumber.BigNumber((0, _isNumber["default"])(precision) ? _math["default"].toFixed(value, precision) : value);
  return strict ? result : _math["default"].fix(result);
}

function parseBigNumber(value, precision) {
  return new _bignumber.BigNumber((0, _isNumber["default"])(precision) ? _math["default"].toFixed(value, precision) : value);
}

function getBeforeStepValue(value, min, step, options) {
  return _math["default"].minus(value, _math["default"].mod(_math["default"].minus(value, min, options), step, options), options);
}

function getNearStepMoments(value, step, unit) {
  var unitValue = value.get(unit);
  var mod = unitValue % step;

  if (mod !== 0) {
    var before = unitValue - mod;
    var after = before + step;
    return [value.clone().set(unit, before), value.clone().set(unit, after)];
  }
}

function getNearStepMomentValues(value, step) {
  if (!(0, _isNumber["default"])(step) && !(0, _isString["default"])(step) && !_math["default"].isBigNumber(step)) {
    var hour = step.hour,
        minute = step.minute,
        second = step.second;

    if (second) {
      return getNearStepMoments(value, second, _enum.TimeUnit.second);
    }

    if (minute) {
      return getNearStepMoments(value, minute, _enum.TimeUnit.minute);
    }

    if (hour) {
      return getNearStepMoments(value, hour, _enum.TimeUnit.hour);
    }
  }
}

function getNearStepValues(value, step, min, max, options) {
  if (!isEmpty(value) && !!step && (_math["default"].isBigNumber(step) || (0, _typeof2["default"])(step) !== 'object')) {
    min = (0, _defaultTo["default"])(min, -Infinity);
    max = (0, _defaultTo["default"])(max, Infinity); // min 等于最小安全数时, 且 max 小于 0, 设置 step 计算起点为: Math.floor(max / step) * step

    var actualMin = _math["default"].isFinite(min) ? min : _math["default"].lt(max, 0) ? _math["default"].multipliedBy(_math["default"].floor(_math["default"].div(max, step, options), options), step, options) : 0;
    var beforeStep = getBeforeStepValue(value, actualMin, step, options);

    if (_math["default"].eq(beforeStep, value)) {
      return undefined;
    }

    if (_math["default"].gt(beforeStep, max)) {
      beforeStep = getBeforeStepValue(max, actualMin, step, options);
    } else if (_math["default"].lt(beforeStep, min)) {
      beforeStep = min;
    }

    var afterStep = _math["default"].plus(beforeStep, step, options);

    var values = [beforeStep];

    if (_math["default"].lte(afterStep, max)) {
      values.push(afterStep);
    }

    return values;
  }
}

function getGlobalConfig(key, field) {
  if (field) {
    return field.dataSet.getConfig(key);
  }

  return (0, _configure.getConfig)(key);
}

function getDateFormatByFieldType(type, field) {
  var formatter = getGlobalConfig('formatter', field);

  switch (type) {
    case _enum2.FieldType.date:
      return formatter.date;

    case _enum2.FieldType.dateTime:
      return formatter.dateTime;

    case _enum2.FieldType.week:
      return formatter.week;

    case _enum2.FieldType.month:
      return formatter.month;

    case _enum2.FieldType.year:
      return formatter.year;

    case _enum2.FieldType.time:
      return formatter.time;

    default:
      return formatter.date;
  }
}

function getDateFormatByField(field, type, record) {
  if (field) {
    return field.get('format', record) || getDateFormatByFieldType(type || field.get('type', record), field);
  }

  if (type) {
    return getDateFormatByFieldType(type, field);
  }

  return getGlobalConfig('formatter', field).jsonDate || _moment["default"].defaultFormat;
}

function getNumberFormatOptions(type, options) {
  if (type === _enum2.FieldType.number) {
    return {
      style: 'decimal'
    };
  }

  if (options && options.currency) {
    return {
      style: 'currency'
    };
  }

  return {
    style: 'decimal',
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  };
}

function toRangeValue(value, range) {
  if ((0, _mobx.isArrayLike)(range)) {
    if ((0, _mobx.isObservableObject)(value)) {
      return [(0, _mobx.get)(value, range[0]), (0, _mobx.get)(value, range[1])];
    }

    if ((0, _isObject["default"])(value)) {
      return [value[range[0]], value[range[1]]];
    }
  } else if ((0, _mobx.isArrayLike)(value)) {
    return value.slice(0, 2);
  }

  return [undefined, undefined];
}

function normalizeLanguage(language) {
  return language && language.replace('_', '-').toLowerCase();
}

var _default = {
  MAX_SAFE_INTEGER: MAX_SAFE_INTEGER,
  MIN_SAFE_INTEGER: MIN_SAFE_INTEGER,
  warning: warning,
  isEmpty: isEmpty,
  isSame: isSame,
  isSameLike: isSameLike,
  parseNumber: parseNumber,
  getNearStepValues: getNearStepValues,
  getDateFormatByFieldType: getDateFormatByFieldType,
  getDateFormatByField: getDateFormatByField,
  getNumberFormatOptions: getNumberFormatOptions,
  toRangeValue: toRangeValue,
  normalizeLanguage: normalizeLanguage,
  parseBigNumber: parseBigNumber,
  formatFileSize: _formatter.formatFileSize
};
exports["default"] = _default;
//# sourceMappingURL=index.js.map
