/*!
 * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

import { DateTime } from "luxon";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "../i18n.generated/i18n.js";
import { ArrowLeftIcon, ArrowRightIcon, CalendarIcon, ChevronDownIcon } from "../icons.generated/index.js";
import { ComponentControlledState, getControlState } from "../inputUtils.js";
import { useDateFieldsTranslations } from "./useDateFieldsTranslations.js";
const {
  CONTROLLED
} = ComponentControlledState;
const isValidDateTime = dateTime => dateTime.isValid;
const utcDateTimeFromIsoString = dateString => DateTime.fromISO(dateString).toUTC();
export const useOdysseyDateFields = ({
  defaultValue,
  isDateEnabled = () => true,
  isMonthEnabled = () => true,
  isYearEnabled = () => true,
  minDate: minDateProp,
  maxDate: maxDateProp,
  onInputChange: onInputChangeProp,
  timeZone = "system",
  value
}) => {
  const [isOpen, setIsOpen] = useState(false);
  const [popperElement, setPopperElement] = useState();
  const [internalTimeZone, setInternalTimeZone] = useState(timeZone);
  const internalValueRef = useRef(null);
  const [validationDateRanges, setValidationDateRanges] = useState({
    minDate: undefined,
    maxDate: undefined
  });
  useEffect(() => {
    const minDate = minDateProp ? DateTime.fromISO(minDateProp) : undefined;
    const maxDate = maxDateProp ? DateTime.fromISO(maxDateProp) : undefined;
    setValidationDateRanges({
      minDate: minDate && isValidDateTime(minDate) ? minDate.startOf("day") : undefined,
      maxDate: maxDate && isValidDateTime(maxDate) ? maxDate.startOf("day") : undefined
    });
  }, [minDateProp, maxDateProp]);
  const {
    i18n
  } = useTranslation();
  const {
    language
  } = i18n;
  const invalidLocales = ["ok_PL", "ok_SK"];
  const isInvalidLocale = invalidLocales.includes(language) || language.includes("@");
  const localeText = useDateFieldsTranslations();
  const controlledStateRef = useRef(getControlState({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const defaultedLanguageCode = isInvalidLocale ? "en-US" : language.replaceAll("_", "-");
  const isValidTimeZone = useCallback(timeZone => DateTime.local().setZone(timeZone).isValid, []);
  const formatDateTimeToUtcIsoDateString = useCallback(value => value.toUTC().toISO() || undefined, []);
  const shouldDisableDate = useCallback(date => !isDateEnabled(new Date(date?.toJSDate())) || false, [isDateEnabled]);
  const shouldDisableMonth = useCallback(date => !isMonthEnabled(new Date(date?.toJSDate())) || false, [isMonthEnabled]);
  const shouldDisableYear = useCallback(date => !isYearEnabled(new Date(date?.toJSDate())) || false, [isYearEnabled]);
  const inputValues = useMemo(() => {
    if (value && controlledStateRef.current === CONTROLLED) {
      const valueAsUTCDateTime = utcDateTimeFromIsoString(value);
      if (isValidDateTime(valueAsUTCDateTime)) {
        return {
          value: valueAsUTCDateTime
        };
      }
    }
    if (defaultValue) {
      const defaultValueAsUTCDateTime = utcDateTimeFromIsoString(defaultValue);
      if (isValidDateTime(defaultValueAsUTCDateTime)) {
        return {
          defaultValue: defaultValueAsUTCDateTime
        };
      }
    }
    return null;
  }, [defaultValue, value]);
  const onTimeZoneChange = useCallback(timeZone => {
    if (timeZone && isValidTimeZone(timeZone)) {
      setInternalTimeZone(timeZone);
    }
  }, [isValidTimeZone, setInternalTimeZone]);
  const toggleCalendarVisibility = useCallback(() => setIsOpen(!isOpen), [isOpen, setIsOpen]);
  const closeCalendar = useCallback(() => {
    setIsOpen(false);
  }, [setIsOpen]);
  const formatDayOfWeek = date => date.toFormat("EEE");
  const onInputChange = useCallback(value => {
    onInputChangeProp?.(value);
  }, [onInputChangeProp]);
  const commonIcons = {
    ArrowLeftIcon: ArrowLeftIcon,
    ArrowRightIcon: ArrowRightIcon,
    CalendarIcon: CalendarIcon,
    ChevronDownIcon: ChevronDownIcon
  };
  return {
    closeCalendar,
    commonIcons,
    defaultedLanguageCode,
    formatDateTimeToUtcIsoDateString,
    formatDayOfWeek,
    inputValues,
    internalTimeZone,
    internalValueRef,
    isOpen,
    isValidTimeZone,
    localeText,
    maxDate: validationDateRanges.maxDate,
    minDate: validationDateRanges.minDate,
    popperElement,
    onInputChange,
    onTimeZoneChange,
    setInternalTimeZone,
    setIsOpen,
    setPopperElement,
    shouldDisableDate,
    shouldDisableMonth,
    shouldDisableYear,
    toggleCalendarVisibility
  };
};
//# sourceMappingURL=useOdysseyDateFields.js.map