{"version":3,"file":"comparators.cjs","sources":["../../src/shared/date/comparators.ts","../../src/date/comparators.ts"],"sourcesContent":["/*\n * Implementation ported from https://github.com/melt-ui/melt-ui/blob/develop/src/lib/internal/helpers/date/utils.ts\n*/\n\nimport type { DateValue, ZonedDateTime } from '@internationalized/date'\nimport { CalendarDate, CalendarDateTime, createCalendar, DateFormatter, Time, toCalendar } from '@internationalized/date'\n\nexport type TimeValue = Time | CalendarDateTime | ZonedDateTime\n\nexport type Granularity = 'day' | 'hour' | 'minute' | 'second'\nexport type TimeGranularity = 'hour' | 'minute' | 'second'\n\ntype GetDefaultDateProps = {\n defaultValue?: DateValue | DateValue[] | undefined\n defaultPlaceholder?: DateValue | undefined\n granularity?: Granularity\n locale?: string\n}\n\n/**\n * A helper function used throughout the various date builders\n * to generate a default `DateValue` using the `defaultValue`,\n * `defaultPlaceholder`, and `granularity` props.\n *\n * It's important to match the `DateValue` type being used\n * elsewhere in the builder, so they behave according to the\n * behavior the user expects based on the props they've provided.\n *\n */\nexport function getDefaultDate(props: GetDefaultDateProps): DateValue {\n const { defaultValue, defaultPlaceholder, granularity = 'day', locale = 'en' } = props\n\n if (Array.isArray(defaultValue) && defaultValue.length)\n return defaultValue.at(-1)!.copy()\n\n if (defaultValue && !Array.isArray(defaultValue))\n return defaultValue.copy()\n\n if (defaultPlaceholder)\n return defaultPlaceholder.copy()\n\n const date = new Date()\n const year = date.getFullYear()\n const month = date.getMonth() + 1\n const day = date.getDate()\n const calendarDateTimeGranularities = ['hour', 'minute', 'second']\n\n const defaultFormatter = new DateFormatter(locale)\n const calendar = createCalendar(defaultFormatter.resolvedOptions().calendar)\n\n if (calendarDateTimeGranularities.includes(granularity ?? 'day'))\n return toCalendar(new CalendarDateTime(year, month, day, 0, 0, 0), calendar)\n\n return toCalendar(new CalendarDate(year, month, day), calendar)\n}\n\ntype GetDefaultTimeProps = {\n defaultValue?: TimeValue | undefined\n defaultPlaceholder?: TimeValue | undefined\n}\n\nexport function getDefaultTime(props: GetDefaultTimeProps): TimeValue {\n const { defaultValue, defaultPlaceholder } = props\n\n if (defaultValue) {\n return defaultValue.copy()\n }\n\n if (defaultPlaceholder) {\n return defaultPlaceholder.copy()\n }\n\n return new Time(0, 0, 0)\n}\n","import type { DateValue } from '@internationalized/date'\nimport type { Matcher } from './types'\nimport { CalendarDateTime, getDayOfWeek, getLocalTimeZone, parseDate, parseDateTime, parseZonedDateTime, toCalendar, ZonedDateTime } from '@internationalized/date'\n\n/**\n * Given a date string and a reference `DateValue` object, parse the\n * string to the same type as the reference object.\n *\n * Useful for parsing strings from data attributes, which are always\n * strings, to the same type being used by the date component.\n */\nexport function parseStringToDateValue(dateStr: string, referenceVal: DateValue): DateValue {\n let dateValue: DateValue\n if (isZonedDateTime(referenceVal))\n dateValue = parseZonedDateTime(dateStr)\n\n else if (isCalendarDateTime(referenceVal))\n dateValue = parseDateTime(dateStr)\n\n else\n dateValue = parseDate(dateStr)\n\n return dateValue.calendar !== referenceVal.calendar ? toCalendar(dateValue, referenceVal.calendar) : dateValue\n}\n\n/**\n * Given a `DateValue` object, convert it to a native `Date` object.\n * If a timezone is provided, the date will be converted to that timezone.\n * If no timezone is provided, the date will be converted to the local timezone.\n */\nexport function toDate(dateValue: DateValue, tz: string = getLocalTimeZone()) {\n if (isZonedDateTime(dateValue))\n return dateValue.toDate()\n else\n return dateValue.toDate(tz)\n}\n\nexport function isCalendarDateTime(dateValue: DateValue): dateValue is CalendarDateTime {\n return dateValue instanceof CalendarDateTime\n}\n\nexport function isZonedDateTime(dateValue: DateValue): dateValue is ZonedDateTime {\n return dateValue instanceof ZonedDateTime\n}\n\nexport function hasTime(dateValue: DateValue) {\n return isCalendarDateTime(dateValue) || isZonedDateTime(dateValue)\n}\n\n/**\n * Given a date, return the number of days in the month.\n */\nexport function getDaysInMonth(date: Date | DateValue) {\n if (date instanceof Date) {\n const year = date.getFullYear()\n const month = date.getMonth() + 1\n /**\n * By using zero as the day, we get the\n * last day of the previous month, which\n * is the month we originally passed in.\n */\n return new Date(year, month, 0).getDate()\n }\n else {\n return date.set({ day: 100 }).day\n }\n}\n\n/**\n * Determine if a date is before the reference date.\n * @param dateToCompare - is this date before the `referenceDate`\n * @param referenceDate - is the `dateToCompare` before this date\n *\n * @see {@link isBeforeOrSame} for inclusive\n */\nexport function isBefore(dateToCompare: DateValue, referenceDate: DateValue) {\n return dateToCompare.compare(referenceDate) < 0\n}\n\n/**\n * Determine if a date is after the reference date.\n * @param dateToCompare - is this date after the `referenceDate`\n * @param referenceDate - is the `dateToCompare` after this date\n *\n * @see {@link isAfterOrSame} for inclusive\n */\nexport function isAfter(dateToCompare: DateValue, referenceDate: DateValue) {\n return dateToCompare.compare(referenceDate) > 0\n}\n\n/**\n * Determine if a date is before or the same as the reference date.\n *\n * @param dateToCompare - the date to compare\n * @param referenceDate - the reference date to make the comparison against\n *\n * @see {@link isBefore} for non-inclusive\n */\nexport function isBeforeOrSame(dateToCompare: DateValue, referenceDate: DateValue) {\n return dateToCompare.compare(referenceDate) <= 0\n}\n\n/**\n * Determine if a date is after or the same as the reference date.\n *\n * @param dateToCompare - is this date after or the same as the `referenceDate`\n * @param referenceDate - is the `dateToCompare` after or the same as this date\n *\n * @see {@link isAfter} for non-inclusive\n */\nexport function isAfterOrSame(dateToCompare: DateValue, referenceDate: DateValue) {\n return dateToCompare.compare(referenceDate) >= 0\n}\n\n/**\n * Determine if a date is inclusively between a start and end reference date.\n *\n * @param date - is this date inclusively between the `start` and `end` dates\n * @param start - the start reference date to make the comparison against\n * @param end - the end reference date to make the comparison against\n *\n * @see {@link isBetween} for non-inclusive\n */\nexport function isBetweenInclusive(date: DateValue, start: DateValue, end: DateValue) {\n return isAfterOrSame(date, start) && isBeforeOrSame(date, end)\n}\n\n/**\n * Determine if a date is between a start and end reference date.\n *\n * @param date - is this date between the `start` and `end` dates\n * @param start - the start reference date to make the comparison against\n * @param end - the end reference date to make the comparison against\n *\n * @see {@link isBetweenInclusive} for inclusive\n */\nexport function isBetween(date: DateValue, start: DateValue, end: DateValue) {\n return isAfter(date, start) && isBefore(date, end)\n}\n\nexport function getLastFirstDayOfWeek(\n date: T,\n firstDayOfWeek: number,\n locale: string,\n): T {\n const day = getDayOfWeek(date, locale)\n\n if (firstDayOfWeek > day)\n return date.subtract({ days: day + 7 - firstDayOfWeek }) as T\n\n if (firstDayOfWeek === day)\n return date as T\n\n return date.subtract({ days: day - firstDayOfWeek }) as T\n}\n\nexport function getNextLastDayOfWeek(\n date: T,\n firstDayOfWeek: number,\n locale: string,\n): T {\n const day = getDayOfWeek(date, locale)\n const lastDayOfWeek = firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1\n\n if (day === lastDayOfWeek)\n return date as T\n\n if (day > lastDayOfWeek)\n return date.add({ days: 7 - day + lastDayOfWeek }) as T\n\n return date.add({ days: lastDayOfWeek - day }) as T\n}\n\nexport function areAllDaysBetweenValid(\n start: DateValue,\n end: DateValue,\n isUnavailable: Matcher | undefined,\n isDisabled: Matcher | undefined,\n isHighlightable?: Matcher | undefined,\n) {\n if (isUnavailable === undefined && isDisabled === undefined && isHighlightable === undefined)\n return true\n\n let dCurrent = start.add({ days: 1 })\n if ((isDisabled?.(dCurrent) || isUnavailable?.(dCurrent))\n && !isHighlightable?.(dCurrent)) {\n return false\n }\n\n const dEnd = end\n while (dCurrent.compare(dEnd) < 0) {\n dCurrent = dCurrent.add({ days: 1 })\n if ((isDisabled?.(dCurrent) || isUnavailable?.(dCurrent))\n && !isHighlightable?.(dCurrent)) {\n return false\n }\n }\n return true\n}\n"],"names":["date","DateFormatter","createCalendar","toCalendar","CalendarDateTime","CalendarDate","Time","parseZonedDateTime","parseDateTime","parseDate","getLocalTimeZone","ZonedDateTime","getDayOfWeek"],"mappings":";;;;AA6BO,SAAS,eAAe,KAAuC,EAAA;AACpE,EAAA,MAAM,EAAE,YAAc,EAAA,kBAAA,EAAoB,cAAc,KAAO,EAAA,MAAA,GAAS,MAAS,GAAA,KAAA;AAEjF,EAAA,IAAI,KAAM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