{"version":3,"file":"useDateFormatter.cjs","sources":["../../src/shared/useDateFormatter.ts"],"sourcesContent":["/*\n  * Implementation ported from https://github.com/melt-ui/melt-ui/blob/develop/src/lib/internal/helpers/date/formatter.ts\n*/\n\nimport { DateFormatter, getLocalTimeZone, today } from '@internationalized/date'\nimport type { DateValue, ZonedDateTime } from '@internationalized/date'\nimport { hasTime, isZonedDateTime, toDate } from '@/date'\nimport { ref } from 'vue'\n\nexport interface DateFormatterOptions extends Intl.DateTimeFormatOptions {\n  calendar?: string\n}\n\nexport type Formatter = {\n  getLocale: () => string\n  setLocale: (newLocale: string) => void\n  custom: (date: Date, options: DateFormatterOptions) => string\n  selectedDate: (date: DateValue, includeTime?: boolean) => string\n  dayOfWeek: (date: Date, length?: DateFormatterOptions['weekday']) => string\n  fullMonthAndYear: (date: Date, options?: DateFormatterOptions) => string\n  fullMonth: (date: Date, options?: DateFormatterOptions) => string\n  fullYear: (date: Date, options?: DateFormatterOptions) => string\n  dayPeriod: (date: Date) => string\n  part: (dateObj: DateValue, type: Intl.DateTimeFormatPartTypes, options?: DateFormatterOptions) => string\n  toParts: (date: DateValue, options?: DateFormatterOptions) => Intl.DateTimeFormatPart[]\n  getMonths: () => { label: string, value: number }[]\n}\n\n/**\n * Creates a wrapper around the `DateFormatter`, which is\n * an improved version of the {@link Intl.DateTimeFormat} API,\n * that is used internally by the various date builders to\n * easily format dates in a consistent way.\n *\n * @see [DateFormatter](https://react-spectrum.adobe.com/internationalized/date/DateFormatter.html)\n */\nexport function useDateFormatter(initialLocale: string): Formatter {\n  const locale = ref(initialLocale)\n\n  function getLocale() {\n    return locale.value\n  }\n\n  function setLocale(newLocale: string) {\n    locale.value = newLocale\n  }\n\n  function custom(date: Date, options: DateFormatterOptions) {\n    return new DateFormatter(locale.value, options).format(date)\n  }\n\n  function selectedDate(date: DateValue, includeTime = true) {\n    if (hasTime(date) && includeTime) {\n      return custom(toDate(date), {\n        dateStyle: 'long',\n        timeStyle: 'long',\n      })\n    }\n    else {\n      return custom(toDate(date), {\n        dateStyle: 'long',\n      })\n    }\n  }\n\n  function fullMonthAndYear(date: Date, options: DateFormatterOptions = {}) {\n    return new DateFormatter(locale.value, { month: 'long', year: 'numeric', ...options }).format(date)\n  }\n\n  function fullMonth(date: Date, options: DateFormatterOptions = {}) {\n    return new DateFormatter(locale.value, { month: 'long', ...options }).format(date)\n  }\n\n  function getMonths() {\n    const defaultDate = today(getLocalTimeZone())\n    const months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n    return months.map(item => ({ label: fullMonth(toDate(defaultDate.set({ month: item }))), value: item }))\n  }\n\n  function fullYear(date: Date, options: DateFormatterOptions = {}) {\n    return new DateFormatter(locale.value, { year: 'numeric', ...options }).format(date)\n  }\n\n  function toParts(date: DateValue, options?: DateFormatterOptions) {\n    if (isZonedDateTime(date)) {\n      return new DateFormatter(locale.value, {\n        ...options,\n        timeZone: (date as ZonedDateTime).timeZone,\n      }).formatToParts(toDate(date))\n    }\n    else {\n      return new DateFormatter(locale.value, options).formatToParts(toDate(date))\n    }\n  }\n\n  function dayOfWeek(date: Date, length: DateFormatterOptions['weekday'] = 'narrow') {\n    return new DateFormatter(locale.value, { weekday: length }).format(date)\n  }\n\n  function dayPeriod(date: Date) {\n    const parts = new DateFormatter(locale.value, {\n      hour: 'numeric',\n      minute: 'numeric',\n    }).formatToParts(date)\n    const value = parts.find(p => p.type === 'dayPeriod')?.value\n    if (value === 'PM')\n      return 'PM'\n\n    return 'AM'\n  }\n\n  const defaultPartOptions: DateFormatterOptions = {\n    year: 'numeric',\n    month: 'numeric',\n    day: 'numeric',\n    hour: 'numeric',\n    minute: 'numeric',\n    second: 'numeric',\n  }\n\n  function part(\n    dateObj: DateValue,\n    type: Intl.DateTimeFormatPartTypes,\n    options: DateFormatterOptions = {},\n  ) {\n    const opts = { ...defaultPartOptions, ...options }\n    const parts = toParts(dateObj, opts)\n    const part = parts.find(p => p.type === type)\n    return part ? part.value : ''\n  }\n\n  return {\n    setLocale,\n    getLocale,\n    fullMonth,\n    fullYear,\n    fullMonthAndYear,\n    toParts,\n    custom,\n    part,\n    dayPeriod,\n    selectedDate,\n    dayOfWeek,\n    getMonths,\n  }\n}\n"],"names":["ref","date","DateFormatter","hasTime","toDate","today","getLocalTimeZone","isZonedDateTime","part"],"mappings":";;;;;;AAoCO,SAAS,iBAAiB,aAAkC,EAAA;AACjE,EAAM,MAAA,MAAA,GAASA,QAAI,aAAa,CAAA;AAEhC,EAAA,SAAS,SAAY,GAAA;AACnB,IAAA,OAAO,MAAO,CAAA,KAAA;AAAA;AAGhB,EAAA,SAAS,UAAU,SAAmB,EAAA;AACpC,IAAA,MAAA,CAAO,KAAQ,GAAA,SAAA;AAAA;AAGjB,EAAS,SAAA,MAAA,CAAOC,QAAY,OAA+B,EAAA;AACzD,IAAA,OAAO,IAAIC,kBAAc,CAAA,MAAA,CAAO,OAAO,OAAO,CAAA,CAAE,OAAOD,MAAI,CAAA;AAAA;AAG7D,EAAS,SAAA,YAAA,CAAa,IAAiB,EAAA,WAAA,GAAc,IAAM,EAAA;AACzD,IAAI,IAAAE,wBAAA,CAAQ,IAAI,CAAA,IAAK,WAAa,EAAA;AAChC,MAAO,OAAA,MAAA,CAAOC,uBAAO,CAAA,IAAI,CAAG,EAAA;AAAA,QAC1B,SAAW,EAAA,MAAA;AAAA,QACX,SAAW,EAAA;AAAA,OACZ,CAAA;AAAA,KAEE,MAAA;AACH,MAAO,OAAA,MAAA,CAAOA,uBAAO,CAAA,IAAI,CAAG,EAAA;AAAA,QAC1B,SAAW,EAAA;AAAA,OACZ,CAAA;AAAA;AACH;AAGF,EAAA,SAAS,gBAAiB,CAAAH,MAAA,EAAY,OAAgC,GAAA,EAAI,EAAA;AACxE,IAAA,OAAO,IAAIC,kBAAA,CAAc,MAAO,CAAA,KAAA,EAAO,EAAE,KAAO,EAAA,MAAA,EAAQ,IAAM,EAAA,SAAA,EAAW,GAAG,OAAA,EAAS,CAAA,CAAE,OAAOD,MA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