import differenceInCalendarDays from 'date-fns/differenceInCalendarDays';
import isBefore from 'date-fns/isBefore';
var FORMATS = /*#__PURE__*/function (FORMATS) {
  FORMATS["ISO_FORMAT"] = "YYYY-MM-DD";
  FORMATS["LOCALIZED_FORMAT"] = "LOCALIZED_FORMAT";
  return FORMATS;
}(FORMATS || {});
export var timestampToUTCDate = function timestampToUTCDate(timestamp) {
  var date = new Date(Number(timestamp));
  var day = date.getUTCDate();
  var month = date.getUTCMonth() + 1;
  var year = date.getUTCFullYear();
  return {
    day: day,
    month: month,
    year: year
  };
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var todayTimestampInUTC = function todayTimestampInUTC(timeZone) {
  var today = new Date(Date.now());
  if (timeZone) {
    var _parts$find, _parts$find2, _parts$find3;
    var parts = new Intl.DateTimeFormat('en-US', {
      timeZone: timeZone,
      year: 'numeric',
      month: 'numeric',
      day: 'numeric'
    }).formatToParts(today);
    var year = Number((_parts$find = parts.find(function (p) {
      return p.type === 'year';
    })) === null || _parts$find === void 0 ? void 0 : _parts$find.value);
    var month = Number((_parts$find2 = parts.find(function (p) {
      return p.type === 'month';
    })) === null || _parts$find2 === void 0 ? void 0 : _parts$find2.value) - 1;
    var day = Number((_parts$find3 = parts.find(function (p) {
      return p.type === 'day';
    })) === null || _parts$find3 === void 0 ? void 0 : _parts$find3.value);
    return Date.UTC(year, month, day).toString();
  }
  var todayInUTC = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate());
  return todayInUTC.toString();
};
var addLeadingZero = function addLeadingZero(val) {
  if (val < 10) {
    return "0".concat(val);
  }
  return val;
};
var capitalizeFirstLetter = function capitalizeFirstLetter(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
};

// example: "23 Jan 2018"
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var timestampToString = function timestampToString(timestamp, intl, pattern) {
  if (!intl || pattern === FORMATS.ISO_FORMAT) {
    return timestampToIsoFormat(timestamp);
  }
  var date = new Date(Number(timestamp));
  return intl.formatDate(date, {
    timeZone: 'UTC',
    month: 'short',
    day: 'numeric',
    year: 'numeric',
    formatMatcher: 'best fit'
  });
};

// example: "2018-01-23"
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var timestampToIsoFormat = function timestampToIsoFormat(timestamp) {
  var date = new Date(Number(timestamp));
  return "".concat(date.getUTCFullYear(), "-").concat(addLeadingZero(date.getUTCMonth() + 1), "-").concat(addLeadingZero(date.getUTCDate()));
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var isPastDate = function isPastDate(timestamp, timeZone) {
  return isBefore(new Date(Number(timestamp)), new Date(Number(todayTimestampInUTC(timeZone))));
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var timestampToTaskContext = function timestampToTaskContext(timestamp, intl, timeZone) {
  var curDate = new Date(Number(todayTimestampInUTC(timeZone)));
  var givenDate = new Date(Number(timestamp));
  var distance = differenceInCalendarDays(givenDate, curDate);
  if (intl && [-1, 0, 1].indexOf(distance) > -1) {
    return capitalizeFirstLetter(intl.formatRelativeTime(distance, 'day', {
      numeric: 'auto'
    }));
  }
  return timestampToString(timestamp, intl, FORMATS.LOCALIZED_FORMAT);
};