{"version":3,"file":"index.cjs","names":["capitalizeFirstLetter"],"sources":["../../../common/dates/index.js"],"sourcesContent":["import {\n  format,\n  formatDuration,\n  intervalToDuration,\n  formatDistance,\n  isToday,\n  isYesterday,\n  isThisWeek,\n  isThisYear,\n} from 'date-fns';\nimport { capitalizeFirstLetter } from '../utils';\n\nlet dialtoneLocale;\n\n// Base functions just wrap core date-fns functions, but this allows us to do checks and set default options.\n\nfunction _baseFormat (date, formatString) {\n  _checkLocaleSet();\n  return format(date, formatString, {\n    locale: dialtoneLocale,\n  });\n}\n\nfunction _baseFormatDuration (duration, formatString) {\n  _checkLocaleSet();\n  return formatDuration(duration, {\n    locale: dialtoneLocale,\n    format: formatString,\n  });\n}\n\nfunction _baseFormatDistance (date, baseDate) {\n  _checkLocaleSet();\n  return formatDistance(date, baseDate, {\n    locale: dialtoneLocale,\n  });\n}\n\nfunction _isLocaleSet () {\n  return dialtoneLocale !== undefined;\n}\n\nfunction _checkLocaleSet () {\n  if (!_isLocaleSet()) {\n    throw new Error('Locale not set, please call setDateLocale(locale) and pass ' +\n    'in a datefns locale object as the locale param before calling this function');\n  }\n}\n\n/**\n * Sets the locale for date-fns. This should be called before any date-fns functions are called.\n * @param {Locale} locale A date-fns locale object\n */\nexport function setDateLocale (locale) {\n  dialtoneLocale = locale;\n}\n\n/**\n * This formats a date to the Dialtone standard medium date format as shown here:\n * https://dialtone.dialpad.com/guides/writing-guidelines/#formats-by-length\n * @param {Date} date A javascript date object\n * @returns {string} A string in the format of 'September 2, 2022'\n */\nexport function getDateMedium (date) {\n  return _baseFormat(date, 'MMMM d, y');\n}\n\n/**\n * Converts a call duration in total number of seconds to a human readable string\n * such as 'less than a minute' or '4 hours 34 minutes'.\n * @param {number} durationInSeconds The duration of the call in seconds\n * @returns {string} A human readable string representing the duration of the call\n */\nexport function durationInHHMM (durationInSeconds) {\n  if (durationInSeconds < 60) {\n    // returns 'less than a minute', we're doing it like this instead of returning a string\n    // so datefns handles i18n.\n    return _baseFormatDistance(0, 29 * 1000);\n  }\n  const duration = intervalToDuration({\n    start: 0,\n    end: durationInSeconds * 1000,\n  });\n  return _baseFormatDuration(duration, ['hours', 'minutes']);\n}\n\n/**\n * gets the human readable name of the day relative to the current time. For example, if you pass in -1 it will\n * say \"Yesterday\" if you pass in 0 it will say \"Today\", if you pass in 1 it will say \"Tomorrow\".\n * @param {number} days The number of days relative to the current time\n * @returns {string} A human readable string representing the distance between the date and now\n */\nfunction _getRelativeDaysText (days) {\n  const rtl = new Intl.RelativeTimeFormat(dialtoneLocale.code, { numeric: 'auto' });\n  return capitalizeFirstLetter(rtl.formatToParts(days, 'day')[0].value, dialtoneLocale.code);\n}\n\n/**\n * Returns the distance between the passed in date and now in a human readable format, typically used\n * when showing a history of items in a log such as a feed list.\n *\n * datefns does not support 'today' and 'yesterday' without showing time so we use Intl for these cases.\n *\n * examples below to explain\n * the different potential formats:\n *\n * If current day:\n * Today\n *\n * If previous day:\n * Yesterday\n *\n * Older than yesterday, but in the same calendar week:\n * Monday\n *\n * Older than the most recent calendar week, but in the same year:\n * Monday, October 14\n *\n * older than a calendar year:\n * October 14, 2022\n *\n *\n * @param {Date} date The timestamp of the item's date\n * @returns {string} A human readable string representing the distance between the date and now\n */\nexport function relativeDate (date) {\n  if (isToday(date)) {\n    return _getRelativeDaysText(0);\n  } else if (isYesterday(date)) {\n    return _getRelativeDaysText(-1);\n  } else if (isThisWeek(date)) {\n    return _baseFormat(date, 'EEEE');\n  } else if (isThisYear(date)) {\n    return _baseFormat(date, 'EEEE, MMMM d');\n  } else {\n    return _baseFormat(date, 'MMMM d, y');\n  }\n}\n"],"mappings":"uKAYA,IAAI,EAIJ,SAAS,EAAa,EAAM,EAAc,CAExC,OADA,GAAiB,EACjB,EAAA,EAAA,QAAc,EAAM,EAAc,CAChC,OAAQ,EACT,CAAC,CAGJ,SAAS,EAAqB,EAAU,EAAc,CAEpD,OADA,GAAiB,EACjB,EAAA,EAAA,gBAAsB,EAAU,CAC9B,OAAQ,EAC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