"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.todayTimestampInUTC = exports.timestampToUTCDate = exports.timestampToTaskContext = exports.timestampToString = exports.timestampToIsoFormat = exports.isPastDate = void 0;
var _differenceInCalendarDays = _interopRequireDefault(require("date-fns/differenceInCalendarDays"));
var _isBefore = _interopRequireDefault(require("date-fns/isBefore"));
var FORMATS = /*#__PURE__*/function (FORMATS) {
  FORMATS["ISO_FORMAT"] = "YYYY-MM-DD";
  FORMATS["LOCALIZED_FORMAT"] = "LOCALIZED_FORMAT";
  return FORMATS;
}(FORMATS || {});
var timestampToUTCDate = exports.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
  };
};
var todayTimestampInUTC = exports.todayTimestampInUTC = function todayTimestampInUTC() {
  var today = new Date(Date.now());
  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"
var timestampToString = exports.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"
var timestampToIsoFormat = exports.timestampToIsoFormat = function timestampToIsoFormat(timestamp) {
  var date = new Date(Number(timestamp));
  return "".concat(date.getUTCFullYear(), "-").concat(addLeadingZero(date.getUTCMonth() + 1), "-").concat(date.getUTCDate());
};
var isPastDate = exports.isPastDate = function isPastDate(timestamp) {
  return (0, _isBefore.default)(new Date(Number(timestamp)), new Date(Number(todayTimestampInUTC())));
};
var timestampToTaskContext = exports.timestampToTaskContext = function timestampToTaskContext(timestamp, intl) {
  var curDate = new Date(Number(todayTimestampInUTC()));
  var givenDate = new Date(Number(timestamp));
  var distance = (0, _differenceInCalendarDays.default)(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);
};