"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx = _interopRequireDefault(require("clsx"));

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

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

var _utils3 = require("../datetime-picker/utils");

var _CalendarDay = _interopRequireDefault(require("./CalendarDay"));

var _useRect = require("../hooks/use-rect");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

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; }

/* eslint-disable no-plusplus */
var CalenderMonth = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useState = (0, _react.useState)(),
      visible = _useState[0],
      setVisible = _useState[1];

  var daysRef = (0, _react.useRef)();

  var _useState2 = (0, _react.useState)(),
      monthRef = _useState2[0],
      setMonthRef = _useState2[1];

  var height = (0, _react.useRef)(0);

  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

  var _createNamespace = createNamespace('calendar', prefixCls),
      bem = _createNamespace[0];

  (0, _react.useEffect)(function () {
    if (monthRef) {
      height.current = (0, _useRect.getRect)(monthRef).height;
    }
  }, [monthRef]);
  var title = (0, _react.useMemo)(function () {
    var _locale$vanCalendar;

    return props.formatMonthTitle ? props.formatMonthTitle(props.date) : (_locale$vanCalendar = locale.vanCalendar).monthTitle.apply(_locale$vanCalendar, (0, _utils2.formatMonthTitle)(props.date));
  }, [props.date, props.formatMonthTitle]);
  var rowHeight = (0, _react.useMemo)(function () {
    return (0, _utils.addUnit)(props.rowHeight);
  }, [props.rowHeight]);
  var offset = (0, _react.useMemo)(function () {
    var realDay = props.date.getDay();

    if (props.firstDayOfWeek) {
      return (realDay + 7 - props.firstDayOfWeek) % 7;
    }

    return realDay;
  }, [props.date, props.firstDayOfWeek]);
  var totalDay = (0, _react.useMemo)(function () {
    return (0, _utils3.getMonthEndDay)(props.date.getFullYear(), props.date.getMonth() + 1);
  }, [props.date]);
  var shouldRender = (0, _react.useMemo)(function () {
    return visible || !props.lazyRender;
  }, [visible]);

  var getTitle = function getTitle() {
    return title;
  };

  var scrollIntoView = function scrollIntoView(body) {
    var el = props.showSubtitle ? daysRef.current : monthRef;
    var scrollTop = el.getBoundingClientRect().top - body.getBoundingClientRect().top + body.scrollTop;
    (0, _utils.setScrollTop)(body, scrollTop);
  };

  var getMultipleDayType = function getMultipleDayType(day) {
    var isSelected = function isSelected(date) {
      return props.currentDate.some(function (item) {
        return (0, _utils2.compareDay)(item, date) === 0;
      });
    };

    if (isSelected(day)) {
      var prevDay = (0, _utils2.getPrevDay)(day);
      var nextDay = (0, _utils2.getNextDay)(day);
      var prevSelected = isSelected(prevDay);
      var nextSelected = isSelected(nextDay);

      if (prevSelected && nextSelected) {
        return 'multiple-middle';
      }

      if (prevSelected) {
        return 'end';
      }

      if (nextSelected) {
        return 'start';
      }

      return 'multiple-selected';
    }

    return '';
  };

  var getRangeDayType = function getRangeDayType(day) {
    var _ref = props.currentDate,
        startDay = _ref[0],
        endDay = _ref[1];

    if (!startDay) {
      return '';
    }

    var compareToStart = (0, _utils2.compareDay)(day, startDay);

    if (!endDay) {
      return compareToStart === 0 ? 'start' : '';
    }

    var compareToEnd = (0, _utils2.compareDay)(day, endDay);

    if (props.allowSameDay && compareToStart === 0 && compareToEnd === 0) {
      return 'start-end';
    }

    if (compareToStart === 0) {
      return 'start';
    }

    if (compareToEnd === 0) {
      return 'end';
    }

    if (compareToStart > 0 && compareToEnd < 0) {
      return 'middle';
    }

    return '';
  };

  var getDayType = function getDayType(day) {
    var type = props.type,
        minDate = props.minDate,
        maxDate = props.maxDate,
        currentDate = props.currentDate;

    if ((0, _utils2.compareDay)(day, minDate) < 0 || (0, _utils2.compareDay)(day, maxDate) > 0) {
      return 'disabled';
    }

    if (currentDate === null) {
      return '';
    }

    if (Array.isArray(currentDate)) {
      if (type === 'multiple') {
        return getMultipleDayType(day);
      }

      if (type === 'range') {
        return getRangeDayType(day);
      }
    } else if (type === 'single') {
      return (0, _utils2.compareDay)(day, currentDate) === 0 ? 'selected' : '';
    }

    return '';
  };

  var getBottomInfo = function getBottomInfo(dayType) {
    if (props.type === 'range') {
      if (dayType === 'start') {
        return locale.vanCalendar.start;
      }

      if (dayType === 'end') {
        return locale.vanCalendar.end;
      }

      if (dayType === 'start-end') {
        return locale.vanCalendar.startEnd;
      }
    }

    return null;
  };

  var renderTitle = function renderTitle() {
    if (props.showMonthTitle) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('month-title'))
      }, title);
    }

    return null;
  };

  var renderMark = function renderMark() {
    if (props.showMark && shouldRender) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('month-mark'))
      }, props.date.getMonth() + 1);
    }

    return null;
  };

  var placeholders = (0, _react.useMemo)(function () {
    var count = Math.ceil((totalDay + offset) / 7);
    return Array(count).fill({
      type: 'placeholder'
    });
  }, [totalDay, offset]);
  var days = (0, _react.useMemo)(function () {
    var internalDays = [];
    var year = props.date.getFullYear();
    var month = props.date.getMonth();

    for (var day = 1; day <= totalDay; day++) {
      var date = new Date(year, month, day);
      var type = getDayType(date);
      var config = {
        date: date,
        type: type,
        text: day,
        bottomInfo: getBottomInfo(type)
      };

      if (props.formatter) {
        config = props.formatter(config);
      }

      internalDays.push(config);
    }

    return internalDays;
  }, [getDayType, props.date, props.formatter]);

  var renderDay = function renderDay(item, index) {
    return /*#__PURE__*/_react.default.createElement(_CalendarDay.default, {
      key: index,
      item: item,
      index: index,
      color: props.color,
      offset: offset,
      rowHeight: rowHeight,
      onClick: props.onClick,
      topInfoRender: props.topInfoRender,
      bottomInfoRender: props.bottomInfoRender
    });
  };

  var renderDays = function renderDays() {
    return /*#__PURE__*/_react.default.createElement("div", {
      ref: daysRef,
      role: "grid",
      className: (0, _clsx.default)(bem('days'))
    }, renderMark(), (shouldRender ? days : placeholders).map(renderDay));
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getTitle: getTitle,
      getHeight: function getHeight() {
        return height.current;
      },
      setVisible: setVisible,
      scrollIntoView: scrollIntoView
    };
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('month')),
    ref: setMonthRef
  }, renderTitle(), renderDays());
});
var _default = CalenderMonth;
exports.default = _default;