{"version":3,"file":"useOdysseyDateFields.cjs","names":["_luxon","require","_react","_i18n","_index","_useDateFieldsTranslations","isValidDateTime","dateTime","isValid","utcDateTimeFromIsoString","dateString","DateTime","fromISO","toUTC","utcDateTimeFromParsableIsoString","dateTimeFromIsoString","useOdysseyDateFields","defaultValue","isDateEnabled","isMonthEnabled","isYearEnabled","minDate","minDateProp","maxDate","maxDateProp","onInputChange","onInputChangeProp","timeZone","value","isOpen","setIsOpen","useState","popperElement","setPopperElement","internalTimeZone","setInternalTimeZone","internalValueRef","useRef","validationDateRanges","setValidationDateRanges","undefined","useEffect","startOf","i18n","useTranslation","language","invalidLocales","isInvalidLocale","includes","localeText","useDateFieldsTranslations","defaultedLanguageCode","replaceAll","isValidTimeZone","useCallback","local","setZone","formatDateTimeToUtcIsoDateString","toISO","shouldDisableDate","date","Date","toJSDate","shouldDisableMonth","shouldDisableYear","seedValueFromDefaultValue","pickerValue","useMemo","onTimeZoneChange","toggleCalendarVisibility","closeCalendar","formatDayOfWeek","toFormat","commonIcons","ArrowLeftIcon","ArrowRightIcon","CalendarIcon","ChevronDownIcon","exports"],"sources":["../../../src/DatePickers/useOdysseyDateFields.ts"],"sourcesContent":["/*!\n * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n */\n\nimport { DateTime } from \"luxon\";\nimport {\n  FocusEventHandler,\n  useCallback,\n  useEffect,\n  useMemo,\n  useRef,\n  useState,\n} from \"react\";\n\nimport { FieldComponentProps } from \"../FieldComponentProps.js\";\nimport { useTranslation } from \"../i18n.generated/i18n.js\";\nimport {\n  ArrowLeftIcon,\n  ArrowRightIcon,\n  CalendarIcon,\n  ChevronDownIcon,\n} from \"../icons.generated/index.js\";\nimport { TimeZoneOption, TimeZonePickerProps } from \"./TimeZonePicker.js\";\nimport { useDateFieldsTranslations } from \"./useDateFieldsTranslations.js\";\n\nconst isValidDateTime = (dateTime: DateTime) => dateTime.isValid;\n\nconst utcDateTimeFromIsoString = (dateString: string) =>\n  DateTime.fromISO(dateString).toUTC();\n\nconst utcDateTimeFromParsableIsoString = (dateString?: string) => {\n  if (!dateString) {\n    return null;\n  }\n\n  const dateTimeFromIsoString = utcDateTimeFromIsoString(dateString);\n\n  return isValidDateTime(dateTimeFromIsoString) ? dateTimeFromIsoString : null;\n};\n\nexport type UncontrolledDateFieldProps = {\n  /**\n   * Value the field is seeded with while it mounts. Read only on that first render, so a later change is ignored.\n   *\n   * NOTE: Must be a date string in ISO format\n   */\n  defaultValue?: string;\n  /**\n   * Value the field displays for its whole life. Should not be used if `defaultValue` is used\n   */\n  value?: never;\n};\n\nexport type ControlledDateFieldProps = {\n  /**\n   * Value the field is seeded with while it mounts. Should not be used if `value` is used\n   */\n  defaultValue?: never;\n  /**\n   * Value the field displays for its whole life, so passing an empty value clears it.\n   *\n   * NOTE: Must be a date string in ISO format. Anything that cannot be parsed\n   * leaves the field empty\n   */\n  value?: string;\n};\n\nexport type OdysseyDateFieldBaseProps = {\n  /**\n   * Disable specific date(s).\n   *\n   * Warning: This function can be called multiple times (for example when rendering date calendar, checking if focus can be moved to a certain date, etc.). Expensive computations can impact performance.\n   */\n  isDateEnabled?: (date: Date) => boolean;\n  /**\n   * Disable specific month(s).\n   */\n  isMonthEnabled?: (date: Date) => boolean;\n  /**\n   * Disable specific year(s).\n   */\n  isYearEnabled?: (date: Date) => boolean;\n  /**\n   * The label for the `input` element.\n   */\n  label: string;\n  /**\n   * Maximum date allowed.\n   *\n   * NOTE: If not a valid date string in ISO format, `maxDate` will not apply\n   */\n  maxDate?: string;\n  /**\n   * Minimum date allowed.\n   *\n   * NOTE: If not a valid date string in ISO format, `minDate` will not apply\n   */\n  minDate?: string;\n  /**\n   * Callback fired when the a date field loses focus\n   */\n  onBlur?: FocusEventHandler<HTMLInputElement>;\n  /**\n   * Callback fired when the a date is selected with the calendar.\n   */\n  onCalendarDateChange?: ({\n    value,\n    timeZone,\n  }: {\n    timeZone: string;\n    value: string | undefined;\n  }) => void;\n  /**\n   * Callback fired when the date/text input changes.\n   */\n  onInputChange?: (value: string) => void;\n  /**\n   * a default timezone for the picker\n   */\n  timeZone?: string;\n  /**\n   * If provided, a `TimeZonePicker` will be rendered below the DatePicker. These options will populate as the Autocomplete options\n   */\n  timeZoneOptions?: TimeZoneOption[];\n  /**\n   * label for `TimeZonePicker`\n   */\n  timeZonePickerLabel?: TimeZonePickerProps[\"label\"];\n};\n\nexport type OdysseyDateFieldProps = OdysseyDateFieldBaseProps &\n  (ControlledDateFieldProps | UncontrolledDateFieldProps);\n\ntype FormatDateTimeToUtcIsoDateString = (value: DateTime) => string | undefined;\n\ntype ValidationDateRanges = {\n  maxDate?: DateTime;\n  minDate?: DateTime;\n};\n\nexport const useOdysseyDateFields = ({\n  defaultValue,\n  isDateEnabled = () => true,\n  isMonthEnabled = () => true,\n  isYearEnabled = () => true,\n  minDate: minDateProp,\n  maxDate: maxDateProp,\n  onInputChange: onInputChangeProp,\n  timeZone = \"system\",\n  value,\n}: Pick<\n  OdysseyDateFieldProps,\n  | \"defaultValue\"\n  | \"isDateEnabled\"\n  | \"isMonthEnabled\"\n  | \"isYearEnabled\"\n  | \"minDate\"\n  | \"maxDate\"\n  | \"onInputChange\"\n  | \"timeZone\"\n  | \"value\"\n> &\n  Pick<FieldComponentProps, \"errorMessage\">) => {\n  const [isOpen, setIsOpen] = useState(false);\n  const [popperElement, setPopperElement] = useState<HTMLInputElement | null>();\n  const [internalTimeZone, setInternalTimeZone] = useState(timeZone);\n\n  const internalValueRef = useRef<DateTime | null>(null);\n\n  const [validationDateRanges, setValidationDateRanges] =\n    useState<ValidationDateRanges>({\n      minDate: undefined,\n      maxDate: undefined,\n    });\n\n  useEffect(() => {\n    const minDate = minDateProp ? DateTime.fromISO(minDateProp) : undefined;\n    const maxDate = maxDateProp ? DateTime.fromISO(maxDateProp) : undefined;\n\n    setValidationDateRanges({\n      minDate:\n        minDate && isValidDateTime(minDate)\n          ? minDate.startOf(\"day\")\n          : undefined,\n      maxDate:\n        maxDate && isValidDateTime(maxDate)\n          ? maxDate.startOf(\"day\")\n          : undefined,\n    });\n  }, [minDateProp, maxDateProp]);\n\n  const { i18n } = useTranslation();\n  const { language } = i18n;\n\n  const invalidLocales = [\"ok_PL\", \"ok_SK\"];\n  // In the Applitools env the language code is `en-us@posix`. Need to check for that\n  const isInvalidLocale =\n    invalidLocales.includes(language) || language.includes(\"@\");\n\n  const localeText = useDateFieldsTranslations();\n\n  const defaultedLanguageCode = isInvalidLocale\n    ? \"en-US\"\n    : language.replaceAll(\"_\", \"-\");\n\n  const isValidTimeZone = useCallback(\n    (timeZone: string) => DateTime.local().setZone(timeZone).isValid,\n    [],\n  );\n\n  const formatDateTimeToUtcIsoDateString =\n    useCallback<FormatDateTimeToUtcIsoDateString>(\n      (value) => value.toUTC().toISO() || undefined,\n      [],\n    );\n\n  const shouldDisableDate = useCallback(\n    (date: DateTime) => !isDateEnabled(new Date(date?.toJSDate())) || false,\n    [isDateEnabled],\n  );\n\n  const shouldDisableMonth = useCallback(\n    (date: DateTime) => !isMonthEnabled(new Date(date?.toJSDate())) || false,\n    [isMonthEnabled],\n  );\n\n  const shouldDisableYear = useCallback(\n    (date: DateTime) => !isYearEnabled(new Date(date?.toJSDate())) || false,\n    [isYearEnabled],\n  );\n\n  // MUI's picker captures `defaultValue` while it mounts and never reads it again, so `defaultValue` can only ever seed the first render. Freezing the seed here keeps that contract explicit instead of letting a later `defaultValue` overwrite whatever the reader has since typed.\n  const [seedValueFromDefaultValue] = useState(() =>\n    utcDateTimeFromParsableIsoString(defaultValue),\n  );\n\n  // Both props resolve to the one `value` the picker is handed on every render, which keeps it controlled for its whole life. Handing MUI `defaultValue` for an uncontrolled field instead would make the picker stop following `value` after mount, so a date arriving later (or a field being cleared) would never reach the input.\n  const pickerValue = useMemo(\n    () =>\n      value === undefined\n        ? seedValueFromDefaultValue\n        : utcDateTimeFromParsableIsoString(value),\n    [seedValueFromDefaultValue, value],\n  );\n\n  const onTimeZoneChange = useCallback(\n    (timeZone: string | undefined) => {\n      if (timeZone && isValidTimeZone(timeZone)) {\n        setInternalTimeZone(timeZone);\n      }\n    },\n    [isValidTimeZone, setInternalTimeZone],\n  );\n\n  const toggleCalendarVisibility = useCallback(\n    () => setIsOpen(!isOpen),\n    [isOpen, setIsOpen],\n  );\n\n  const closeCalendar = useCallback(() => {\n    setIsOpen(false);\n  }, [setIsOpen]);\n\n  const formatDayOfWeek = (date: DateTime) => date.toFormat(\"EEE\");\n\n  const onInputChange = useCallback<(value: string) => void>(\n    (value) => {\n      onInputChangeProp?.(value);\n    },\n    [onInputChangeProp],\n  );\n\n  const commonIcons = {\n    ArrowLeftIcon: ArrowLeftIcon,\n    ArrowRightIcon: ArrowRightIcon,\n    CalendarIcon: CalendarIcon,\n    ChevronDownIcon: ChevronDownIcon,\n  };\n\n  return {\n    closeCalendar,\n    commonIcons,\n    defaultedLanguageCode,\n    formatDateTimeToUtcIsoDateString,\n    formatDayOfWeek,\n    internalTimeZone,\n    internalValueRef,\n    isOpen,\n    isValidTimeZone,\n    localeText,\n    maxDate: validationDateRanges.maxDate,\n    minDate: validationDateRanges.minDate,\n    popperElement,\n    onInputChange,\n    onTimeZoneChange,\n    pickerValue,\n    setInternalTimeZone,\n    setIsOpen,\n    setPopperElement,\n    shouldDisableDate,\n    shouldDisableMonth,\n    shouldDisableYear,\n    toggleCalendarVisibility,\n  };\n};\n"],"mappings":";;;;;;AAYA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAUA,IAAAE,KAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OA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