"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.parseDateStrings = exports.parseDateString = exports.newDate = exports.isValid = exports.getLocale = exports.getDateFormat = exports.getCalendarIndex = exports.getCalendar = void 0;
var _format = _interopRequireDefault(require("date-fns/format"));
var _isAfter = _interopRequireDefault(require("date-fns/isAfter"));
var _isValid = _interopRequireDefault(require("date-fns/isValid"));
var _addDays = _interopRequireDefault(require("date-fns/addDays"));
var _toDate = _interopRequireDefault(require("date-fns/toDate"));
var _parseISO = _interopRequireDefault(require("date-fns/parseISO"));
var _startOfWeek = _interopRequireDefault(require("date-fns/startOfWeek"));
var _sv = _interopRequireDefault(require("date-fns/locale/sv"));
var _enUS = _interopRequireDefault(require("date-fns/locale/en-US"));
var _nb = _interopRequireDefault(require("date-fns/locale/nb"));
var _da = _interopRequireDefault(require("date-fns/locale/da"));
var _fi = _interopRequireDefault(require("date-fns/locale/fi"));
var _dateFns = require("date-fns");
var _textUtils = require("./textUtils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var dateFormat = {
  sv: 'yyyy-MM-dd',
  nb: 'd.M.yyyy',
  da: 'd.M.yyyy',
  fi: 'd.M.yyyy',
  en: 'dd/MM/yyyy'
};
var locales = {
  sv: _sv.default,
  nb: _nb.default,
  da: _da.default,
  fi: _fi.default,
  en: _enUS.default
};
var getLocale = exports.getLocale = function getLocale() {
  var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  if (locales[locale.split('-')[0]]) {
    return locales[locale.split('-')[0]];
  }
  return locales.en;
};
var getDateFormat = exports.getDateFormat = function getDateFormat() {
  var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  if (dateFormat[locale.split('-')[0]]) {
    return dateFormat[locale.split('-')[0]];
  }
  return dateFormat.en;
};
var isValid = exports.isValid = function isValid(date) {
  return (0, _isValid.default)(date) && (0, _isAfter.default)(date, new Date('1/1/1000'));
};
function isString(value) {
  return typeof value === 'string' || value instanceof String;
}
var newDate = exports.newDate = function newDate() {
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date();
  var d = isString(value) ? (0, _parseISO.default)(value) : (0, _toDate.default)(value);
  return isValid(d) ? d : new Date();
};
var parseDateString = exports.parseDateString = function parseDateString(dateString, locale) {
  if (!(0, _dateFns.isMatch)(dateString, getDateFormat(locale), {
    locale: getLocale(locale)
  })) return null;
  var date = (0, _dateFns.parse)(dateString, getDateFormat(locale), newDate());
  if (!isValid(date)) return null;
  return date;
};
var parseDateStrings = exports.parseDateStrings = function parseDateStrings(startDateString, endDateString, locale) {
  var parsedStartDate = parseDateString(startDateString, locale);
  var parsedEndDate = parseDateString(endDateString, locale);
  if (parsedStartDate && parsedEndDate) return parsedStartDate < parsedEndDate ? [parsedStartDate, parsedEndDate] : [parsedEndDate, parsedStartDate];
  return [parsedStartDate, parsedEndDate];
};
var getCalendar = exports.getCalendar = function getCalendar(now, opts) {
  var _ref, _ref2;
  var calendar = {
    weekDays: [],
    dates: []
  };
  var firstCalDay = (0, _startOfWeek.default)(new Date(now.getFullYear(), now.getMonth(), 0), {
    locale: opts === null || opts === void 0 ? void 0 : opts.locale,
    weekStartsOn: 1
  });
  calendar.weekDays = (_ref = _toConsumableArray(Array(7).keys())) === null || _ref === void 0 ? void 0 : _ref.map(function (w) {
    return (0, _textUtils.capitalize)((0, _format.default)((0, _addDays.default)(firstCalDay, w), 'EEE', {
      locale: opts === null || opts === void 0 ? void 0 : opts.locale
    }));
  });
  calendar.dates = (_ref2 = _toConsumableArray(Array(6).keys())) === null || _ref2 === void 0 ? void 0 : _ref2.map(function (w) {
    var _ref3;
    return (_ref3 = _toConsumableArray(Array(7).keys())) === null || _ref3 === void 0 ? void 0 : _ref3.map(function (d) {
      return (0, _addDays.default)(firstCalDay, w * 7 + d);
    });
  });
  return calendar;
};
var getCalendarIndex = exports.getCalendarIndex = function getCalendarIndex(now, calendar) {
  var calendarStartDate = calendar.dates[0][0];
  var weekIndex = (0, _dateFns.differenceInCalendarWeeks)(now, calendarStartDate, {
    weekStartsOn: 1
  });
  var dayIndex = (0, _dateFns.differenceInCalendarDays)(now, (0, _dateFns.addWeeks)(calendarStartDate, weekIndex));
  return [weekIndex, dayIndex];
};