"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.InternalDate = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _constants = require("../locale/constants");

var _constants2 = require("./constants");
var _InternalDateCalculator = require("./InternalDateCalculator");
var _InternalDateGetter = require("./InternalDateGetter");
var _InternalDateSetter = require("./InternalDateSetter");
var _InternalDateTransformer = require("./InternalDateTransformer");
var _InternalDateValidator = require("./InternalDateValidator");
var _localeSets = require("./localeSets");










var _types = require("./types");






var getRestoreYear = function getRestoreYear(
prev,
today)
{
  if (prev.year !== null && _InternalDateValidator.InternalDateValidator.testParseToNumber(prev.year)) {
    if (prev.year > 50 && prev.year < 100) {
      return Number(prev.year) + 1900;
    } else if (prev.year > 0 && prev.year < 51) {
      return Number(prev.year) + 2000;
    }

    return prev.year;
  }

  return today.year;
};var

InternalDate = exports.InternalDate = /*#__PURE__*/function () {








  function InternalDate(_temp) {var _ref = _temp === void 0 ? {} : _temp,order = _ref.order,separator = _ref.separator,_ref$langCode = _ref.langCode,langCode = _ref$langCode === void 0 ? _constants.defaultLangCode : _ref$langCode,value = _ref.value;this.components = (0, _extends2.default)({}, _constants2.emptyDateComponents);this.canonicalLocale = 'ru-RU';this.start = null;this.end = null;
    this.order = order != null ? order : _localeSets.internalDateLocale[langCode].order;
    this.separator = separator != null ? separator : _localeSets.internalDateLocale[langCode].separator;
    this.canonicalLocale = _constants.langCodesToCanonicalLocale[langCode];

    if (value !== undefined) {
      this.parseInternalValue(value);
    }
  }var _proto = InternalDate.prototype;_proto.

  getComponentsRaw = function getComponentsRaw() {
    return (0, _extends2.default)({}, this.components);
  };_proto.

  getComponentsLikeNumber = function getComponentsLikeNumber() {
    return _InternalDateTransformer.InternalDateTransformer.dateComponentsStringToNumber(this.getComponentsRaw());
  };_proto.

  getSeparator = function getSeparator() {
    return this.separator;
  };_proto.

  getOrder = function getOrder() {
    return this.order;
  };_proto.

  getYear = function getYear() {
    return this.components.year;
  };_proto.

  getMonth = function getMonth() {
    return this.components.month;
  };_proto.

  getDate = function getDate() {
    return this.components.date;
  };_proto.

  getRangeStart = function getRangeStart() {
    return this.start;
  };_proto.

  getRangeEnd = function getRangeEnd() {
    return this.end;
  };_proto.

  setOrder = function setOrder(order) {if (order === void 0) {order = _constants2.defaultDateComponentsOrder;}
    this.order = order;
    return this;
  };_proto.

  setSeparator = function setSeparator(separator) {if (separator === void 0) {separator = _constants2.defaultDateComponentsSeparator;}
    this.separator = separator;
    return this;
  };_proto.

  setComponents = function setComponents(components, isNativeMonth) {if (isNativeMonth === void 0) {isNativeMonth = false;}
    if (components && isNativeMonth) {
      var clone = this.clone().setComponents(components).shiftMonth(1);
      if (clone.validate({ checks: [_types.InternalDateValidateCheck.Native] })) {
        this.components = (0, _extends2.default)({}, clone.getComponentsLikeNumber());
      }
      return this;
    }
    this.components = components || (0, _extends2.default)({}, _constants2.emptyDateComponents);
    return this;
  };_proto.

  setYear = function setYear(year) {
    this.components.year = year;
    return this;
  };_proto.

  setMonth = function setMonth(month) {
    this.components.month = month;
    return this;
  };_proto.

  setDate = function setDate(date) {
    this.components.date = date;
    return this;
  };_proto.

  shiftYear = function shiftYear(step, _temp2) {var _ref2 = _temp2 === void 0 ? {} : _temp2,isLoop = _ref2.isLoop,isRange = _ref2.isRange,isCutFeb = _ref2.isCutFeb;
    var min = this.getMinValue(_types.InternalDateComponentType.Year, isRange);
    var max = this.getMaxValue(_types.InternalDateComponentType.Year, { isRange: isRange, isCutFeb: isCutFeb });
    var _this$getComponentsLi = this.getComponentsLikeNumber(),year = _this$getComponentsLi.year;
    this.components.year = _InternalDateCalculator.InternalDateCalculator.calcShiftValueDateComponent(step, year, min, max, isLoop);
    return this;
  };_proto.

  shiftMonth = function shiftMonth(step, _temp3) {var _ref3 = _temp3 === void 0 ? {} : _temp3,isLoop = _ref3.isLoop,isRange = _ref3.isRange,isCutFeb = _ref3.isCutFeb;
    var min = this.getMinValue(_types.InternalDateComponentType.Month, isRange);
    var max = this.getMaxValue(_types.InternalDateComponentType.Month, { isRange: isRange, isCutFeb: isCutFeb });
    var _this$getComponentsLi2 = this.getComponentsLikeNumber(),month = _this$getComponentsLi2.month;
    this.components.month = _InternalDateCalculator.InternalDateCalculator.calcShiftValueDateComponent(step, month, min, max, isLoop);
    return this;
  };_proto.

  shiftDate = function shiftDate(step, _temp4) {var _ref4 = _temp4 === void 0 ? {} : _temp4,isLoop = _ref4.isLoop,isRange = _ref4.isRange,isCutFeb = _ref4.isCutFeb;
    var min = this.getMinValue(_types.InternalDateComponentType.Date, isRange);
    var max = this.getMaxValue(_types.InternalDateComponentType.Date, { isRange: isRange, isCutFeb: isCutFeb });
    var _this$getComponentsLi3 = this.getComponentsLikeNumber(),date = _this$getComponentsLi3.date;
    this.components.date = _InternalDateCalculator.InternalDateCalculator.calcShiftValueDateComponent(step, date, min, max, isLoop);
    return this;
  };_proto.

  setRangeStart = function setRangeStart(internalDate) {
    this.start = internalDate;
    return this;
  };_proto.

  setRangeEnd = function setRangeEnd(internalDate) {
    this.end = internalDate;
    return this;
  };_proto.

  get = function get(type) {
    return type !== null ? _InternalDateGetter.InternalDateGetter.getValueDateComponent(type, this.getComponentsRaw()) : null;
  };_proto.

  set = function set(type, value) {
    return type !== null ? _InternalDateSetter.InternalDateSetter.setValueDateComponent(this, type, value) : this;
  };_proto.

  shift = function shift(
  type,
  step,
  settings)
  {
    return type !== null ? _InternalDateSetter.InternalDateSetter.shiftValueDateComponent(this, type, step, settings) : this;
  };_proto.

  parseValue = function parseValue(value) {if (value === void 0) {value = '';}
    var components = _InternalDateTransformer.InternalDateTransformer.parseValueToDate(value, this.order) || (0, _extends2.default)({}, _constants2.emptyDateComponents);
    this.setComponents(components);
    return this;
  };_proto.

  parseInternalValue = function parseInternalValue(value) {if (value === void 0) {value = '';}
    var components = _InternalDateTransformer.InternalDateTransformer.parseValueToDate(value, _types.InternalDateOrder.DMY) || (0, _extends2.default)({},
    _constants2.emptyDateComponents);

    this.setComponents(components);
    return this;
  };_proto.

  validate = function validate(_temp5)







  {var _ref5 = _temp5 === void 0 ? {} : _temp5,type = _ref5.type,nextValue = _ref5.nextValue,_ref5$checks = _ref5.checks,checks = _ref5$checks === void 0 ? Object.values(_types.InternalDateValidateCheck).filter(_types.isInternalDateValidateCheck) : _ref5$checks;
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    var self = this;
    if (type !== undefined) {
      var clone = this.clone();
      if (nextValue !== undefined) {
        clone.set(type, nextValue);
      }
      self = clone;
    }
    if (
    checks.includes(_types.InternalDateValidateCheck.NotNull) &&
    !_InternalDateValidator.InternalDateValidator.checkForNull(self.getComponentsRaw(), type))
    {
      return false;
    }
    if (
    checks.includes(_types.InternalDateValidateCheck.Number) &&
    !Object.values(self.getComponentsRaw()).every(_InternalDateValidator.InternalDateValidator.testParseToNumber))
    {
      return false;
    }
    if (
    checks.includes(_types.InternalDateValidateCheck.Limits) &&
    !_InternalDateValidator.InternalDateValidator.checkLimits(self.getComponentsLikeNumber(), type))
    {
      return false;
    }
    if (
    checks.includes(_types.InternalDateValidateCheck.Native) &&
    !_InternalDateValidator.InternalDateValidator.compareWithNativeDate(self.getComponentsLikeNumber()))
    {
      return false;
    }
    if (checks.includes(_types.InternalDateValidateCheck.Range)) {
      return type !== undefined ?
      _InternalDateValidator.InternalDateValidator.checkRangePiecemeal(
        type,
        self.getComponentsLikeNumber(),
        self.start && self.start.getComponentsLikeNumber(),
        self.end && self.end.getComponentsLikeNumber()
      ) :
      _InternalDateValidator.InternalDateValidator.checkRangeFully(
        self.toNumber(),
        self.start && self.start.toNumber(),
        self.end && self.end.toNumber()
      );
    }
    return true;
  };_proto.

  toFragments = function toFragments(
  settings,
  components)
  {if (settings === void 0) {settings = {};}if (components === void 0) {components = this.getComponentsRaw();}
    return _InternalDateTransformer.InternalDateTransformer.dateToFragments(components, (0, _extends2.default)({
      order: this.order,
      separator: this.separator },
    settings)
    );
  }

  /**
   * Перевод даты в числовое представление (**НЕ** аналог `timestamp`)
   * Предназначено для быстрого сравнивания дат `<=>`
   */;_proto.
  toNumber = function toNumber() {
    return Number(
      this.toFragments({ order: _types.InternalDateOrder.YMD, withPad: true }).
      map(function (_ref6) {var valueWithPad = _ref6.valueWithPad;return valueWithPad;}).
      join('')
    );
  };_proto.

  toString = function toString(settings) {if (settings === void 0) {settings = {};}
    return this.toFragments((0, _extends2.default)({ withPad: true, withSeparator: true }, settings)).
    filter(function (_ref7) {var value = _ref7.value;return value !== null;}).
    map(function (_ref8) {var type = _ref8.type,valueWithPad = _ref8.valueWithPad,value = _ref8.value;return (
        settings.withPad && type !== _types.InternalDateComponentType.Separator ? valueWithPad : value);}
    ).
    join('');
  };_proto.

  toInternalString = function toInternalString() {
    return _InternalDateTransformer.InternalDateTransformer.dateToInternalString(this.getComponentsRaw());
  };_proto.

  toNativeFormat = function toNativeFormat() {
    var components = this.getComponentsLikeNumber();
    if (_InternalDateValidator.InternalDateValidator.compareWithNativeDate(components)) {
      return (0, _extends2.default)({}, components, { month: components.month - 1 });
    }
    return undefined;
  };_proto.

  toA11YFormat = function toA11YFormat() {
    return _InternalDateTransformer.InternalDateTransformer.componentsToA11YFormat(this.components, this.canonicalLocale);
  };_proto.

  clone = function clone() {
    return new InternalDate({ order: this.order, separator: this.separator }).
    setComponents((0, _extends2.default)({}, this.components)).
    setRangeStart(this.start && this.start.clone()).
    setRangeEnd(this.end && this.end.clone());
  };_proto.

  duplicateOf = function duplicateOf(pattern) {
    return this.setComponents(pattern.getComponentsRaw());
  };_proto.

  restore = function restore(type) {if (type === void 0) {type = null;}
    var prev = this.getComponentsRaw();
    var today = _InternalDateGetter.InternalDateGetter.getTodayComponents();

    if (prev.year === null && prev.month === null && prev.date === null) {
      return this;
    }

    var restoreYear = getRestoreYear(prev, today);

    if (
    type === null && restoreYear !== prev.year ||
    type === _types.InternalDateComponentType.Year ||
    type === _types.InternalDateComponentType.All)
    {
      this.setYear(restoreYear);
    }

    if (
    type === null && prev.month === null ||
    type === _types.InternalDateComponentType.Month ||
    type === _types.InternalDateComponentType.All)
    {
      this.setMonth(today.month);
    }

    if (
    type === null && prev.date === null ||
    type === _types.InternalDateComponentType.Date ||
    type === _types.InternalDateComponentType.All)
    {
      this.setDate(today.date);
    }

    return this;
  };_proto.

  cutOffExcess = function cutOffExcess(
  type, _temp6)

  {if (type === void 0) {type = null;}var _ref9 = _temp6 === void 0 ? {} : _temp6,_ref9$isLoop = _ref9.isLoop,isLoop = _ref9$isLoop === void 0 ? false : _ref9$isLoop,isRange = _ref9.isRange,_ref9$isCutFeb = _ref9.isCutFeb,isCutFeb = _ref9$isCutFeb === void 0 ? false : _ref9$isCutFeb;
    var _this$components = this.components,year = _this$components.year,month = _this$components.month,date = _this$components.date;
    if ((type === null || type === _types.InternalDateComponentType.Year) && _InternalDateValidator.InternalDateValidator.testParseToNumber(year)) {
      this.shiftYear(0, { isLoop: isLoop, isRange: isRange, isCutFeb: isCutFeb });
    }
    if ((type === null || type === _types.InternalDateComponentType.Month) && _InternalDateValidator.InternalDateValidator.testParseToNumber(month)) {
      this.shiftMonth(0, { isLoop: isLoop, isRange: isRange, isCutFeb: isCutFeb });
    }
    if ((type === null || type === _types.InternalDateComponentType.Date) && _InternalDateValidator.InternalDateValidator.testParseToNumber(date)) {
      this.shiftDate(0, { isLoop: isLoop, isRange: isRange, isCutFeb: isCutFeb });
    }
    return this;
  };_proto.

  isIncomplete = function isIncomplete() {
    return !this.isEmpty() && Object.values(this.components).some(function (component) {return component === null;});
  };_proto.

  isEmpty = function isEmpty() {
    return Object.values(this.components).every(function (component) {return component === null;});
  };_proto.

  isEqualComponentDate = function isEqualComponentDate(type, compared) {
    return this.get(type) === compared.get(type);
  };_proto.

  isEqual = function isEqual(compared) {
    return (
      _InternalDateValidator.InternalDateValidator.isEqualDateValues(this, compared) &&
      _InternalDateValidator.InternalDateValidator.isEqualDateFormats(this, compared));

  };_proto.

  getMinValue = function getMinValue(type, isRange) {
    if (isRange === true && this.start !== null) {
      return Number(
        _InternalDateCalculator.InternalDateCalculator.calcRangeStartDateComponent(
          type,
          this.getComponentsLikeNumber(),
          this.start.getComponentsLikeNumber()
        )
      );
    }
    return _InternalDateGetter.InternalDateGetter.getDefaultMin(type);
  };_proto.

  getMaxValue = function getMaxValue(
  type, _temp7)

  {var _ref10 = _temp7 === void 0 ? {} : _temp7,isRange = _ref10.isRange,isCutFeb = _ref10.isCutFeb;
    if (isRange && this.end !== null) {
      return Number(
        _InternalDateCalculator.InternalDateCalculator.calcRangeEndDateComponent(
          type,
          this.getComponentsLikeNumber(),
          this.end.getComponentsLikeNumber()
        )
      );
    }
    if (!isCutFeb) {
      return _InternalDateGetter.InternalDateGetter.getDefaultMax(type);
    }
    return _InternalDateGetter.InternalDateGetter.getDefaultMax(type, this.getComponentsLikeNumber());
  };return InternalDate;}();