function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
// Date utility functions
import { CALENDAR_GREGORY } from '../constants/date';
import { RX_DATE, RX_DATE_SPLIT } from '../constants/regex';
import { concat } from './array';
import { identity } from './identity';
import { isDate, isString } from './inspect';
import { toInteger } from './number';

// --- Date utility methods ---

// Create or clone a date (`new Date(...)` shortcut)
export var createDate = function createDate() {
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key];
  }
  return _construct(Date, args);
};

// Parse a date sting, or Date object, into a Date object (with no time information)
export var parseYMD = function parseYMD(date) {
  if (isString(date) && RX_DATE.test(date.trim())) {
    var _date$split$map = date.split(RX_DATE_SPLIT).map(function (v) {
        return toInteger(v, 1);
      }),
      _date$split$map2 = _slicedToArray(_date$split$map, 3),
      year = _date$split$map2[0],
      month = _date$split$map2[1],
      day = _date$split$map2[2];
    return createDate(year, month - 1, day);
  } else if (isDate(date)) {
    return createDate(date.getFullYear(), date.getMonth(), date.getDate());
  }
  return null;
};

// Format a date object as `YYYY-MM-DD` format
export var formatYMD = function formatYMD(date) {
  date = parseYMD(date);
  if (!date) {
    return null;
  }
  var year = date.getFullYear();
  var month = "0".concat(date.getMonth() + 1).slice(-2);
  var day = "0".concat(date.getDate()).slice(-2);
  return "".concat(year, "-").concat(month, "-").concat(day);
};

// Given a locale (or locales), resolve the browser available locale
export var resolveLocale = function resolveLocale(locales) /* istanbul ignore next */{
  var calendar = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : CALENDAR_GREGORY;
  locales = concat(locales).filter(identity);
  var fmt = new Intl.DateTimeFormat(locales, {
    calendar: calendar
  });
  return fmt.resolvedOptions().locale;
};

// Create a `Intl.DateTimeFormat` formatter function
export var createDateFormatter = function createDateFormatter(locale, options) /* istanbul ignore next */{
  var dtf = new Intl.DateTimeFormat(locale, options);
  return dtf.format;
};

// Determine if two dates are the same date (ignoring time portion)
export var datesEqual = function datesEqual(date1, date2) {
  // Returns true of the date portion of two date objects are equal
  // We don't compare the time portion
  return formatYMD(date1) === formatYMD(date2);
};

// --- Date "math" utility methods (for BCalendar component mainly) ---

export var firstDateOfMonth = function firstDateOfMonth(date) {
  date = createDate(date);
  date.setDate(1);
  return date;
};
export var lastDateOfMonth = function lastDateOfMonth(date) {
  date = createDate(date);
  date.setMonth(date.getMonth() + 1);
  date.setDate(0);
  return date;
};
export var addYears = function addYears(date, numberOfYears) {
  date = createDate(date);
  var month = date.getMonth();
  date.setFullYear(date.getFullYear() + numberOfYears);
  // Handle Feb 29th for leap years
  if (date.getMonth() !== month) {
    date.setDate(0);
  }
  return date;
};
export var oneMonthAgo = function oneMonthAgo(date) {
  date = createDate(date);
  var month = date.getMonth();
  date.setMonth(month - 1);
  // Handle when days in month are different
  if (date.getMonth() === month) {
    date.setDate(0);
  }
  return date;
};
export var oneMonthAhead = function oneMonthAhead(date) {
  date = createDate(date);
  var month = date.getMonth();
  date.setMonth(month + 1);
  // Handle when days in month are different
  if (date.getMonth() === (month + 2) % 12) {
    date.setDate(0);
  }
  return date;
};
export var oneYearAgo = function oneYearAgo(date) {
  return addYears(date, -1);
};
export var oneYearAhead = function oneYearAhead(date) {
  return addYears(date, 1);
};
export var oneDecadeAgo = function oneDecadeAgo(date) {
  return addYears(date, -10);
};
export var oneDecadeAhead = function oneDecadeAhead(date) {
  return addYears(date, 10);
};

// Helper function to constrain a date between two values
// Always returns a `Date` object or `null` if no date passed
export var constrainDate = function constrainDate(date) {
  var min = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  var max = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  // Ensure values are `Date` objects (or `null`)
  date = parseYMD(date);
  min = parseYMD(min) || date;
  max = parseYMD(max) || date;
  // Return a new `Date` object (or `null`)
  return date ? date < min ? min : date > max ? max : date : null;
};