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; }
import format from 'date-fns/format';
import isAfter from 'date-fns/isAfter';
import isValidDate from 'date-fns/isValid';
import addDays from 'date-fns/addDays';
import toDate from 'date-fns/toDate';
import parseISO from 'date-fns/parseISO';
import startOfWeek from 'date-fns/startOfWeek';
import svLocale from 'date-fns/locale/sv';
import enLocale from 'date-fns/locale/en-US';
import nbLocale from 'date-fns/locale/nb';
import daLocale from 'date-fns/locale/da';
import fiLocale from 'date-fns/locale/fi';
import { addWeeks, differenceInCalendarDays, differenceInCalendarWeeks, isMatch, parse } from 'date-fns';
import { capitalize } from './textUtils';
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: svLocale,
  nb: nbLocale,
  da: daLocale,
  fi: fiLocale,
  en: enLocale
};
export var 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;
};
export var 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;
};
export var isValid = function isValid(date) {
  return isValidDate(date) && isAfter(date, new Date('1/1/1000'));
};
function isString(value) {
  return typeof value === 'string' || value instanceof String;
}
export var newDate = function newDate() {
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date();
  var d = isString(value) ? parseISO(value) : toDate(value);
  return isValid(d) ? d : new Date();
};
export var parseDateString = function parseDateString(dateString, locale) {
  if (!isMatch(dateString, getDateFormat(locale), {
    locale: getLocale(locale)
  })) return null;
  var date = parse(dateString, getDateFormat(locale), newDate());
  if (!isValid(date)) return null;
  return date;
};
export var 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];
};
export var getCalendar = function getCalendar(now, opts) {
  var _ref, _ref2;
  var calendar = {
    weekDays: [],
    dates: []
  };
  var firstCalDay = startOfWeek(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 capitalize(format(addDays(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 addDays(firstCalDay, w * 7 + d);
    });
  });
  return calendar;
};
export var getCalendarIndex = function getCalendarIndex(now, calendar) {
  var calendarStartDate = calendar.dates[0][0];
  var weekIndex = differenceInCalendarWeeks(now, calendarStartDate, {
    weekStartsOn: 1
  });
  var dayIndex = differenceInCalendarDays(now, addWeeks(calendarStartDate, weekIndex));
  return [weekIndex, dayIndex];
};