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ACvC,YAAY,cAAc,gBAAgB,CAAA,GAAA,4CAAiB,EAAE,MAAM,YAAY,IAAI,MAAM,gBAAgB;YAG3G,aAAa,OAAO,CAAC;QACvB;yBACA;QACA,WAAW;QACX,aAAY,MAAM,EAAE,YAAY;YAC9B,IAAI,CAAC,WACH,OAAO;YAGT,IAAI,gBAAgB,CAAA,GAAA,0CAAe,EAAE,cAAc;YACnD,IAAI,YAAY,IAAI,CAAA,GAAA,0CAAY,EAAE,QAAQ;YAC1C,OAAO,UAAU,MAAM,CAAC;QAC1B;QACA,kBAAiB,MAAM,EAAE,aAA+B;YACtD,IAAI,aAAa;gBAAC,GAAG,UAAU;gBAAE,GAAG,aAAa;YAAA;YACjD,IAAI,mBAAmB,CAAA,GAAA,0CAAe,EAAE,CAAC,GAAG;YAC5C,OAAO,IAAI,CAAA,GAAA,0CAAY,EAAE,QAAQ;QACnC;IACF;AACF","sources":["packages/react-stately/src/datepicker/useDatePickerState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {CalendarDate, DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';\nimport {DatePickerProps, DateValue, Granularity, MappedDateValue, TimeValue} from './types';\nimport {FieldOptions, FormatterOptions, getFormatOptions, getPlaceholderTime, getValidationResult, useDefaultProps} from './utils';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\nimport {ValidationState} from '@react-types/shared';\n\nexport interface DatePickerStateOptions<T extends DateValue> extends DatePickerProps<T> {\n  /**\n   * Determines whether the date picker popover should close automatically when a date is selected.\n   * @default true\n   */\n  shouldCloseOnSelect?: boolean | (() => boolean)\n}\n\nexport interface DatePickerState extends OverlayTriggerState, FormValidationState {\n  /** The currently selected date. */\n  value: DateValue | null,\n  /** The default date. */\n  defaultValue: DateValue | null,\n  /** Sets the selected date. */\n  setValue(value: DateValue | null): void,\n  /**\n   * The date portion of the value. This may be set prior to `value` if the user has\n   * selected a date but has not yet selected a time.\n   */\n  dateValue: DateValue | null,\n  /** Sets the date portion of the value. */\n  setDateValue(value: DateValue): void,\n  /**\n   * The time portion of the value. This may be set prior to `value` if the user has\n   * selected a time but has not yet selected a date.\n   */\n  timeValue: TimeValue | null,\n  /** Sets the time portion of the value. */\n  setTimeValue(value: TimeValue): void,\n  /** The granularity for the field, based on the `granularity` prop and current value. */\n  granularity: Granularity,\n  /** Whether the date picker supports selecting a time, according to the `granularity` prop and current value. */\n  hasTime: boolean,\n  /** Whether the calendar popover is currently open. */\n  isOpen: boolean,\n  /** Sets whether the calendar popover is open. */\n  setOpen(isOpen: boolean): void,\n  /**\n   * The current validation state of the date picker, based on the `validationState`, `minValue`, and `maxValue` props.\n   * @deprecated Use `isInvalid` instead.\n   */\n  validationState: ValidationState | null,\n  /** Whether the date picker is invalid, based on the `isInvalid`, `minValue`, and `maxValue` props. */\n  isInvalid: boolean,\n  /** Formats the selected value using the given options. */\n  formatValue(locale: string, fieldOptions: FieldOptions): string,\n  /** Gets a formatter based on state's props. */\n  getDateFormatter(locale: string, formatOptions: FormatterOptions): DateFormatter\n}\n\n/**\n * Provides state management for a date picker component.\n * A date picker combines a DateField and a Calendar popover to allow users to enter or select a date and time value.\n */\nexport function useDatePickerState<T extends DateValue = DateValue>(props: DatePickerStateOptions<T>): DatePickerState {\n  let overlayState = useOverlayTriggerState(props);\n  let [value, setValue] = useControlledState<DateValue | null, MappedDateValue<T> | null>(props.value, props.defaultValue || null, props.onChange);\n  let [initialValue] = useState(value);\n\n  let v = (value || props.placeholderValue || null);\n  let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);\n  let dateValue = value != null ? value.toDate(defaultTimeZone ?? 'UTC') : null;\n  let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';\n  let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;\n\n  let [selectedDate, setSelectedDate] = useState<DateValue | null>(null);\n  let [selectedTime, setSelectedTime] = useState<TimeValue | null>(null);\n\n  if (value) {\n    selectedDate = value;\n    if ('hour' in value) {\n      selectedTime = value;\n    }\n  }\n\n  // props.granularity must actually exist in the value if one is provided.\n  if (v && !(granularity in v)) {\n    throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());\n  }\n\n  let showEra = value?.calendar.identifier === 'gregory' && value.era === 'BC';\n  let formatOpts = useMemo(() => ({\n    granularity,\n    timeZone: defaultTimeZone,\n    hideTimeZone: props.hideTimeZone,\n    hourCycle: props.hourCycle,\n    shouldForceLeadingZeros: props.shouldForceLeadingZeros,\n    showEra\n  }), [granularity, props.hourCycle, props.shouldForceLeadingZeros, defaultTimeZone, props.hideTimeZone, showEra]);\n\n  let {minValue, maxValue, isDateUnavailable} = props;\n  let builtinValidation = useMemo(() => getValidationResult(\n    value,\n    minValue,\n    maxValue,\n    isDateUnavailable,\n    formatOpts\n  ), [value, minValue, maxValue, isDateUnavailable, formatOpts]);\n\n  let validation = useFormValidationState({\n    ...props,\n    value: value as MappedDateValue<T> | null,\n    builtinValidation\n  });\n\n  let isValueInvalid = validation.displayValidation.isInvalid;\n  let validationState: ValidationState | null = props.validationState || (isValueInvalid ? 'invalid' : null);\n\n  let commitValue = (date: DateValue, time: TimeValue) => {\n    setValue('timeZone' in time ? time.set(toCalendarDate(date)) : toCalendarDateTime(date, time));\n    setSelectedDate(null);\n    setSelectedTime(null);\n    validation.commitValidation();\n  };\n\n  // Intercept setValue to make sure the Time section is not changed by date selection in Calendar\n  let selectDate = (newValue: CalendarDate) => {\n    let shouldClose = typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;\n    if (hasTime) {\n      if (selectedTime || shouldClose) {\n        commitValue(newValue, selectedTime || getPlaceholderTime(props.defaultValue || props.placeholderValue));\n      } else {\n        setSelectedDate(newValue);\n      }\n    } else {\n      setValue(newValue);\n      validation.commitValidation();\n    }\n\n    if (shouldClose) {\n      overlayState.setOpen(false);\n    }\n  };\n\n  let selectTime = (newValue: TimeValue) => {\n    if (selectedDate && newValue) {\n      commitValue(selectedDate, newValue);\n    } else {\n      setSelectedTime(newValue);\n    }\n  };\n\n  return {\n    ...validation,\n    value,\n    defaultValue: props.defaultValue ?? initialValue,\n    setValue,\n    dateValue: selectedDate,\n    timeValue: selectedTime,\n    setDateValue: selectDate,\n    setTimeValue: selectTime,\n    granularity,\n    hasTime,\n    ...overlayState,\n    setOpen(isOpen) {\n      // Commit the selected date when the calendar is closed. Use a placeholder time if one wasn't set.\n      // If only the time was set and not the date, don't commit. The state will be preserved until\n      // the user opens the popover again.\n      if (!isOpen && !value && selectedDate && hasTime) {\n        commitValue(selectedDate, selectedTime || getPlaceholderTime(props.defaultValue || props.placeholderValue));\n      }\n\n      overlayState.setOpen(isOpen);\n    },\n    validationState,\n    isInvalid: isValueInvalid,\n    formatValue(locale, fieldOptions) {\n      if (!dateValue) {\n        return '';\n      }\n\n      let formatOptions = getFormatOptions(fieldOptions, formatOpts);\n      let formatter = new DateFormatter(locale, formatOptions);\n      return formatter.format(dateValue);\n    },\n    getDateFormatter(locale, formatOptions: FormatterOptions) {\n      let newOptions = {...formatOpts, ...formatOptions};\n      let newFormatOptions = getFormatOptions({}, newOptions);\n      return new DateFormatter(locale, newFormatOptions);\n    }\n  };\n}\n"],"names":[],"version":3,"file":"useDatePickerState.cjs.map"}