'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var DateStructureItem = require('../DateStructureItem/DateStructureItem.js');
var Calendar_tokens = require('../../Calendar.tokens.js');
var CalendarDays_styles = require('./CalendarDays.styles.js');
var useDays = require('../../hooks/useDays.js');
var calendarGridHelper = require('../../utils/calendarGridHelper.js');
var calendarRangeHelper = require('../../utils/calendarRangeHelper.js');
var constants = require('../../utils/constants.js');

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

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

/**
 * Компонент дней в календаре.
 */
var CalendarDays = function CalendarDays(_ref) {
  var currentDate = _ref.date,
    value = _ref.value,
    eventList = _ref.eventList,
    disabledList = _ref.disabledList,
    min = _ref.min,
    max = _ref.max,
    includeEdgeDates = _ref.includeEdgeDates,
    hoveredDay = _ref.hoveredDay,
    selectIndexes = _ref.selectIndexes,
    isDouble = _ref.isDouble,
    isSecond = _ref.isSecond,
    outerRefs = _ref.outerRefs,
    onChangeDay = _ref.onChangeDay,
    onHoverDay = _ref.onHoverDay,
    onTriggerEventTooltip = _ref.onTriggerEventTooltip,
    onSetSelected = _ref.onSetSelected,
    onKeyDown = _ref.onKeyDown,
    _ref$locale = _ref.locale,
    locale = _ref$locale === void 0 ? 'ru' : _ref$locale;
  var _useDays = useDays.useDays({
      date: currentDate,
      value: value,
      eventList: eventList,
      disabledList: disabledList,
      min: min,
      max: max,
      includeEdgeDates: includeEdgeDates
    }),
    _useDays2 = _rollupPluginBabelHelpers.slicedToArray(_useDays, 2),
    days = _useDays2[0],
    selected = _useDays2[1];
  var selectedRef = React.useRef(selected);
  var onSetSelectedRef = React.useRef(onSetSelected);
  var noopRef = React.useRef(null);
  var offset = isSecond ? constants.ROW_STEP : 0;
  var dayItemTokens = _rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty({}, "".concat(Calendar_tokens.innerTokens.dateStructureFontFamily), "var(".concat(Calendar_tokens.tokens.calendarDayFontFamily, ")")), "".concat(Calendar_tokens.innerTokens.dateStructureFontSize), "var(".concat(Calendar_tokens.tokens.calendarDayFontSize, ")")), "".concat(Calendar_tokens.innerTokens.dateStructureFontWeight), "var(".concat(Calendar_tokens.tokens.calendarDayFontWeight, ")")), "".concat(Calendar_tokens.innerTokens.dateStructureFontLineHeight), "var(".concat(Calendar_tokens.tokens.calendarDayFontLineHeight, ")")), "".concat(Calendar_tokens.innerTokens.dateStructureFontLetterSpacing), "var(".concat(Calendar_tokens.tokens.calendarDayFontLetterSpacing, ")")), "".concat(Calendar_tokens.innerTokens.dateStructureFontStyle), "var(".concat(Calendar_tokens.tokens.calendarDayFontStyle, ")")), "".concat(Calendar_tokens.innerTokens.dateStructureSelectedFontWeight), "var(".concat(Calendar_tokens.tokens.calendarDaySelectedFontWeight, ")")), "".concat(Calendar_tokens.innerTokens.dateStructureBorderRadius), "var(".concat(Calendar_tokens.tokens.calendarDayItemBorderRadius, ")"));
  var getSelectedDate = React.useCallback(function (event) {
    var _event$currentTarget$ = event.currentTarget.dataset,
      day = _event$currentTarget$.day,
      monthIndex = _event$currentTarget$.monthIndex,
      year = _event$currentTarget$.year;
    var selectedDate = {
      day: Number(day),
      monthIndex: Number(monthIndex),
      year: Number(year)
    };
    if (!calendarGridHelper.canSelectDate(selectedDate, value, disabledList)) {
      return;
    }
    return selectedDate;
  }, [disabledList, value]);
  var handleOnChangeDay = React.useCallback(function (i, j) {
    return function (event) {
      var selectedDate = getSelectedDate(event);
      if (!selectedDate) {
        return;
      }
      onChangeDay(selectedDate, [i + offset, j]);
      if (calendarRangeHelper.isSelectProcess(value)) {
        onHoverDay === null || onHoverDay === void 0 || onHoverDay(undefined);
      }
    };
  }, [getSelectedDate, onChangeDay, offset, value, onHoverDay]);
  var handleOnHoverDay = React.useCallback(function (event) {
    var selectedDate = getSelectedDate(event);
    var isSelectedDone = Array.isArray(value) && value[0] && value[1];
    if (!selectedDate || !Array.isArray(value) || isSelectedDone) {
      return;
    }
    onHoverDay === null || onHoverDay === void 0 || onHoverDay(selectedDate);
  }, [getSelectedDate, onHoverDay, value]);
  var handleMouseOutGrid = function handleMouseOutGrid() {
    onHoverDay === null || onHoverDay === void 0 || onHoverDay(undefined);
    onTriggerEventTooltip === null || onTriggerEventTooltip === void 0 || onTriggerEventTooltip(noopRef, []);
  };
  var getRefs = function getRefs(element, isDayInCurrentMonth, i, j) {
    if (isDayInCurrentMonth) {
      if (!outerRefs.current[i + offset]) {
        outerRefs.current[i + offset] = [];
      }
      outerRefs.current[i + offset][j] = element;
    }
  };
  React.useEffect(function () {
    if (selectedRef.current) {
      var _onSetSelectedRef$cur;
      (_onSetSelectedRef$cur = onSetSelectedRef.current) === null || _onSetSelectedRef$cur === void 0 || _onSetSelectedRef$cur.call(onSetSelectedRef, selectedRef.current);
    }
  }, []);
  return /*#__PURE__*/React__default.default.createElement(CalendarDays_styles.StyledCalendarDays, {
    role: "grid",
    "aria-labelledby": "id-grid-label",
    onKeyDown: onKeyDown,
    onMouseLeave: handleMouseOutGrid
  }, /*#__PURE__*/React__default.default.createElement(CalendarDays_styles.StyledCalendarDaysHint, {
    id: "withShift"
  }, constants.I18N.navigationByShift[locale]), /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, constants.SHORT_DAY_NAMES[locale].map(function (name) {
    return /*#__PURE__*/React__default.default.createElement(DateStructureItem.DateStructureItem, {
      role: "columnheader",
      "aria-label": constants.FULL_DAY_NAMES[locale][name],
      key: name,
      dayOfWeek: true,
      day: name,
      style: dayItemTokens
    }, name);
  })), days.map(function (day, i) {
    return /*#__PURE__*/React__default.default.createElement(React.Fragment, {
      key: i
    }, day.map(function (_ref2, j) {
      var date = _ref2.date,
        events = _ref2.events,
        disabled = _ref2.disabled,
        isSelected = _ref2.isSelected,
        isCurrent = _ref2.isCurrent,
        isDayInCurrentMonth = _ref2.isDayInCurrentMonth,
        inRange = _ref2.inRange,
        _ref2$isOutOfMinMaxRa = _ref2.isOutOfMinMaxRange,
        isOutOfMinMaxRange = _ref2$isOutOfMinMaxRa === void 0 ? false : _ref2$isOutOfMinMaxRa,
        disabledArrowKey = _ref2.disabledArrowKey,
        disabledDates = _ref2.disabledDates;
      return /*#__PURE__*/React__default.default.createElement(DateStructureItem.DateStructureItem, {
        ref: function ref(element) {
          return getRefs(element, Boolean(isDayInCurrentMonth), i, j);
        },
        style: dayItemTokens,
        eventList: events,
        disabled: disabled,
        day: date.day,
        year: date.year,
        monthIndex: date.monthIndex,
        isFocused: i + offset === (selectIndexes === null || selectIndexes === void 0 ? void 0 : selectIndexes[0]) && j === (selectIndexes === null || selectIndexes === void 0 ? void 0 : selectIndexes[1]) && !isOutOfMinMaxRange,
        isSelected: isSelected,
        isCurrent: isCurrent,
        isDayInCurrentMonth: isDayInCurrentMonth,
        isDouble: isDouble,
        isHovered: calendarRangeHelper.isSameDay(date, hoveredDay),
        inRange: calendarRangeHelper.getInRange(value, date, hoveredDay, inRange),
        sideInRange: calendarRangeHelper.getSideInRange(value, date, hoveredDay, isSelected),
        onClick: disabled ? undefined : handleOnChangeDay(i, j),
        onMouseOver: disabled ? undefined : handleOnHoverDay,
        onTriggerEventTooltip: onTriggerEventTooltip,
        key: "StyledDay-".concat(i, "-").concat(j),
        role: "gridcell",
        disabledArrowKey: disabledArrowKey,
        disabledMonths: disabledDates
      }, date.day);
    }));
  }));
};

exports.CalendarDays = CalendarDays;
