import { CALENDAR_GREGORY } from '../constants/date';
import { RX_DATE, RX_DATE_SPLIT } from '../constants/regex';
import { concat } from './array';
import { identity } from './identity';
import { isString, isDate } from './inspect';
import { toInteger } from './number';

// Date utility functions

// --- Date utility methods ---

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

// Parse a date sting, or Date object, into a Date object (with no time information)
const parseYMD = date => {
  if (isString(date) && RX_DATE.test(date.trim())) {
    const [year, month, day] = date.split(RX_DATE_SPLIT).map(v => toInteger(v, 1));
    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
const formatYMD = date => {
  date = parseYMD(date);
  if (!date) {
    return null;
  }
  const year = date.getFullYear();
  const month = `0${date.getMonth() + 1}`.slice(-2);
  const day = `0${date.getDate()}`.slice(-2);
  return `${year}-${month}-${day}`;
};

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

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

// Determine if two dates are the same date (ignoring time portion)
const 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) ---

const firstDateOfMonth = date => {
  date = createDate(date);
  date.setDate(1);
  return date;
};
const lastDateOfMonth = date => {
  date = createDate(date);
  date.setMonth(date.getMonth() + 1);
  date.setDate(0);
  return date;
};
const addYears = (date, numberOfYears) => {
  date = createDate(date);
  const month = date.getMonth();
  date.setFullYear(date.getFullYear() + numberOfYears);
  // Handle Feb 29th for leap years
  if (date.getMonth() !== month) {
    date.setDate(0);
  }
  return date;
};
const oneMonthAgo = date => {
  date = createDate(date);
  const month = date.getMonth();
  date.setMonth(month - 1);
  // Handle when days in month are different
  if (date.getMonth() === month) {
    date.setDate(0);
  }
  return date;
};
const oneMonthAhead = date => {
  date = createDate(date);
  const 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;
};
const oneYearAgo = date => {
  return addYears(date, -1);
};
const oneYearAhead = date => {
  return addYears(date, 1);
};
const oneDecadeAgo = date => {
  return addYears(date, -10);
};
const 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
const constrainDate = function (date) {
  let min = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  let 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;
};

export { addYears, constrainDate, createDate, createDateFormatter, datesEqual, firstDateOfMonth, formatYMD, lastDateOfMonth, oneDecadeAgo, oneDecadeAhead, oneMonthAgo, oneMonthAhead, oneYearAgo, oneYearAhead, parseYMD, resolveLocale };
