'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index = require('../../../../utils/index.js');
var Calendar_tokens = require('../../Calendar.tokens.js');
var DateStructureItem_styles = require('./DateStructureItem.styles.js');
var useForkRef = require('../../../../hooks/useForkRef.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

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__*/React.memo(/*#__PURE__*/React.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 = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
  var structureRef = React.useRef(null);
  var innerRef = useForkRef.useForkRef(outerRef, structureRef);
  var selectableClass = !dayOfWeek && !disabled ? Calendar_tokens.classes.selectableItem : undefined;
  var selectedClass = isDayInCurrentMonth && isSelected ? Calendar_tokens.classes.selectedItem : undefined;
  var currentClass = isDayInCurrentMonth && isCurrent ? Calendar_tokens.classes.currentItem : undefined;
  var hoveredClass = isDayInCurrentMonth && isHovered ? Calendar_tokens.classes.hoveredItem : undefined;
  var dayOfWeekClass = dayOfWeek ? Calendar_tokens.classes.dayOfWeek : undefined;
  var dayInCurrentMonthClass = isDayInCurrentMonth ? Calendar_tokens.classes.dayInCurrentMonth : undefined;
  var disabledClass = disabled && !isCurrent ? Calendar_tokens.classes.disabled : undefined;
  var disabledCurrentClass = disabled && isCurrent ? Calendar_tokens.classes.disabledCurrent : undefined;
  var sideInRangeLeft = isDayInCurrentMonth && sideInRange === 'left' ? Calendar_tokens.classes.sideLeft : undefined;
  var sideInRangeRight = isDayInCurrentMonth && sideInRange === 'right' ? Calendar_tokens.classes.sideRight : undefined;
  var sideInRangeClass = isDayInCurrentMonth && sideInRange ? Calendar_tokens.classes.sideInRange : undefined;
  var inRangeStyle = isDayInCurrentMonth && inRange ? Calendar_tokens.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.default.createElement(DateStructureItem_styles.StyledItemRoot, _rollupPluginBabelHelpers.extends({
    ref: innerRef,
    className: index.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.default.createElement(DateStructureItem_styles.StyledItem, {
    className: index.cx(sideInRangeClass, sideInRangeLeft, sideInRangeRight, inRangeStyle),
    offset: sideOffset
  }, children), /*#__PURE__*/React__default.default.createElement(DateStructureItem_styles.StyledEvents, {
    "aria-hidden": true
  }, [eventList[0], eventList[1], eventList[2]].map(function (event, i) {
    return event && /*#__PURE__*/React__default.default.createElement(DateStructureItem_styles.StyledEvent, {
      key: "".concat(event.date, "-").concat(i),
      color: event.color
    });
  })));
}));

exports.DateStructureItem = DateStructureItem;
