"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DatePicker = void 0;
var _styled = _interopRequireDefault(require("@emotion/styled"));
var _xDatePickers = require("@mui/x-date-pickers");
var _react = require("react");
var _Button = require("../Buttons/Button.cjs");
var _i18n = require("../i18n.generated/i18n.cjs");
var _OdysseyDesignTokensContext = require("../OdysseyDesignTokensContext.cjs");
var _OdysseyThemeProvider = require("../OdysseyThemeProvider.cjs");
var _useUniqueId = require("../useUniqueId.cjs");
var _DateField = require("./DateField.cjs");
var _DateFieldActionBar = require("./DateFieldActionBar.cjs");
var _DateFieldLocalizationProvider = require("./DateFieldLocalizationProvider.cjs");
var _datePickerTheme = require("./datePickerTheme.cjs");
var _TimeZonePicker = require("./TimeZonePicker.cjs");
var _useOdysseyDateFields = require("./useOdysseyDateFields.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * Copyright (c) 2023-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 DatePickerContainer = _styled.default.div({
  ".MuiFormControl-root": {
    marginBlockEnd: 0
  }
});
const DatePickerWidthContainer = _styled.default.div(({
  odysseyDesignTokens
}) => ({
  ".MuiInput-root": {
    width: "100%",
    maxWidth: odysseyDesignTokens.TypographyLineLengthMax
  }
}));
const TimeZonePickerContainer = (0, _styled.default)("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  marginBlockStart: odysseyDesignTokens.Spacing3
}));
const DatePicker = ({
  defaultValue: defaultValueProp,
  errorMessage,
  hint,
  HintLinkComponent,
  id: idOverride,
  isDateEnabled = () => true,
  isDisabled,
  isOptional,
  isReadOnly,
  isMonthEnabled = () => true,
  isYearEnabled = () => true,
  label,
  minDate: minDateProp,
  maxDate: maxDateProp,
  onBlur,
  onCalendarDateChange,
  onInputChange: onInputChangeProp,
  timeZone,
  timeZonePickerLabel,
  timeZoneOptions,
  value: valueProp
}) => {
  const {
    i18n,
    t
  } = (0, _i18n.useTranslation)();
  const inputRef = (0, _react.useRef)(null);
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const datePickerId = (0, _useUniqueId.useUniqueId)(idOverride);
  const {
    closeCalendar,
    commonIcons,
    defaultedLanguageCode,
    formatDateTimeToUtcIsoDateString,
    formatDayOfWeek,
    inputValues,
    internalTimeZone,
    isOpen,
    localeText,
    minDate,
    maxDate,
    onInputChange,
    onTimeZoneChange,
    popperElement,
    setPopperElement,
    shouldDisableDate,
    shouldDisableMonth,
    shouldDisableYear,
    toggleCalendarVisibility
  } = (0, _useOdysseyDateFields.useOdysseyDateFields)({
    defaultValue: defaultValueProp,
    errorMessage,
    isDateEnabled,
    isMonthEnabled,
    isYearEnabled,
    minDate: minDateProp,
    maxDate: maxDateProp,
    onInputChange: onInputChangeProp,
    timeZone,
    value: valueProp
  });
  const {
    language
  } = i18n;
  const containerRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    setPopperElement(containerRef.current);
  }, [setPopperElement]);
  const formatDateTimeToJsDateStringOnCalendarSelection = (0, _react.useCallback)(value => {
    if (value) {
      const dateStringFromDateTime = formatDateTimeToUtcIsoDateString(value);
      if (dateStringFromDateTime) {
        onCalendarDateChange?.({
          value: dateStringFromDateTime,
          timeZone: internalTimeZone
        });
      }
    }
  }, [formatDateTimeToUtcIsoDateString, internalTimeZone, onCalendarDateChange]);
  const renderDateField = (0, _react.useCallback)(({
    defaultValue,
    inputRef,
    value
  }) => {
    return (0, _jsxRuntime.jsx)(_DateField.DateField, {
      defaultValue: defaultValue,
      endAdornment: (0, _jsxRuntime.jsx)(_Button.Button, {
        ariaLabel: t("picker.labels.date.choose"),
        label: "",
        onClick: toggleCalendarVisibility,
        size: "small",
        startIcon: (0, _jsxRuntime.jsx)(commonIcons.CalendarIcon, {}),
        variant: "floating"
      }),
      errorMessage: errorMessage,
      hint: hint,
      HintLinkComponent: HintLinkComponent,
      id: datePickerId,
      inputRef: inputRef,
      isDisabled: isDisabled,
      isOptional: isOptional,
      isReadOnly: isReadOnly,
      label: label,
      maxDate: maxDate,
      minDate: minDate,
      onBlur: onBlur,
      onChange: onInputChange,
      timezone: internalTimeZone,
      value: value
    });
  }, [commonIcons, datePickerId, errorMessage, hint, HintLinkComponent, internalTimeZone, isDisabled, isOptional, isReadOnly, label, onBlur, onInputChange, minDate, maxDate, t, toggleCalendarVisibility]);
  const slots = (0, _react.useMemo)(() => ({
    actionBar: _DateFieldActionBar.DateFieldActionBar,
    field: muiProps => renderDateField(muiProps),
    leftArrowIcon: () => (0, _jsxRuntime.jsx)(commonIcons.ArrowLeftIcon, {}),
    rightArrowIcon: () => (0, _jsxRuntime.jsx)(commonIcons.ArrowRightIcon, {}),
    switchViewIcon: () => (0, _jsxRuntime.jsx)(commonIcons.ChevronDownIcon, {})
  }), [commonIcons, renderDateField]);
  const slotProps = (0, _react.useMemo)(() => ({
    actionBar: ({
      wrapperVariant,
      onAccept,
      onCancel
    }) => ({
      actions: wrapperVariant === "desktop" ? [] : ["accept", "cancel"],
      onAccept,
      onCancel
    }),
    popper: {
      anchorEl: popperElement
    },
    toolbar: {
      toolbarPlaceholder: ""
    }
  }), [popperElement]);
  return (0, _jsxRuntime.jsx)(_OdysseyThemeProvider.OdysseyThemeProvider, {
    themeOverride: _datePickerTheme.datePickerTheme,
    children: (0, _jsxRuntime.jsxs)(_DateFieldLocalizationProvider.DateFieldLocalizationProvider, {
      defaultedLanguageCode: defaultedLanguageCode,
      localeText: localeText,
      children: [(0, _jsxRuntime.jsx)(DatePickerContainer, {
        children: (0, _jsxRuntime.jsx)(DatePickerWidthContainer, {
          odysseyDesignTokens: odysseyDesignTokens,
          ref: containerRef,
          children: (0, _jsxRuntime.jsx)(_xDatePickers.DatePicker, {
            dayOfWeekFormatter: formatDayOfWeek,
            defaultValue: inputValues?.defaultValue,
            disabled: isDisabled,
            fixedWeekNumber: 6,
            inputRef: inputRef,
            label: label,
            maxDate: maxDate,
            minDate: minDate,
            onChange: formatDateTimeToJsDateStringOnCalendarSelection,
            onClose: closeCalendar,
            open: isOpen,
            readOnly: isReadOnly,
            shouldDisableDate: shouldDisableDate,
            shouldDisableMonth: shouldDisableMonth,
            shouldDisableYear: shouldDisableYear,
            slotProps: slotProps,
            slots: slots,
            timezone: internalTimeZone,
            value: inputValues?.value
          }, language)
        })
      }), timeZoneOptions && timeZonePickerLabel && (0, _jsxRuntime.jsx)(TimeZonePickerContainer, {
        odysseyDesignTokens: odysseyDesignTokens,
        children: (0, _jsxRuntime.jsx)(_TimeZonePicker.TimeZonePicker, {
          isReadOnly: isReadOnly,
          label: timeZonePickerLabel,
          onTimeZoneChange: onTimeZoneChange,
          timeZoneOptions: timeZoneOptions,
          value: internalTimeZone
        })
      })]
    })
  });
};
const MemoizedDatePicker = exports.DatePicker = (0, _react.memo)(DatePicker);
MemoizedDatePicker.displayName = "DatePicker";
//# sourceMappingURL=DatePicker.cjs.map