"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.isInvalidValue = isInvalidValue;
exports.isValid = isValid;
exports.parseDate = parseDate;
exports.parseStrictInputValue = parseStrictInputValue;
var _isValid = _interopRequireDefault(require("date-fns/isValid"));
var _isBefore = _interopRequireDefault(require("date-fns/isBefore"));
var _parse = _interopRequireDefault(require("date-fns/parse"));
var _constants = require("./constants");
var _constants2 = require("../constants");
function isValid(date, minDate, maxDate) {
  return (0, _isValid.default)(date) && !(0, _isBefore.default)(date, minDate !== null && minDate !== void 0 ? minDate : _constants2.MIN_DATE) && (0, _isBefore.default)(date, maxDate !== null && maxDate !== void 0 ? maxDate : _constants2.MAX_DATE);
}
function isInvalidValue(value) {
  return value === '0';
}
function getDateFormat(dateStyle, locale, isOtherFormat) {
  if (dateStyle === 'short' && isOtherFormat || dateStyle === 'medium' && !isOtherFormat) {
    return _constants.mediumDateFormats[locale];
  }
  return _constants.shortDateFormats[locale];
}
function parseLongerFormats(value, format, locale) {
  var parsedDate = (0, _parse.default)(value, format, Date.now());
  if (!isValid(parsedDate)) {
    parsedDate = (0, _parse.default)(value, _constants.longDateFormats[locale], Date.now());
  }
  if (!isValid(parsedDate)) {
    parsedDate = (0, _parse.default)(value, _constants.fullDateFormats[locale], Date.now());
  }
  return parsedDate;
}
function parseShortFormat(value, format) {
  var _value$match, _format$match;
  if (!_constants2.SHORT_SEPARATORS_REGEX.test(value)) {
    return null;
  }
  var invalidSeparator = (_value$match = value.match(/\W/)) == null ? void 0 : _value$match[0];
  var validSeparator = (_format$match = format.match(/\W/)) == null ? void 0 : _format$match[0];
  if (!invalidSeparator || !validSeparator) {
    return null;
  }
  var newValue = value.split(invalidSeparator).join(validSeparator);
  return (0, _parse.default)(newValue, format, Date.now());
}
function parseStrictInputValue(value, dateStyle, locale) {
  var dateFormat = getDateFormat(dateStyle, locale);
  var otherDateFormat = getDateFormat(dateStyle, locale, true);
  var parsedDate = parseShortFormat(value, dateStyle === 'short' ? dateFormat : otherDateFormat);
  if (!isValid(parsedDate)) {
    parsedDate = parseLongerFormats(value, dateStyle === 'short' ? otherDateFormat : dateFormat, locale);
  }
  return isValid(parsedDate) ? {
    parsedDate,
    dateFormat
  } : {
    dateFormat
  };
}
function parseDate(value, dateStyle, locale) {
  if (isInvalidValue(value)) {
    return null;
  }
  var dateFormat = getDateFormat(dateStyle, locale);
  var slicedDateFormat = dateFormat.slice(0, value.length);
  var otherDateFormat = getDateFormat(dateStyle, locale, true).slice(0, value.length);
  var parsedDate = (0, _parse.default)(value, dateFormat, Date.now());
  if (!isValid(parsedDate)) {
    parsedDate = (0, _parse.default)(value, slicedDateFormat, Date.now());
  }
  if (!isValid(parsedDate)) {
    parsedDate = parseShortFormat(value, dateStyle === 'short' ? slicedDateFormat : otherDateFormat);
  }
  if (!isValid(parsedDate)) {
    parsedDate = parseLongerFormats(value, dateStyle === 'short' ? otherDateFormat : slicedDateFormat, locale);
  }
  return isValid(parsedDate) ? parsedDate : null;
}
//# sourceMappingURL=utils.js.map