"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useOdysseyDateFields = void 0;
var _luxon = require("luxon");
var _react = require("react");
var _i18n = require("../i18n.generated/i18n.cjs");
var _index = require("../icons.generated/index.cjs");
var _inputUtils = require("../inputUtils.cjs");
var _useDateFieldsTranslations = require("./useDateFieldsTranslations.cjs");
/*!
 * 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.
 */

const {
  CONTROLLED
} = _inputUtils.ComponentControlledState;
const isValidDateTime = dateTime => dateTime.isValid;
const utcDateTimeFromIsoString = dateString => _luxon.DateTime.fromISO(dateString).toUTC();
const useOdysseyDateFields = ({
  defaultValue,
  isDateEnabled = () => true,
  isMonthEnabled = () => true,
  isYearEnabled = () => true,
  minDate: minDateProp,
  maxDate: maxDateProp,
  onInputChange: onInputChangeProp,
  timeZone = "system",
  value
}) => {
  const [isOpen, setIsOpen] = (0, _react.useState)(false);
  const [popperElement, setPopperElement] = (0, _react.useState)();
  const [internalTimeZone, setInternalTimeZone] = (0, _react.useState)(timeZone);
  const internalValueRef = (0, _react.useRef)(null);
  const [validationDateRanges, setValidationDateRanges] = (0, _react.useState)({
    minDate: undefined,
    maxDate: undefined
  });
  (0, _react.useEffect)(() => {
    const minDate = minDateProp ? _luxon.DateTime.fromISO(minDateProp) : undefined;
    const maxDate = maxDateProp ? _luxon.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
  } = (0, _i18n.useTranslation)();
  const {
    language
  } = i18n;
  const invalidLocales = ["ok_PL", "ok_SK"];
  const isInvalidLocale = invalidLocales.includes(language) || language.includes("@");
  const localeText = (0, _useDateFieldsTranslations.useDateFieldsTranslations)();
  const controlledStateRef = (0, _react.useRef)((0, _inputUtils.getControlState)({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const defaultedLanguageCode = isInvalidLocale ? "en-US" : language.replaceAll("_", "-");
  const isValidTimeZone = (0, _react.useCallback)(timeZone => _luxon.DateTime.local().setZone(timeZone).isValid, []);
  const formatDateTimeToUtcIsoDateString = (0, _react.useCallback)(value => value.toUTC().toISO() || undefined, []);
  const shouldDisableDate = (0, _react.useCallback)(date => !isDateEnabled(new Date(date?.toJSDate())) || false, [isDateEnabled]);
  const shouldDisableMonth = (0, _react.useCallback)(date => !isMonthEnabled(new Date(date?.toJSDate())) || false, [isMonthEnabled]);
  const shouldDisableYear = (0, _react.useCallback)(date => !isYearEnabled(new Date(date?.toJSDate())) || false, [isYearEnabled]);
  const inputValues = (0, _react.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 = (0, _react.useCallback)(timeZone => {
    if (timeZone && isValidTimeZone(timeZone)) {
      setInternalTimeZone(timeZone);
    }
  }, [isValidTimeZone, setInternalTimeZone]);
  const toggleCalendarVisibility = (0, _react.useCallback)(() => setIsOpen(!isOpen), [isOpen, setIsOpen]);
  const closeCalendar = (0, _react.useCallback)(() => {
    setIsOpen(false);
  }, [setIsOpen]);
  const formatDayOfWeek = date => date.toFormat("EEE");
  const onInputChange = (0, _react.useCallback)(value => {
    onInputChangeProp?.(value);
  }, [onInputChangeProp]);
  const commonIcons = {
    ArrowLeftIcon: _index.ArrowLeftIcon,
    ArrowRightIcon: _index.ArrowRightIcon,
    CalendarIcon: _index.CalendarIcon,
    ChevronDownIcon: _index.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
  };
};
exports.useOdysseyDateFields = useOdysseyDateFields;
//# sourceMappingURL=useOdysseyDateFields.cjs.map