"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 _getLocale = require("../../../../../utils/getLocale");

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

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

require("./index.css");

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

// const ROWS_HEIGHT = 32;
var DATE_ROW_NUM = 6;

var DatePanelBody = function DatePanelBody(props) {
  var viewDate = props.viewDate,
      _props$weekStartDay = props.weekStartDay,
      weekStartDay = _props$weekStartDay === void 0 ? 'SUNDAY' : _props$weekStartDay,
      onSelect = props.onSelect;

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

  var weekStartDaySun = weekStartDay === 'SUNDAY';

  var getCells = function getCells(sunday, currentMonth) {
    var dates = [];

    var _loop = function _loop(day) {
      var _classNames;

      var date = (0, _moment["default"])(sunday).add(day, 'd');

      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"), date.isSame((0, _moment["default"])(), 'date')), _defineProperty(_classNames, "".concat(_utils.PickerCellClass, "--in-view"), date.format('YYYY/MM') === currentMonth), _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 date.isSame((0, _moment["default"])(v).format('YYYY/MM/DD'), 'date');
      })), _classNames));
      dates.push( /*#__PURE__*/_react["default"].createElement("td", {
        className: classes,
        key: date.format('YYYY/MM/DD')
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])("".concat(_utils.PickerCellClass, "__inner")),
        title: date.format('YYYY-MM-DD'),
        onClick: function onClick() {
          return onSelect && onSelect(date, 'date');
        }
      }, date.date())));
    };

    for (var day = 0; day < _utils.WEEK_DAYS_COUNT; day += 1) {
      _loop(day);
    }

    return dates;
  };

  var getRows = function getRows(date) {
    var currentDate = (0, _moment["default"])(date);
    var monthFirstDay = currentDate.date(1);
    var currentMonth = monthFirstDay.format('YYYY/MM');
    var daysDiff = weekStartDaySun ? 0 : 1;
    var weekStartDay = monthFirstDay.subtract(monthFirstDay.day() - daysDiff, 'days');
    var rows = [];

    for (var row = 0; row < DATE_ROW_NUM; row += 1) {
      rows.push( /*#__PURE__*/_react["default"].createElement("tr", {
        key: row
      }, getCells(weekStartDay, currentMonth)));
      weekStartDay = weekStartDay.add(1, 'w');
    }

    return rows;
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(_utils.PickerBodyClass)
  }, /*#__PURE__*/_react["default"].createElement("table", {
    className: (0, _classnames["default"])("".concat(_utils.PickerClass, "-content"))
  }, /*#__PURE__*/_react["default"].createElement("thead", null, /*#__PURE__*/_react["default"].createElement("tr", null, weekStartDaySun && /*#__PURE__*/_react["default"].createElement("th", {
    className: _utils.PickerCellClass
  }, (0, _getLocale.getLocale)('Mod_Sun')), /*#__PURE__*/_react["default"].createElement("th", {
    className: _utils.PickerCellClass
  }, (0, _getLocale.getLocale)('Mod_Mon')), /*#__PURE__*/_react["default"].createElement("th", {
    className: _utils.PickerCellClass
  }, (0, _getLocale.getLocale)('Mod_Tues')), /*#__PURE__*/_react["default"].createElement("th", {
    className: _utils.PickerCellClass
  }, (0, _getLocale.getLocale)('Mod_Wed')), /*#__PURE__*/_react["default"].createElement("th", {
    className: _utils.PickerCellClass
  }, (0, _getLocale.getLocale)('Mod_Thur')), /*#__PURE__*/_react["default"].createElement("th", {
    className: _utils.PickerCellClass
  }, (0, _getLocale.getLocale)('Mod_Fri')), /*#__PURE__*/_react["default"].createElement("th", {
    className: _utils.PickerCellClass
  }, (0, _getLocale.getLocale)('Mod_Sat')), weekStartDay !== 'SUNDAY' && /*#__PURE__*/_react["default"].createElement("th", {
    className: _utils.PickerCellClass
  }, (0, _getLocale.getLocale)('Mod_Sun')))), /*#__PURE__*/_react["default"].createElement("tbody", null, getRows((0, _moment["default"])(viewDate)))));
};

DatePanelBody.defaultProps = {
  weekStartDay: 'SUNDAY'
};
var _default = DatePanelBody;
exports["default"] = _default;