{"version":3,"sources":["../../../../../src/components/Calendar/utils/dateGrid/dateAvailability.ts"],"sourcesContent":["import { addDays, compareDatePart } from '../dateMath';\nimport type { AvailableDateOptions, RestrictedDatesOptions } from './dateGrid.types';\n\n/**\n * Checks if a date is after the maximum allowed date based on the given restrictions.\n */\nconst isAfterMaxDate = (date: Date, options: RestrictedDatesOptions): boolean => {\n  const { maxDate } = options;\n  return maxDate ? compareDatePart(date, maxDate) >= 1 : false;\n};\n\n/**\n * Checks if a date is before the minimum allowed date based on the given restrictions.\n */\nconst isBeforeMinDate = (date: Date, options: RestrictedDatesOptions): boolean => {\n  const { minDate } = options;\n  return minDate ? compareDatePart(minDate, date) >= 1 : false;\n};\n\n/**\n * Checks if `date` falls into the restricted `options`\n * @param date - date to check\n * @param options - restriction options (min date, max date and list of restricted dates)\n */\nexport const isRestrictedDate = (date: Date, options: RestrictedDatesOptions): boolean => {\n  const { restrictedDates, minDate, maxDate } = options;\n  if (!restrictedDates && !minDate && !maxDate) {\n    return false;\n  }\n  const inRestrictedDates = restrictedDates && restrictedDates.some((rd: Date) => compareDatePart(rd, date) === 0);\n  return inRestrictedDates || isBeforeMinDate(date, options) || isAfterMaxDate(date, options);\n};\n\n/**\n * Returns closest available date given the restriction `options`, or undefined otherwise\n * @param options - list of search options\n * @throws RangeError if the direction is not 1 or -1, or search dates cannot be represented.\n */\nexport const findAvailableDate = (options: AvailableDateOptions): Date | undefined => {\n  const { targetDate, initialDate, direction, ...restrictionOptions } = options;\n  if (direction !== 1 && direction !== -1) {\n    throw new RangeError('direction must be 1 or -1.');\n  }\n  if (!Number.isFinite(targetDate.getTime()) || !Number.isFinite(initialDate.getTime())) {\n    throw new RangeError('targetDate and initialDate must be valid.');\n  }\n\n  let availableDate = targetDate;\n  let daysOffset = 0;\n  // if the target date is available, return it immediately\n  if (!isRestrictedDate(targetDate, restrictionOptions)) {\n    return targetDate;\n  }\n\n  while (\n    compareDatePart(initialDate, availableDate) !== 0 &&\n    isRestrictedDate(availableDate, restrictionOptions) &&\n    !isAfterMaxDate(availableDate, restrictionOptions) &&\n    !isBeforeMinDate(availableDate, restrictionOptions)\n  ) {\n    // A skipped local date may normalize back to the previous candidate.\n    daysOffset += direction;\n    availableDate = addDays(targetDate, daysOffset);\n    if (!Number.isFinite(availableDate.getTime())) {\n      throw new RangeError('Cannot search for an out-of-range date.');\n    }\n  }\n\n  if (compareDatePart(initialDate, availableDate) !== 0 && !isRestrictedDate(availableDate, restrictionOptions)) {\n    return availableDate;\n  }\n\n  return undefined;\n};\n\n/**\n * Generates a list of dates, bounded by min and max dates\n * @param dateRange - input date range\n * @param minDate - min date to limit the range\n * @param maxDate - max date to limit the range\n */\nexport const getBoundedDateRange = (dateRange: Date[], minDate?: Date, maxDate?: Date): Date[] => {\n  return dateRange.filter(\n    date => (!minDate || compareDatePart(date, minDate) >= 0) && (!maxDate || compareDatePart(date, maxDate) <= 0),\n  );\n};\n\n/** Filters selection candidates using inclusive bounds and restricted dates, preserving their order. */\nexport const getAvailableDates = (dates: Date[], options: RestrictedDatesOptions): Date[] =>\n  dates.filter(date => !isRestrictedDate(date, options));\n"],"names":["findAvailableDate","getAvailableDates","getBoundedDateRange","isRestrictedDate","isAfterMaxDate","date","options","maxDate","compareDatePart","isBeforeMinDate","minDate","restrictedDates","inRestrictedDates","some","rd","targetDate","initialDate","direction","restrictionOptions","RangeError","Number","isFinite","getTime","availableDate","daysOffset","addDays","undefined","dateRange","filter","dates"],"mappings":";;;;;;;;;;;QAsCaA;eAAAA;;QAkDAC;eAAAA;;QAPAC;eAAAA;;QAzDAC;eAAAA;;;0BAxB4B;AAGzC;;CAEC,GACD,MAAMC,iBAAiB,CAACC,MAAYC;IAClC,MAAM,EAAEC,OAAO,EAAE,GAAGD;IACpB,OAAOC,UAAUC,IAAAA,yBAAe,EAACH,MAAME,YAAY,IAAI;AACzD;AAEA;;CAEC,GACD,MAAME,kBAAkB,CAACJ,MAAYC;IACnC,MAAM,EAAEI,OAAO,EAAE,GAAGJ;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