'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
require('../common/theme.js');
require('../common/direction.js');
require('@transferwise/neptune-tokens');
require('../common/propsValues/control.js');
require('../common/propsValues/size.js');
require('../common/propsValues/typography.js');
require('../common/propsValues/width.js');
require('../common/propsValues/type.js');
require('../common/propsValues/dateMode.js');
var monthFormat = require('../common/propsValues/monthFormat.js');
require('../common/propsValues/position.js');
require('../common/propsValues/layouts.js');
require('../common/propsValues/status.js');
require('../common/propsValues/sentiment.js');
require('../common/propsValues/profileType.js');
require('../common/propsValues/variant.js');
require('../common/propsValues/scroll.js');
require('../common/propsValues/markdownNodeType.js');
require('../common/fileType.js');
require('@transferwise/formatting');
require('@transferwise/icons');
require('clsx');
require('react-intl');
require('../common/closeButton/CloseButton.messages.js');
var jsxRuntime = require('react/jsx-runtime');
var moveToWithinRange = require('../common/dateUtils/moveToWithinRange/moveToWithinRange.js');
var isWithinRange = require('../common/dateUtils/isWithinRange/isWithinRange.js');
var getDateView = require('../common/dateUtils/getDateView/getDateView.js');
var DayCalendar = require('./dayCalendar/DayCalendar.js');
var getStartOfDay = require('./getStartOfDay/getStartOfDay.js');
var MonthCalendar = require('./monthCalendar/MonthCalendar.js');
var YearCalendar = require('./yearCalendar/YearCalendar.js');

const Calendar = ({
  value,
  min: minProp = null,
  max: maxProp = null,
  monthFormat: monthFormat$1 = monthFormat.MonthFormat.LONG,
  placeholder,
  onChange,
  onSelect
}) => {
  const selectedDate = getStartOfDay.getStartOfDay(value);
  const min = getStartOfDay.getStartOfDay(minProp);
  const max = getStartOfDay.getStartOfDay(maxProp);
  React.useEffect(() => {
    if (selectedDate && !isWithinRange.isWithinRange(selectedDate, min, max)) {
      onChange(moveToWithinRange.moveToWithinRange(selectedDate, min, max));
    }
  }, [selectedDate?.getTime(), min?.getTime(), max?.getTime(), onChange]); // eslint-disable-line react-hooks/exhaustive-deps
  const dateView = getDateView.returnDateView(selectedDate, min, max);
  const [viewMonth, setViewMonth] = React.useState(dateView.getMonth());
  const [viewYear, setViewYear] = React.useState(dateView.getFullYear());
  const [mode, setMode] = React.useState('day');
  React.useEffect(() => {
    const view = getDateView.returnDateView(selectedDate, min, max);
    // eslint-disable-next-line react-hooks/set-state-in-effect
    setViewMonth(view.getMonth());
    setViewYear(view.getFullYear());
  }, [selectedDate?.getTime(), min?.getTime(), max?.getTime()]); // eslint-disable-line react-hooks/exhaustive-deps
  const handleViewDateUpdate = React.useCallback(
  // eslint-disable-next-line react-hooks/preserve-manual-memoization
  ({
    month,
    year
  }) => {
    if (month !== undefined) setViewMonth(month);
    if (year !== undefined) setViewYear(year);
  }, []);
  const switchToDays = React.useCallback(() => setMode('day'), []);
  const switchToMonths = React.useCallback(() => setMode('month'), []);
  const switchToYears = React.useCallback(() => setMode('year'), []);
  const handleSelectedDateUpdate = React.useCallback(date => {
    if (onSelect) {
      onSelect(date);
    } else {
      onChange(date);
    }
  }, [onChange, onSelect]);
  const adjustDate = React.useCallback((daysToAdd, monthsToAdd, yearsToAdd) => {
    let date;
    if (selectedDate) {
      date = new Date(mode === 'year' ? selectedDate.getFullYear() + yearsToAdd : selectedDate.getFullYear(), mode === 'month' ? selectedDate.getMonth() + monthsToAdd : selectedDate.getMonth(), mode === 'day' ? selectedDate.getDate() + daysToAdd : selectedDate.getDate());
    } else {
      date = getStartOfDay.getStartOfDay(new Date());
    }
    date &&= moveToWithinRange.moveToWithinRange(date, min, max);
    if (date?.getTime() !== selectedDate?.getTime()) {
      onChange(date);
    }
  }, [selectedDate, min, max, mode, onChange]);
  const handleKeyDown = React.useCallback(event => {
    switch (event.key) {
      case 'ArrowLeft':
        adjustDate(-1, -1, -1);
        event.preventDefault();
        break;
      case 'ArrowUp':
        adjustDate(-7, -4, -4);
        event.preventDefault();
        break;
      case 'ArrowRight':
        adjustDate(1, 1, 1);
        event.preventDefault();
        break;
      case 'ArrowDown':
        adjustDate(7, 4, 4);
        event.preventDefault();
        break;
    }
  }, [adjustDate]);
  return (
    /*#__PURE__*/
    // eslint-disable-next-line jsx-a11y/no-static-element-interactions
    jsxRuntime.jsxs("div", {
      onKeyDown: handleKeyDown,
      children: [mode === 'day' && /*#__PURE__*/jsxRuntime.jsx(DayCalendar.default, {
        selectedDate: selectedDate,
        min: min,
        max: max,
        viewMonth: viewMonth,
        viewYear: viewYear,
        monthFormat: monthFormat$1,
        onSelect: handleSelectedDateUpdate,
        onLabelClick: switchToYears,
        onViewDateUpdate: handleViewDateUpdate
      }), mode === 'month' && /*#__PURE__*/jsxRuntime.jsx(MonthCalendar.default, {
        selectedDate: selectedDate,
        min: min,
        max: max,
        viewYear: viewYear,
        ...(placeholder != null && {
          placeholder
        }),
        onSelect: switchToDays,
        onLabelClick: switchToYears,
        onViewDateUpdate: handleViewDateUpdate
      }), mode === 'year' && /*#__PURE__*/jsxRuntime.jsx(YearCalendar.default, {
        selectedDate: selectedDate,
        min: min,
        max: max,
        viewYear: viewYear,
        ...(placeholder != null && {
          placeholder
        }),
        onSelect: switchToMonths,
        onViewDateUpdate: handleViewDateUpdate
      })]
    })
  );
};

exports.default = Calendar;
//# sourceMappingURL=Calendar.js.map
