'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
var _possibleConstructorReturn = _interopDefault(require('@babel/runtime/helpers/possibleConstructorReturn'));
var _getPrototypeOf = _interopDefault(require('@babel/runtime/helpers/getPrototypeOf'));
var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits'));
var _wrapNativeSuper = _interopDefault(require('@babel/runtime/helpers/wrapNativeSuper'));
var utils = require('@forensic-js/utils');
var Constants = require('./Constants');

var CustomDate =
/*#__PURE__*/
function (_Date) {
  _inherits(CustomDate, _Date);

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

    return _possibleConstructorReturn(this, _getPrototypeOf(CustomDate).apply(this, arguments));
  }

  _createClass(CustomDate, [{
    key: "toString",
    value: function toString() {
      return "".concat(this.getFullYear(), "-").concat(utils.padLeft(this.getMonth() + 1, 2), "-").concat(utils.padLeft(this.getDate(), 2));
    }
  }], [{
    key: "isValid",

    /**
     * returns a custom date object if the given date params are valid, else it returns false
     *
     * @param yearOrDateString year parameter or date string
     * @param month month parameter, default to first month
     * @param day day parameter, defaults to first day
     */
    value: function isValid(yearOrDateString) {
      var month = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
      var day = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
      var year = 0;

      if (utils.isString(yearOrDateString) && !Constants.DATE_FORMAT.test(yearOrDateString)) {
        return false;
      } else if (utils.isString(yearOrDateString)) {
        year = parseInt(RegExp.$1);
        month = parseInt(RegExp.$3) - 1;
        day = parseInt(RegExp.$4);
      } else {
        year = yearOrDateString;
      }

      var d = new CustomDate(year, month, day);
      var isValid = d.getFullYear() === year && d.getMonth() === month && d.getDate() === day;
      return isValid ? d : false;
    }
  }]);

  return CustomDate;
}(_wrapNativeSuper(Date));

module.exports = CustomDate;
//# sourceMappingURL=CustomDate.js.map
