import { objectWithoutProperties as _objectWithoutProperties, extends as _extends } from '../../../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { memo, forwardRef, useRef } from 'react';
import { cx } from '../../../../utils/index.js';
import { classes } from '../../Calendar.tokens.js';
import { StyledItemRoot, StyledItem, StyledEvents, StyledEvent } from './DateStructureItem.styles.js';
import { useForkRef } from '../../../../hooks/useForkRef.js';

var _excluded = ["children", "isFocused", "dayOfWeek", "disabled", "isCurrent", "isSelected", "isDayInCurrentMonth", "isDouble", "inRange", "isHovered", "sideInRange", "eventList", "day", "monthIndex", "year", "onClick", "onMouseOver", "onTriggerEventTooltip", "onFocus", "disabledArrowKey", "disabledMonths"];

/**
 * Компонент элемента даты в сетке календаря.
 */
var DateStructureItem = /*#__PURE__*/memo(/*#__PURE__*/forwardRef(function (_ref, outerRef) {
  var children = _ref.children,
    isFocused = _ref.isFocused,
    dayOfWeek = _ref.dayOfWeek,
    disabled = _ref.disabled,
    isCurrent = _ref.isCurrent,
    isSelected = _ref.isSelected,
    isDayInCurrentMonth = _ref.isDayInCurrentMonth,
    isDouble = _ref.isDouble,
    inRange = _ref.inRange,
    isHovered = _ref.isHovered,
    sideInRange = _ref.sideInRange,
    _ref$eventList = _ref.eventList,
    eventList = _ref$eventList === void 0 ? [] : _ref$eventList,
    day = _ref.day,
    monthIndex = _ref.monthIndex,
    year = _ref.year,
    onClick = _ref.onClick,
    onMouseOver = _ref.onMouseOver,
    onTriggerEventTooltip = _ref.onTriggerEventTooltip,
    onFocus = _ref.onFocus,
    disabledArrowKey = _ref.disabledArrowKey,
    disabledMonths = _ref.disabledMonths,
    rest = _objectWithoutProperties(_ref, _excluded);
  var structureRef = useRef(null);
  var innerRef = useForkRef(outerRef, structureRef);
  var selectableClass = !dayOfWeek && !disabled ? classes.selectableItem : undefined;
  var selectedClass = isDayInCurrentMonth && isSelected ? classes.selectedItem : undefined;
  var currentClass = isDayInCurrentMonth && isCurrent ? classes.currentItem : undefined;
  var hoveredClass = isDayInCurrentMonth && isHovered ? classes.hoveredItem : undefined;
  var dayOfWeekClass = dayOfWeek ? classes.dayOfWeek : undefined;
  var dayInCurrentMonthClass = isDayInCurrentMonth ? classes.dayInCurrentMonth : undefined;
  var disabledClass = disabled && !isCurrent ? classes.disabled : undefined;
  var disabledCurrentClass = disabled && isCurrent ? classes.disabledCurrent : undefined;
  var sideInRangeLeft = isDayInCurrentMonth && sideInRange === 'left' ? classes.sideLeft : undefined;
  var sideInRangeRight = isDayInCurrentMonth && sideInRange === 'right' ? classes.sideRight : undefined;
  var sideInRangeClass = isDayInCurrentMonth && sideInRange ? classes.sideInRange : undefined;
  var inRangeStyle = isDayInCurrentMonth && inRange ? classes.inRange : undefined;
  var sideOffset = !isSelected && isCurrent ? -1 : 0;
  var handleMouseOver = function handleMouseOver(event) {
    if (onMouseOver) {
      onMouseOver(event);
    }
    if (onTriggerEventTooltip) {
      var eventNodes = (eventList === null || eventList === void 0 ? void 0 : eventList.map(function (_ref2) {
        var eventInfo = _ref2.eventInfo;
        return eventInfo;
      }).filter(Boolean)) || [];
      onTriggerEventTooltip(structureRef, eventNodes);
    }
  };
  return /*#__PURE__*/React__default.createElement(StyledItemRoot, _extends({
    ref: innerRef,
    className: cx(disabledClass, disabledCurrentClass, dayOfWeekClass, selectableClass, selectedClass, currentClass, hoveredClass, dayInCurrentMonthClass),
    tabIndex: isFocused ? 0 : -1,
    dayOfWeek: dayOfWeek,
    isCurrent: isCurrent,
    isSelected: isSelected,
    isDayInCurrentMonth: isDayInCurrentMonth,
    isDouble: isDouble,
    isHovered: isHovered,
    sideInRange: sideInRange,
    onClick: disabled ? undefined : onClick,
    onMouseOver: handleMouseOver,
    onFocus: onFocus,
    "data-day": day,
    "data-month-index": monthIndex,
    "data-year": year,
    "data-disabled-arrow-key": disabledArrowKey || undefined,
    "data-disabled-months": disabledMonths || undefined,
    "aria-selected": isSelected,
    "aria-disabled": disabled,
    "aria-describedby": disabled ? 'withShift' : undefined
  }, rest), /*#__PURE__*/React__default.createElement(StyledItem, {
    className: cx(sideInRangeClass, sideInRangeLeft, sideInRangeRight, inRangeStyle),
    offset: sideOffset
  }, children), /*#__PURE__*/React__default.createElement(StyledEvents, {
    "aria-hidden": true
  }, [eventList[0], eventList[1], eventList[2]].map(function (event, i) {
    return event && /*#__PURE__*/React__default.createElement(StyledEvent, {
      key: "".concat(event.date, "-").concat(i),
      color: event.color
    });
  })));
}));

export { DateStructureItem };
