import _useEffectState3 from "parsec-hooks/lib/effectStateHooks";
var _excluded = ["className", "current", "renderDot", "onChange", "itemCls", "renderDisable", "activeItemCls", "disableItemCls", "activeDotCls", "dotWrapCls", "limit", "renderDate", "listEndDay", "monthCls", "startDay", "renderItemProps", "dotCls"];

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

import classNames from 'classnames';
import styles from './index.module.less';
import { View } from 'remax/one';
import dayjs from 'dayjs';
import React, { useMemo } from 'react';
import weekday from 'dayjs/plugin/weekday';
dayjs.extend(weekday);
var weeks = ['日', '一', '二', '三', '四', '五', '六'];
export default (function (_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(dayjs(), '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 = _useEffectState3(useMemo(function () {
    return current || dayjs();
  }, [current])),
      _useEffectState2 = _slicedToArray(_useEffectState, 2),
      selected = _useEffectState2[0],
      setSelected = _useEffectState2[1];

  var startDay = useMemo(function () {
    return listEndDay ? dayjs().set('date', 1) : outStartDay || dayjs();
  }, [listEndDay, outStartDay]);
  limit = useMemo(function () {
    return listEndDay ? listEndDay.diff(dayjs(), 'day') + 1 : limit;
  }, [limit, listEndDay]);
  var days = useMemo(function () {
    return new Array(limit).fill(0).map(function (_, index) {
      return dayjs(startDay.subtract(startDay.day(), 'day').add(index, 'day').format('YYYY-MM-DD'));
    });
  }, [limit, startDay]);
  return /*#__PURE__*/React.createElement(View, _objectSpread({
    className: classNames(styles.calendar, className)
  }, props), weeks.map(function (item, index) {
    return /*#__PURE__*/React.createElement(View, {
      className: classNames(styles.item, itemCls, styles.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.createElement(View, {
          className: classNames(itemCls, styles.item, styles.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.createElement(React.Fragment, {
      key: index
    }, day.date() === 1 && listEndDay && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(View, {
      className: classNames(styles.month, monthCls)
    }, day.format('YYYY年MM月')), renderEmpty(true)), /*#__PURE__*/React.createElement(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: classNames(styles.item, itemCls, renderProps === null || renderProps === void 0 ? void 0 : renderProps.className, renderDisable(day) && classNames(styles.disable, disableItemCls), active && classNames(styles.active, activeItemCls))
    }), renderDate(day), /*#__PURE__*/React.createElement(View, {
      className: classNames(styles.dotWrap, dotWrapCls)
    }, dot === true ? /*#__PURE__*/React.createElement(View, {
      className: classNames(styles.dot, dotCls, active && classNames(styles.activeDot, activeDotCls))
    }, dot) : dot)), listEndDay && (day.month() !== dayjs(day).add(1, 'day').month() || days.length === index + 1) && renderEmpty(false));
  }));
});