"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _moment = _interopRequireDefault(require("moment"));

var _reactVirtualized = require("react-virtualized");

var _context = _interopRequireDefault(require("../../../context"));

var _utils = require("../../../utils");

var _useRowsData = _interopRequireDefault(require("./useRowsData"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var DATE_ROW_NUM = 6;
var DATE_CELL_HEIGHT = 32;
var DATE_PANEL_WIDTH = 240;
var ROW_INDEX_DIFF = 3;

var DatePanelRows = function DatePanelRows(props) {
  var enableScrollHandler = props.enableScrollHandler,
      mergeDate = props.mergeDate,
      onSelect = props.onSelect,
      setEnableScrollHandler = props.setEnableScrollHandler,
      setMergeDate = props.setMergeDate;

  var _useContext = (0, _react.useContext)(_context["default"]),
      format = _useContext.format,
      max = _useContext.max,
      min = _useContext.min,
      value = _useContext.value;

  var listRef = (0, _react.useRef)();
  var rowsData = (0, _useRowsData["default"])({
    format: format,
    max: max,
    min: min
  }); // Gets the row that was first rendered in the viewport

  var scrollToIndex = (0, _react.useMemo)(function () {
    return (0, _moment["default"])(mergeDate).diff((0, _moment["default"])(min), 'weeks');
  }, [value]);

  var scrollToIndexHandler = function scrollToIndexHandler() {
    if (enableScrollHandler) return;
    var activatedDateIndex = (0, _moment["default"])(mergeDate).diff((0, _moment["default"])(min), 'weeks');
    var offset = listRef.current.getOffsetForRow({
      alignment: 'start',
      index: activatedDateIndex
    });

    listRef.current.Grid._scrollingContainer.scrollTo({
      top: offset,
      left: 0,
      behavior: 'smooth'
    });
  };

  var rowRenderer = (0, _react.useCallback)(function (_ref) {
    var key = _ref.key,
        index = _ref.index,
        isScrolling = _ref.isScrolling,
        isVisible = _ref.isVisible,
        style = _ref.style;
    var rowData = rowsData[index];

    if (!isScrolling && isVisible) {
      var _rowsData;

      setEnableScrollHandler(true);
      setMergeDate((_rowsData = rowsData[index - ROW_INDEX_DIFF]) === null || _rowsData === void 0 ? void 0 : _rowsData[0]);
    }

    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(_utils.PickerBodyClass, "__calendar-item")),
      key: key,
      style: style
    }, rowData.map(function (date) {
      var _classNames;

      var currentDate = (0, _moment["default"])(date);

      var _isCurrentDateInRange = (0, _utils.isCurrentDateInRanges)({
        nowDate: date,
        values: value
      }),
          inRanges = _isCurrentDateInRange.inRanges,
          rangeStart = _isCurrentDateInRange.rangeStart,
          rangeEnd = _isCurrentDateInRange.rangeEnd;

      var classes = (0, _classnames["default"])(_utils.PickerCellClass, (_classNames = {}, _defineProperty(_classNames, "".concat(_utils.PickerCellClass, "--today"), currentDate.isSame((0, _moment["default"])(), 'date')), _defineProperty(_classNames, "".concat(_utils.PickerCellClass, "--in-view"), currentDate.format('YYYY/MM') == (0, _moment["default"])(mergeDate).format('YYYY/MM') && isVisible), _defineProperty(_classNames, "".concat(_utils.PickerCellClass, "--in-ranges"), inRanges), _defineProperty(_classNames, "".concat(_utils.PickerCellClass, "--in-ranges--start"), rangeStart), _defineProperty(_classNames, "".concat(_utils.PickerCellClass, "--in-ranges--end"), rangeEnd), _defineProperty(_classNames, "".concat(_utils.PickerCellClass, "--selected"), value === null || value === void 0 ? void 0 : value.some(function (v) {
        return currentDate.isSame((0, _moment["default"])(v).format(_utils.PickerFormat), 'date');
      })), _classNames));
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classes,
        key: currentDate.format(_utils.PickerFormat)
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])("".concat(_utils.PickerCellClass, "__inner")),
        title: (0, _moment["default"])(currentDate).format('YYYY-MM-DD'),
        onClick: function onClick() {
          return onSelect === null || onSelect === void 0 ? void 0 : onSelect(currentDate, 'date');
        }
      }, (0, _moment["default"])(currentDate).format('DD')));
    }));
  }, [mergeDate, rowsData, value]);
  (0, _react.useEffect)(function () {
    scrollToIndexHandler();
  }, [mergeDate]);
  return /*#__PURE__*/_react["default"].createElement(_reactVirtualized.List, {
    height: DATE_CELL_HEIGHT * DATE_ROW_NUM,
    overscanRowCount: DATE_ROW_NUM,
    ref: listRef,
    rowCount: rowsData.length,
    rowHeight: DATE_CELL_HEIGHT,
    rowRenderer: rowRenderer,
    scrollToIndex: scrollToIndex,
    width: DATE_PANEL_WIDTH
  });
};

var _default = /*#__PURE__*/(0, _react.memo)(DatePanelRows);

exports["default"] = _default;