{"ast":null,"code":"import { secondsInDay, secondsInHour, secondsInMinute, secondsInMonth, secondsInQuarter, secondsInWeek, secondsInYear } from \"./constants.mjs\";\nimport { differenceInCalendarDays } from \"./differenceInCalendarDays.mjs\";\nimport { differenceInCalendarMonths } from \"./differenceInCalendarMonths.mjs\";\nimport { differenceInCalendarQuarters } from \"./differenceInCalendarQuarters.mjs\";\nimport { differenceInCalendarWeeks } from \"./differenceInCalendarWeeks.mjs\";\nimport { differenceInCalendarYears } from \"./differenceInCalendarYears.mjs\";\nimport { differenceInHours } from \"./differenceInHours.mjs\";\nimport { differenceInMinutes } from \"./differenceInMinutes.mjs\";\nimport { differenceInSeconds } from \"./differenceInSeconds.mjs\";\nimport { toDate } from \"./toDate.mjs\";\n\n/**\n * The {@link intlFormatDistance} function options.\n */\n\n/**\n * The unit used to format the distance in {@link intlFormatDistance}.\n */\n\n/**\n * @name intlFormatDistance\n * @category Common Helpers\n * @summary Formats distance between two dates in a human-readable format\n * @description\n * The function calculates the difference between two dates and formats it as a human-readable string.\n *\n * The function will pick the most appropriate unit depending on the distance between dates. For example, if the distance is a few hours, it might return `x hours`. If the distance is a few months, it might return `x months`.\n *\n * You can also specify a unit to force using it regardless of the distance to get a result like `123456 hours`.\n *\n * See the table below for the unit picking logic:\n *\n * | Distance between dates | Result (past)  | Result (future) |\n * | ---------------------- | -------------- | --------------- |\n * | 0 seconds              | now            | now             |\n * | 1-59 seconds           | X seconds ago  | in X seconds    |\n * | 1-59 minutes           | X minutes ago  | in X minutes    |\n * | 1-23 hours             | X hours ago    | in X hours      |\n * | 1 day                  | yesterday      | tomorrow        |\n * | 2-6 days               | X days ago     | in X days       |\n * | 7 days                 | last week      | next week       |\n * | 8 days-1 month         | X weeks ago    | in X weeks      |\n * | 1 month                | last month     | next month      |\n * | 2-3 months             | X months ago   | in X months     |\n * | 1 quarter              | last quarter   | next quarter    |\n * | 2-3 quarters           | X quarters ago | in X quarters   |\n * | 1 year                 | last year      | next year       |\n * | 2+ years               | X years ago    | in X years      |\n *\n * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).\n *\n * @param date - The date\n * @param baseDate - The date to compare with.\n * @param options - An object with options.\n * See MDN for details [Locale identification and negotiation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation)\n * The narrow one could be similar to the short one for some locales.\n *\n * @returns The distance in words according to language-sensitive relative time formatting.\n *\n * @throws `date` must not be Invalid Date\n * @throws `baseDate` must not be Invalid Date\n * @throws `options.unit` must not be invalid Unit\n * @throws `options.locale` must not be invalid locale\n * @throws `options.localeMatcher` must not be invalid localeMatcher\n * @throws `options.numeric` must not be invalid numeric\n * @throws `options.style` must not be invalid style\n *\n * @example\n * // What is the distance between the dates when the fist date is after the second?\n * intlFormatDistance(\n *   new Date(1986, 3, 4, 11, 30, 0),\n *   new Date(1986, 3, 4, 10, 30, 0)\n * )\n * //=> 'in 1 hour'\n *\n * // What is the distance between the dates when the fist date is before the second?\n * intlFormatDistance(\n *   new Date(1986, 3, 4, 10, 30, 0),\n *   new Date(1986, 3, 4, 11, 30, 0)\n * )\n * //=> '1 hour ago'\n *\n * @example\n * // Use the unit option to force the function to output the result in quarters. Without setting it, the example would return \"next year\"\n * intlFormatDistance(\n *   new Date(1987, 6, 4, 10, 30, 0),\n *   new Date(1986, 3, 4, 10, 30, 0),\n *   { unit: 'quarter' }\n * )\n * //=> 'in 5 quarters'\n *\n * @example\n * // Use the locale option to get the result in Spanish. Without setting it, the example would return \"in 1 hour\".\n * intlFormatDistance(\n *   new Date(1986, 3, 4, 11, 30, 0),\n *   new Date(1986, 3, 4, 10, 30, 0),\n *   { locale: 'es' }\n * )\n * //=> 'dentro de 1 hora'\n *\n * @example\n * // Use the numeric option to force the function to use numeric values. Without setting it, the example would return \"tomorrow\".\n * intlFormatDistance(\n *   new Date(1986, 3, 5, 11, 30, 0),\n *   new Date(1986, 3, 4, 11, 30, 0),\n *   { numeric: 'always' }\n * )\n * //=> 'in 1 day'\n *\n * @example\n * // Use the style option to force the function to use short values. Without setting it, the example would return \"in 2 years\".\n * intlFormatDistance(\n *   new Date(1988, 3, 4, 11, 30, 0),\n *   new Date(1986, 3, 4, 11, 30, 0),\n *   { style: 'short' }\n * )\n * //=> 'in 2 yr'\n */\nexport function intlFormatDistance(date, baseDate, options) {\n  let value = 0;\n  let unit;\n  const dateLeft = toDate(date);\n  const dateRight = toDate(baseDate);\n  if (!options?.unit) {\n    // Get the unit based on diffInSeconds calculations if no unit is specified\n    const diffInSeconds = differenceInSeconds(dateLeft, dateRight); // The smallest unit\n\n    if (Math.abs(diffInSeconds) < secondsInMinute) {\n      value = differenceInSeconds(dateLeft, dateRight);\n      unit = \"second\";\n    } else if (Math.abs(diffInSeconds) < secondsInHour) {\n      value = differenceInMinutes(dateLeft, dateRight);\n      unit = \"minute\";\n    } else if (Math.abs(diffInSeconds) < secondsInDay && Math.abs(differenceInCalendarDays(dateLeft, dateRight)) < 1) {\n      value = differenceInHours(dateLeft, dateRight);\n      unit = \"hour\";\n    } else if (Math.abs(diffInSeconds) < secondsInWeek && (value = differenceInCalendarDays(dateLeft, dateRight)) && Math.abs(value) < 7) {\n      unit = \"day\";\n    } else if (Math.abs(diffInSeconds) < secondsInMonth) {\n      value = differenceInCalendarWeeks(dateLeft, dateRight);\n      unit = \"week\";\n    } else if (Math.abs(diffInSeconds) < secondsInQuarter) {\n      value = differenceInCalendarMonths(dateLeft, dateRight);\n      unit = \"month\";\n    } else if (Math.abs(diffInSeconds) < secondsInYear) {\n      if (differenceInCalendarQuarters(dateLeft, dateRight) < 4) {\n        // To filter out cases that are less than a year but match 4 quarters\n        value = differenceInCalendarQuarters(dateLeft, dateRight);\n        unit = \"quarter\";\n      } else {\n        value = differenceInCalendarYears(dateLeft, dateRight);\n        unit = \"year\";\n      }\n    } else {\n      value = differenceInCalendarYears(dateLeft, dateRight);\n      unit = \"year\";\n    }\n  } else {\n    // Get the value if unit is specified\n    unit = options?.unit;\n    if (unit === \"second\") {\n      value = differenceInSeconds(dateLeft, dateRight);\n    } else if (unit === \"minute\") {\n      value = differenceInMinutes(dateLeft, dateRight);\n    } else if (unit === \"hour\") {\n      value = differenceInHours(dateLeft, dateRight);\n    } else if (unit === \"day\") {\n      value = differenceInCalendarDays(dateLeft, dateRight);\n    } else if (unit === \"week\") {\n      value = differenceInCalendarWeeks(dateLeft, dateRight);\n    } else if (unit === \"month\") {\n      value = differenceInCalendarMonths(dateLeft, dateRight);\n    } else if (unit === \"quarter\") {\n      value = differenceInCalendarQuarters(dateLeft, dateRight);\n    } else if (unit === \"year\") {\n      value = differenceInCalendarYears(dateLeft, dateRight);\n    }\n  }\n  const rtf = new Intl.RelativeTimeFormat(options?.locale, {\n    localeMatcher: options?.localeMatcher,\n    numeric: options?.numeric || \"auto\",\n    style: options?.style\n  });\n  return rtf.format(value, unit);\n}\n\n// Fallback for modularized imports:\nexport default intlFormatDistance;","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}