"use strict";

function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }

function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var AbstractSmartDatePart =
/*#__PURE__*/
function () {
  function AbstractSmartDatePart(placeholder, isStatic) {
    _classCallCheck(this, AbstractSmartDatePart);

    this._state = AbstractSmartDatePart.STATE_NONE;
    this._cleanedValue = '';
    this._parsedValue = '';
    this._scanned = '';
    this._placeholder = placeholder;
    this._static = isStatic;
  }

  _createClass(AbstractSmartDatePart, [{
    key: "state",
    value: function state() {
      return this._state;
    }
  }, {
    key: "isNoState",
    value: function isNoState() {
      return this._state === AbstractSmartDatePart.STATE_NONE;
    }
  }, {
    key: "isIncomplete",
    value: function isIncomplete() {
      return this._state === AbstractSmartDatePart.STATE_INCOMPLETE;
    }
  }, {
    key: "isComplete",
    value: function isComplete() {
      return this._state === AbstractSmartDatePart.STATE_COMPLETE;
    }
  }, {
    key: "resetState",
    value: function resetState() {
      this._state = AbstractSmartDatePart.STATE_NONE;
    }
  }, {
    key: "cleanedValue",
    value: function cleanedValue() {
      return this._cleanedValue;
    }
  }, {
    key: "parsedValue",
    value: function parsedValue() {
      return this._parsedValue;
    }
  }, {
    key: "placeHolder",
    value: function placeHolder() {
      return this._placeholder;
    }
  }, {
    key: "isStatic",
    value: function isStatic() {
      return this._static;
    }
  }, {
    key: "_isNumericSymbol",
    value: function _isNumericSymbol(symbol) {
      return '0' <= symbol && symbol <= '9';
    }
    /**
     * @param {SmartDateCheckState} state
     * @returns {Boolean}
     */

  }, {
    key: "check",
    value: function check(state) {
      return false;
    }
  }]);

  return AbstractSmartDatePart;
}();

;
AbstractSmartDatePart.STATE_NONE = 0;
AbstractSmartDatePart.STATE_INCOMPLETE = 1;
AbstractSmartDatePart.STATE_COMPLETE = 2;

var SmartDateStaticPart =
/*#__PURE__*/
function (_AbstractSmartDatePar) {
  _inherits(SmartDateStaticPart, _AbstractSmartDatePar);

  function SmartDateStaticPart(placeholder) {
    var _this2;

    _classCallCheck(this, SmartDateStaticPart);

    _this2 = _possibleConstructorReturn(this, _getPrototypeOf(SmartDateStaticPart).call(this, placeholder, true));
    _this2._cleanedValue = placeholder;
    return _this2;
  }
  /**
   * @param {SmartDateCheckState} state
   * @returns {Boolean}
   */


  _createClass(SmartDateStaticPart, [{
    key: "parse",
    value: function parse(state) {
      var value = state.value();
      var index = state.valueIndex();

      while (index < value.length && !this._isNumericSymbol(value[index])) {
        index++;
      }

      this._parsedValue = value.substring(state.valueIndex(), index);
      this._state = AbstractSmartDatePart.STATE_COMPLETE;
      state.setValueIndex(index);
      return true;
    }
  }]);

  return SmartDateStaticPart;
}(AbstractSmartDatePart);

var AbstractSmartDateNumericPart =
/*#__PURE__*/
function (_AbstractSmartDatePar2) {
  _inherits(AbstractSmartDateNumericPart, _AbstractSmartDatePar2);

  function AbstractSmartDateNumericPart(placeholder, size) {
    var _this3;

    _classCallCheck(this, AbstractSmartDateNumericPart);

    _this3 = _possibleConstructorReturn(this, _getPrototypeOf(AbstractSmartDateNumericPart).call(this, placeholder, false));
    _this3._numericValue = 0;
    _this3._size = size;
    return _this3;
  }

  _createClass(AbstractSmartDateNumericPart, [{
    key: "numericValue",
    value: function numericValue() {
      return this._numericValue;
    }
    /**
     * @param {Date} date
     * @returns {String}
     */

  }, {
    key: "formatDate",
    value: function formatDate(date) {
      return '';
    }
  }, {
    key: "formatValue",
    value: function formatValue(value) {
      var stringValue = String(this._correctValue(value));

      if (stringValue === '0') {
        return stringValue;
      }

      if (stringValue.length > this._size) {
        return stringValue.substring(0, this._size);
      }

      if (stringValue.length < this._size) {
        return stringValue.padStart(this._size, '0');
      }

      return stringValue;
    }
  }, {
    key: "_correctValue",
    value: function _correctValue(value) {
      return Math.round(value);
    }
    /**
     * @param {SmartDateCheckState} state
     * @returns {String}
     */

  }, {
    key: "scanStringNumber",
    value: function scanStringNumber(state) {
      var index = state.valueIndex();
      var value = state.value();

      while (index < value.length && this._isNumericSymbol(value[index])) {
        index++;
      }

      var stringValue = value.substring(state.valueIndex(), index);
      state.setValueIndex(index);
      return stringValue;
    }
  }, {
    key: "calculateNumericValue",
    value: function calculateNumericValue(stringValue, maximalNumber) {
      var startIndex = 0;
      var endIndex = 0;
      var numericValue = 0;

      while (startIndex < stringValue.length && stringValue[startIndex] === 0) {
        startIndex++;
      }

      endIndex = startIndex;

      while (endIndex < stringValue.length && numericValue < maximalNumber) {
        numericValue = numericValue * 10 + Number(stringValue[endIndex]);
        endIndex++;
      }

      if (numericValue > maximalNumber) {
        endIndex--;
      }

      return Number(stringValue.substring(startIndex, endIndex));
    }
    /**
     * @param {SmartDateCheckState} state
     * @param {Number} maximalValue
     */

  }, {
    key: "scanValue",
    value: function scanValue(state, maximalValue) {
      var index = state.valueIndex();
      var value = state.value();
      var numericValue = 0;

      while (index < value.length && this._isNumericSymbol(value[index]) && numericValue < maximalValue) {
        numericValue = numericValue * 10 + Number(value[index]);
        index++;
      }

      if (numericValue > maximalValue) {
        index--;
      }

      var stringValue = value.substring(state.valueIndex(), index);
      state.setValueIndex(index);
      return stringValue;
    }
  }, {
    key: "_defineParsingState",
    value: function _defineParsingState(maximalValue, isAtEnd) {
      if (this._numericValue > 0 && (this._parsedValue.length >= this._size || !isAtEnd)) {
        this._state = AbstractSmartDatePart.STATE_COMPLETE;
        return;
      }

      if (this._numericValue * 10 > maximalValue) {
        this._state = AbstractSmartDatePart.STATE_COMPLETE;
        return;
      }

      this._state = AbstractSmartDatePart.STATE_INCOMPLETE;
    }
  }, {
    key: "_defineCleanValue",
    value: function _defineCleanValue() {
      this._cleanedValue = '';

      if (this.isNoState()) {
        return;
      }

      if (this.isComplete()) {
        this._cleanedValue = this.formatValue(this._numericValue);
        return;
      }

      if (this._parsedValue.length > 0) {
        this._cleanedValue = String(this._numericValue);
      }
    }
  }]);

  return AbstractSmartDateNumericPart;
}(AbstractSmartDatePart);

var SmartDateDayPart =
/*#__PURE__*/
function (_AbstractSmartDateNum) {
  _inherits(SmartDateDayPart, _AbstractSmartDateNum);

  function SmartDateDayPart() {
    var _this4;

    _classCallCheck(this, SmartDateDayPart);

    _this4 = _possibleConstructorReturn(this, _getPrototypeOf(SmartDateDayPart).call(this, SmartDateFormat.DAY_PLACEHOLDER, 2));
    _this4.maximalValue = SmartDateDayPart.MAXIMAL_VALUE;
    return _this4;
  }

  _createClass(SmartDateDayPart, [{
    key: "resetState",
    value: function resetState() {
      _get(_getPrototypeOf(SmartDateDayPart.prototype), "resetState", this).call(this);

      this.maximalValue = SmartDateDayPart.MAXIMAL_VALUE;
    }
  }, {
    key: "setMaximalValue",
    value: function setMaximalValue(value) {
      if (value != value
      /* isNaN */
      || value < SmartDateDayPart.MAXIMAL_VALUE_LOW_LIMIT || SmartDateDayPart.MAXIMAL_VALUE_HIGH_LIMIT < value) {
        return;
      }

      this.maximalValue = value;

      if (this._numericValue <= this.maximalValue) {
        return;
      }

      this._numericValue = this.maximalValue;

      this._defineCleanValue();
    }
  }, {
    key: "resetMaximalValue",
    value: function resetMaximalValue() {
      this.maximalValue = SmartDateDayPart.MAXIMAL_VALUE;
    }
    /**
     * @param {Date} date
     * @returns {String}
     */

  }, {
    key: "formatDate",
    value: function formatDate(date) {
      return this.formatValue(date.getDate());
    }
    /**
     * @param {SmartDateCheckState} state
     */

  }, {
    key: "parse",
    value: function parse(state) {
      this._parsedValue = this.scanStringNumber(state);
      this._numericValue = this.calculateNumericValue(this._parsedValue, this.maximalValue);

      this._defineParsingState(this.maximalValue, state.isOver());

      this._defineCleanValue();
    }
  }]);

  return SmartDateDayPart;
}(AbstractSmartDateNumericPart);

SmartDateDayPart.MAXIMAL_VALUE_LOW_LIMIT = 28;
SmartDateDayPart.MAXIMAL_VALUE_HIGH_LIMIT = 31;
SmartDateDayPart.MAXIMAL_VALUE = SmartDateDayPart.MAXIMAL_VALUE_HIGH_LIMIT;

var SmartDateMonthPart =
/*#__PURE__*/
function (_AbstractSmartDateNum2) {
  _inherits(SmartDateMonthPart, _AbstractSmartDateNum2);

  function SmartDateMonthPart() {
    _classCallCheck(this, SmartDateMonthPart);

    return _possibleConstructorReturn(this, _getPrototypeOf(SmartDateMonthPart).call(this, SmartDateFormat.MONTH_PLACEHOLDER, 2));
  }
  /**
   * @param {Date} date
   * @returns {String}
   */


  _createClass(SmartDateMonthPart, [{
    key: "formatDate",
    value: function formatDate(date) {
      return this.formatValue(date.getMonth() + 1);
    }
    /**
     * @param {SmartDateCheckState} state
     */

  }, {
    key: "parse",
    value: function parse(state) {
      this._parsedValue = this.scanStringNumber(state);
      this._numericValue = this.calculateNumericValue(this._parsedValue, SmartDateMonthPart.MAXIMAL_VALUE);

      this._defineParsingState(SmartDateMonthPart.MAXIMAL_VALUE, state.isOver());

      this._defineCleanValue();
    }
  }]);

  return SmartDateMonthPart;
}(AbstractSmartDateNumericPart);

SmartDateMonthPart.MAXIMAL_VALUE = 12;

var SmartDateYearPart =
/*#__PURE__*/
function (_AbstractSmartDateNum3) {
  _inherits(SmartDateYearPart, _AbstractSmartDateNum3);

  function SmartDateYearPart() {
    _classCallCheck(this, SmartDateYearPart);

    return _possibleConstructorReturn(this, _getPrototypeOf(SmartDateYearPart).call(this, SmartDateFormat.YEAR_PLACEHOLDER, 4));
  }
  /**
   * @param {Date} date
   * @returns {String}
   */


  _createClass(SmartDateYearPart, [{
    key: "formatDate",
    value: function formatDate(date) {
      return this.formatValue(date.getFullYear());
    }
    /**
     * @param {SmartDateCheckState} state
     * @returns {Boolean}
     */

  }, {
    key: "parse",
    value: function parse(state) {
      this._parsedValue = this.scanStringNumber(state);
      this._numericValue = Number(this._parsedValue);

      this._defineParsingState();

      if (this._numericValue > SmartDateYearPart.MAXIMAL_VALUE) {
        this._numericValue = SmartDateYearPart.MAXIMAL_VALUE;
      }

      if (this.isComplete() && this._numericValue < SmartDateYearPart.MINIMAL_VALUE) {
        this._numericValue = SmartDateYearPart.MINIMAL_VALUE;
      }

      this._defineCleanValue();
    }
  }, {
    key: "_defineParsingState",
    value: function _defineParsingState() {
      if (this._numericValue > 0 && this._parsedValue.length >= this._size) {
        this._state = AbstractSmartDatePart.STATE_COMPLETE;
        return;
      }

      this._state = AbstractSmartDatePart.STATE_INCOMPLETE;
    }
  }, {
    key: "_defineCleanValue",
    value: function _defineCleanValue() {
      if (this._numericValue > 0) {
        this._cleanedValue = String(this._numericValue);
      } else {
        this._cleanedValue = '';
      }
    }
  }]);

  return SmartDateYearPart;
}(AbstractSmartDateNumericPart);

SmartDateYearPart.MINIMAL_VALUE = 1000;
SmartDateYearPart.MAXIMAL_VALUE = 9999;

var SmartDateParseState =
/*#__PURE__*/
function () {
  function SmartDateParseState() {
    _classCallCheck(this, SmartDateParseState);

    this._value = '';
    this._valueIndex = 0;
  }

  _createClass(SmartDateParseState, [{
    key: "start",
    value: function start(value) {
      this._value = value;
      this._valueIndex = 0;
    }
  }, {
    key: "current",
    value: function current() {
      return this.isOver() ? '' : this._value[this._valueIndex];
    }
  }, {
    key: "next",
    value: function next() {
      if (this.isOver()) {
        return false;
      }

      this._valueIndex++;
      return !this.isOver();
    }
  }, {
    key: "isOver",
    value: function isOver() {
      return this._valueIndex >= this._value.length;
    }
  }, {
    key: "value",
    value: function value() {
      return this._value;
    }
  }, {
    key: "valueIndex",
    value: function valueIndex() {
      return this._valueIndex;
    }
  }, {
    key: "setValueIndex",
    value: function setValueIndex(index) {
      if (this._valueIndex < index) {
        this._valueIndex = index;
      }
    }
  }]);

  return SmartDateParseState;
}();

var SmartDateFormat =
/*#__PURE__*/
function () {
  function SmartDateFormat(format) {
    _classCallCheck(this, SmartDateFormat);

    this._day = null;
    this._month = null;
    this._year = null;
    this.loadFormat(format);
    this._parseState = new SmartDateParseState();
  }

  _createClass(SmartDateFormat, [{
    key: "loadFormat",
    value: function loadFormat(format) {
      var index = 0;
      var lowFormat = format.toLowerCase();
      this._format = format;
      this._parts = [];

      while (index < format.length) {
        index = this._scanPart(index, format, lowFormat);
      }
    }
  }, {
    key: "_scanPart",
    value: function _scanPart(index, format, lowFormat) {
      switch (lowFormat[index]) {
        case SmartDateFormat.DAY_ANCHOR:
          return this._scanDay(index, lowFormat);

        case SmartDateFormat.MONTH_ANCHOR:
          return this._scanMonth(index, lowFormat);

        case SmartDateFormat.YEAR_ANCHOR:
          return this._scanYear(index, lowFormat);
      }

      return this._scanStatic(index, format, lowFormat);
    }
  }, {
    key: "_scanDay",
    value: function _scanDay(startIndex, lowFormat) {
      this._checkLastIsStatic();

      this._day = new SmartDateDayPart();

      this._parts.push(this._day);

      return this._avoidAnchor(SmartDateFormat.DAY_ANCHOR, startIndex, lowFormat);
    }
  }, {
    key: "_scanMonth",
    value: function _scanMonth(startIndex, lowFormat) {
      this._checkLastIsStatic();

      this._month = new SmartDateMonthPart();

      this._parts.push(this._month);

      return this._avoidAnchor(SmartDateFormat.MONTH_ANCHOR, startIndex, lowFormat);
    }
  }, {
    key: "_scanYear",
    value: function _scanYear(startIndex, lowFormat) {
      this._checkLastIsStatic();

      this._year = new SmartDateYearPart();

      this._parts.push(this._year);

      return this._avoidAnchor(SmartDateFormat.YEAR_ANCHOR, startIndex, lowFormat);
    }
  }, {
    key: "_avoidAnchor",
    value: function _avoidAnchor(anchor, startIndex, lowFormat) {
      var endingIndex = startIndex;

      while (endingIndex < lowFormat.length && lowFormat[endingIndex] === anchor) {
        endingIndex++;
      }

      return endingIndex;
    }
  }, {
    key: "_checkLastIsStatic",
    value: function _checkLastIsStatic() {
      if (this._parts.length === 0) {
        return;
      }

      if (!this._parts[this._parts.length - 1].isStatic()) {
        throw new Error('Dynamic date parts must be separated by static substrings');
      }
    }
  }, {
    key: "_scanStatic",
    value: function _scanStatic(startIndex, format, lowFormat) {
      var endingIndex = startIndex;

      while (endingIndex < format.length && this._isStaticAnchorSymbol(lowFormat[endingIndex])) {
        endingIndex++;
      }

      this._parts.push(new SmartDateStaticPart(format.substring(startIndex, endingIndex)));

      return endingIndex;
    }
  }, {
    key: "_isStaticAnchorSymbol",
    value: function _isStaticAnchorSymbol(symbol) {
      return symbol !== SmartDateFormat.DAY_ANCHOR && symbol !== SmartDateFormat.MONTH_ANCHOR && symbol !== SmartDateFormat.YEAR_ANCHOR;
    }
  }, {
    key: "getPlaceHolder",
    value: function getPlaceHolder() {
      var placeHolder = '';

      for (var i = 0; i < this._parts.length; i++) {
        placeHolder += this._parts[i].placeHolder();
      }

      return placeHolder;
    }
    /**
     * @param {Date} date
     * @returns {String}
     */

  }, {
    key: "formatDate",
    value: function formatDate(date) {
      var formattedDate = '';

      for (var i = 0; i < this._parts.length; i++) {
        if (this._parts[i].isStatic()) {
          formattedDate += this._parts[i].placeHolder();
        } else {
          formattedDate += this._parts[i].formatDate(date);
        }
      }

      return formattedDate;
    }
  }, {
    key: "parse",
    value: function parse(value, autocomplete) {
      this._resetParseState();

      this._parseState.start(value);

      this._parseParts(autocomplete);

      this._correctDate();
    }
  }, {
    key: "_resetParseState",
    value: function _resetParseState() {
      for (var i = 0; i < this._parts.length; i++) {
        this._parts[i].resetState();
      }
    }
  }, {
    key: "_parseParts",
    value: function _parseParts(autocomplete) {
      var index = 0;

      while (index < this._parts.length && (autocomplete || !this._parseState.isOver())) {
        this._parts[index].parse(this._parseState);

        if (!this._parts[index].isComplete()) {
          return;
        }

        index++;
      }
    }
  }, {
    key: "_correctDate",
    value: function _correctDate() {
      if (!this._day.isComplete() || !this._month.isComplete()) {
        return;
      }

      this._day.setMaximalValue(this._getMonthSize(this._month.numericValue() - 1, !this._year.isComplete() || this._isLeaPYear(this._year.numericValue())));
    }
  }, {
    key: "getParsedDate",
    value: function getParsedDate() {
      if (!this.isComplete()) {
        return null;
      }

      return new Date(this._year.numericValue(), this._month.numericValue() - 1, this._day.numericValue());
    }
  }, {
    key: "_getMonthSize",
    value: function _getMonthSize(monthIndex, isLeapYear) {
      if (monthIndex < 0 || monthIndex >= SmartDateMonthPart.MAXIMAL_VALUE) {
        return SmartDateMonthPart.MAXIMAL_VALUE;
      }

      if (monthIndex === 1 && isLeapYear) {
        // February
        return SmartDateFormat.BASIC_MONTH_SIZES[monthIndex] + 1;
      }

      return SmartDateFormat.BASIC_MONTH_SIZES[monthIndex];
    }
  }, {
    key: "_isLeaPYear",
    value: function _isLeaPYear(year) {
      return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
    }
  }, {
    key: "getCorrectedValue",
    value: function getCorrectedValue() {
      var index = 0;
      var value = '';

      while (index < this._parts.length && !this._parts[index].isNoState()) {
        value += this._parts[index].cleanedValue();
        index++;
      }

      return value;
    }
  }, {
    key: "isComplete",
    value: function isComplete() {
      return this._parts[this._parts.length - 1].isComplete();
    }
  }]);

  return SmartDateFormat;
}();

SmartDateFormat.DAY_ANCHOR = 'd';
SmartDateFormat.DAY_PLACEHOLDER = 'ДД';
SmartDateFormat.MONTH_ANCHOR = 'm';
SmartDateFormat.MONTH_PLACEHOLDER = 'ММ';
SmartDateFormat.YEAR_ANCHOR = 'y';
SmartDateFormat.YEAR_PLACEHOLDER = 'ГГГГ';
SmartDateFormat.BASIC_MONTH_SIZES = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

var SmartDateField =
/*#__PURE__*/
function () {
  function SmartDateField(field, options) {
    _classCallCheck(this, SmartDateField);

    this._dateYmdParser = new RegExp('^([0-9]{4})[,\._\/\-]+([0-9]{1,2})[,\._\/\-]+([0-9]{1,2})$');
    this._dateDmyParser = new RegExp('^([0-9]{1,2})[,\._\/\-]+([0-9]{1,2})[,\._\/\-]+([0-9]{4})$');

    this._loadOptions(options);

    this._formatter = new SmartDateFormat(this._format, this._minimalDate, this._maximalDate);
    this._lastCorrectValue = this._defaultDate === null ? null : this._formatter.formatDate(this._defaultDate);

    this._initField(field);

    this._subscribeEvents();
  }

  _createClass(SmartDateField, [{
    key: "_loadDefaultOptions",
    value: function _loadDefaultOptions() {
      this._format = 'd.m.y';
      this._minimalDate = new Date(1900, 0, 1);
      this._maximalDate = new Date(2099, 11, 31);
      this._defaultDate = null;
    }
  }, {
    key: "_loadOptions",
    value: function _loadOptions(options) {
      this._loadDefaultOptions();

      if (_typeof(options) !== 'object' || options == null) {
        return;
      }

      if (typeof options.format === 'string' && options.format.indexOf('d') >= 0 && options.format.indexOf('m') >= 0 && options.format.indexOf('y') >= 0) {
        this._format = options.format;
      }

      if (typeof options.minimal !== 'undefined') {
        var date = this._parseDateValue(options.minimal);

        this._minimalDate = date;
      }

      if (typeof options.maximal !== 'undefined') {
        var _date = this._parseDateValue(options.maximal);

        this._maximalDate = _date;
      }

      if (typeof options["default"] !== 'undefined') {
        var _date2 = this._parseDateValue(options["default"]);

        this._defaultDate = _date2;
      }

      this._correctDateOptions();
    }
  }, {
    key: "_correctDateOptions",
    value: function _correctDateOptions() {
      if (this._minimalDate.valueOf() > this._maximalDate.valueOf()) {
        var temporaryDate = this._minimalDate;
        this._minimalDate = this._maximalDate;
        this._maximalDate = temporaryDate;
      }

      if (this._defaultDate === null) {
        return;
      }

      if (this._defaultDate.valueOf() < this._minimalDate.valueOf()) {
        this._defaultDate = this._minimalDate;
      }

      if (this._defaultDate.valueOf() > this._maximalDate.valueOf()) {
        this._defaultDate = this._maximalDate;
      }
    }
  }, {
    key: "_parseDateValue",
    value: function _parseDateValue(date) {
      if (_typeof(date) === 'object' && date !== null && date instanceof Date) {
        return date;
      }

      if (typeof date !== 'string' || date.trim() === '') {
        return null;
      }

      var parsed = this._dateYmdParser.exec(date);

      if (parsed !== null) {
        return new Date(Number(parsed[1]), Number(parsed[2]) - 1, Number(parsed[3]));
      }

      parsed = this._dateDmyParser.exec(date);

      if (parsed === null) {
        return null;
      }

      return new Date(Number(parsed[3]), Number(parsed[2]) - 1, Number(parsed[1]));
    }
  }, {
    key: "_initField",
    value: function _initField(field) {
      this._field = field;
      this._field.placeholder = this._formatter.getPlaceHolder();

      if (this._isEmptyValue(this._field.value)) {
        this._field.value = this._defaultDate === null ? '' : this._formatter.formatDate(this._defaultDate);
      } else {
        this.correctValue(true);
      }
    }
  }, {
    key: "_subscribeEvents",
    value: function _subscribeEvents() {
      var _this = this;

      var correct = function correct(event) {
        if (event === null || event.type !== 'keyup') {
          _this.correctValue(true);
        }

        if (_this._isServiceCode(event.keyCode)) {
          return;
        }

        if (event.keyCode === 27) {
          // Escape
          _this.returnLastCorrectValue();
        }

        _this.correctValue(true);
      };

      this._field.addEventListener('change', correct);

      this._field.addEventListener('keyup', correct);

      this._field.addEventListener('blur', function () {
        _this.correctValue(true);

        if (!_this._isEmptyValue(_this._field.value)) {
          _this.returnLastCorrectValue();
        }
      });
    }
  }, {
    key: "_isServiceCode",
    value: function _isServiceCode(code) {
      return code === 8
      /* backspace */
      || code === 46
      /* delete */
      || code === 16
      /* shift */
      || code === 17
      /* shift */
      || code === 18
      /* shift */
      || code === 20
      /* caps lock */
      || code === 35
      /* end */
      || code === 36
      /* home */
      || code === 37
      /* left */
      || code === 39
      /* right */
      || code === 46
      /* delete */
      ;
    }
  }, {
    key: "_isEmptyValue",
    value: function _isEmptyValue(value) {
      return value.trim() === '';
    }
  }, {
    key: "getDefaultValue",
    value: function getDefaultValue() {
      return this._defaultDate === null ? '' : this._formatter.formatDate(this._defaultDate);
    }
  }, {
    key: "correctValue",
    value: function correctValue(autocomplete) {
      this._formatter.parse(this._field.value, autocomplete);

      var parsedValue = this._getLimitedParsedValue();

      if (this._formatter.isComplete()) {
        this._lastCorrectValue = parsedValue;
      }

      if (this._field.value === parsedValue) {
        return;
      }

      this._setValueToField(parsedValue);
    }
  }, {
    key: "setValue",
    value: function setValue(value) {
      var date = this._parseDateValue(value);

      if (date === null || date.valueOf() < this._minimalDate.valueOf() || date.valueOf() > this._maximalDate.valueOf()) {
        return;
      }

      this._lastCorrectValue = this._formatter.formatDate(date);
      this._field.value = this._lastCorrectValue;
    }
  }, {
    key: "_getLimitedParsedValue",
    value: function _getLimitedParsedValue() {
      if (!this._formatter.isComplete()) {
        return this._formatter.getCorrectedValue();
      }

      var date = this._formatter.getParsedDate();

      if (date.valueOf() < this._minimalDate.valueOf()) {
        return this._formatter.formatDate(this._minimalDate);
      } else if (date.valueOf() > this._maximalDate.valueOf()) {
        return this._formatter.formatDate(this._maximalDate);
      }

      return this._formatter.getCorrectedValue();
    }
  }, {
    key: "_setValueToField",
    value: function _setValueToField(newValue) {
      var position = this._field.selectionStart;
      var isAtEnd = position === this._field.value.length;
      this._field.value = newValue;

      if (!isAtEnd && position < this._field.value.length) {
        this._field.selectionStart = position;
        this._field.selectionEnd = position;
      }
    }
  }, {
    key: "returnLastCorrectValue",
    value: function returnLastCorrectValue() {
      if (this._lastCorrectValue !== null) {
        this._field.value = this._lastCorrectValue;
      }
    }
  }]);

  return SmartDateField;
}();

SmartDateField.initBy = function (id) {
  return SmartDateField.initFor(document.getElementById(id));
};

SmartDateField.initFor = function (field, options) {
  if (_typeof(field) !== 'object' || field === null || field.tagName.toLowerCase() !== 'input' || field.type.toLowerCase() !== 'text') {
    return;
  }

  if (_typeof(field._smartDate) === 'object' && field._smartDate !== null && field._smartDate instanceof SmartDateField) {
    return;
  }

  field._smartDate = new SmartDateField(field, options);
};

jQuery.fn.smartdate = function (options) {
  var collection = [];
  var field = null;

  for (var i = 0; i < this.length; i++) {
    field = SmartDateField.initFor(this[i], options);

    if (field !== null) {
      collection.push(field);
    }
  }

  return jQuery(this);
};

jQuery.fn.setDateValue = function (value) {
  for (var i = 0; i < this.length; i++) {
    if (_typeof(this[i]._smartDate) === 'object' && this[i]._smartDate !== null && this[i]._smartDate instanceof SmartDateField) {
      this[i]._smartDate.setValue(value);
    }
  }

  return jQuery(this);
};
