"use strict";

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

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

var _effectStateHooks = _interopRequireDefault(require("parsec-hooks/lib/effectStateHooks"));

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

var _indexModule = _interopRequireDefault(require("./index.module.less"));

var _one = require("remax/one");

var _dayjs = _interopRequireDefault(require("dayjs"));

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

var _weekday = _interopRequireDefault(require("dayjs/plugin/weekday"));

var _excluded = ["className", "current", "renderDot", "onChange", "itemCls", "renderDisable", "activeItemCls", "disableItemCls", "activeDotCls", "dotWrapCls", "limit", "renderDate", "listEndDay", "monthCls", "startDay", "renderItemProps", "dotCls"];

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

_dayjs.default.extend(_weekday.default);

var weeks = ['日', '一', '二', '三', '四', '五', '六'];

var _default = function _default(_ref) {
  var className = _ref.className,
      current = _ref.current,
      renderDot = _ref.renderDot,
      onChange = _ref.onChange,
      itemCls = _ref.itemCls,
      _ref$renderDisable = _ref.renderDisable,
      renderDisable = _ref$renderDisable === void 0 ? function (day) {
    return day.isBefore((0, _dayjs.default)(), 'date');
  } : _ref$renderDisable,
      activeItemCls = _ref.activeItemCls,
      disableItemCls = _ref.disableItemCls,
      activeDotCls = _ref.activeDotCls,
      dotWrapCls = _ref.dotWrapCls,
      _ref$limit = _ref.limit,
      limit = _ref$limit === void 0 ? 14 : _ref$limit,
      _ref$renderDate = _ref.renderDate,
      renderDate = _ref$renderDate === void 0 ? function (day) {
    return day.get('date');
  } : _ref$renderDate,
      listEndDay = _ref.listEndDay,
      monthCls = _ref.monthCls,
      outStartDay = _ref.startDay,
      renderItemProps = _ref.renderItemProps,
      dotCls = _ref.dotCls,
      props = _objectWithoutProperties(_ref, _excluded);

  var _useEffectState = (0, _effectStateHooks.default)((0, _react.useMemo)(function () {
    return current || (0, _dayjs.default)();
  }, [current])),
      _useEffectState2 = _slicedToArray(_useEffectState, 2),
      selected = _useEffectState2[0],
      setSelected = _useEffectState2[1];

  var startDay = (0, _react.useMemo)(function () {
    return listEndDay ? (0, _dayjs.default)().set('date', 1) : outStartDay || (0, _dayjs.default)();
  }, [listEndDay, outStartDay]);
  limit = (0, _react.useMemo)(function () {
    return listEndDay ? listEndDay.diff((0, _dayjs.default)(), 'day') + 1 : limit;
  }, [limit, listEndDay]);
  var days = (0, _react.useMemo)(function () {
    return new Array(limit).fill(0).map(function (_, index) {
      return (0, _dayjs.default)(startDay.subtract(startDay.day(), 'day').add(index, 'day').format('YYYY-MM-DD'));
    });
  }, [limit, startDay]);
  return /*#__PURE__*/_react.default.createElement(_one.View, _objectSpread({
    className: (0, _classnames.default)(_indexModule.default.calendar, className)
  }, props), weeks.map(function (item, index) {
    return /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)(_indexModule.default.item, itemCls, _indexModule.default.week),
      key: item,
      style: {
        marginRight: index === 6 ? 0 : undefined
      }
    }, item);
  }), days.map(function (day, index) {
    var dot = renderDot === null || renderDot === void 0 ? void 0 : renderDot(day, index);
    var active = day.isSame(selected, 'date');

    var renderEmpty = function renderEmpty() {
      var before = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
      var length = before ? day.weekday() : 7 - day.weekday() - 1;
      return new Array(length).fill(0).map(function (_, i) {
        return /*#__PURE__*/_react.default.createElement(_one.View, {
          className: (0, _classnames.default)(itemCls, _indexModule.default.item, _indexModule.default.empty),
          key: i,
          style: {
            marginRight: !before && i === length - 1 ? 0 : undefined
          }
        });
      });
    };

    var renderProps = renderItemProps === null || renderItemProps === void 0 ? void 0 : renderItemProps(day);

    if (listEndDay && day.month() === startDay.month() - 1) {
      return null;
    }

    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
      key: index
    }, day.date() === 1 && listEndDay && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)(_indexModule.default.month, monthCls)
    }, day.format('YYYY年MM月')), renderEmpty(true)), /*#__PURE__*/_react.default.createElement(_one.View, _objectSpread(_objectSpread({
      "wechat-aria-role": 'button',
      "wechat-aria-label": day.format('MM月DD日'),
      "wechat-aria-selected": active,
      "wechat-aria-disabled": renderDisable(day)
    }, renderProps), {}, {
      onTap: function onTap() {
        setSelected(day);
        onChange === null || onChange === void 0 ? void 0 : onChange(day);
      },
      style: _objectSpread({
        marginRight: day.weekday() === 6 ? 0 : undefined
      }, renderProps === null || renderProps === void 0 ? void 0 : renderProps.style),
      className: (0, _classnames.default)(_indexModule.default.item, itemCls, renderProps === null || renderProps === void 0 ? void 0 : renderProps.className, renderDisable(day) && (0, _classnames.default)(_indexModule.default.disable, disableItemCls), active && (0, _classnames.default)(_indexModule.default.active, activeItemCls))
    }), renderDate(day), /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)(_indexModule.default.dotWrap, dotWrapCls)
    }, dot === true ? /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)(_indexModule.default.dot, dotCls, active && (0, _classnames.default)(_indexModule.default.activeDot, activeDotCls))
    }, dot) : dot)), listEndDay && (day.month() !== (0, _dayjs.default)(day).add(1, 'day').month() || days.length === index + 1) && renderEmpty(false));
  }));
};

exports.default = _default;