"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _instance = _interopRequireDefault(require("./instance.js"));

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

// require (PrototypeDate.js), 使用了format
var Calendar = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$type = _ref.type,
      type = _ref$type === void 0 ? 'month' : _ref$type,
      _ref$activeTo = _ref.activeTo,
      activeTo = _ref$activeTo === void 0 ? '' : _ref$activeTo,
      defaultDate = _ref.defaultDate,
      titleFormat = _ref.titleFormat,
      disableBeforeDate = _ref.disableBeforeDate,
      disableAfterDate = _ref.disableAfterDate,
      verticalDrag = _ref.verticalDrag,
      _ref$prevHTML = _ref.prevHTML,
      prevHTML = _ref$prevHTML === void 0 ? '&lt' : _ref$prevHTML,
      _ref$nextHTML = _ref.nextHTML,
      nextHTML = _ref$nextHTML === void 0 ? '&gt' : _ref$nextHTML,
      onChange = _ref.onChange,
      onClick = _ref.onClick,
      onError = _ref.onError;
  var rootRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return rootRef.current;
  });
  var instance = (0, _react.useRef)(null); // 更新句柄, 防止synchronization模式, 每次组件在render的时候都生成上次render的state、function、effects

  if (instance.current) {
    instance.current.params.onChange = change;
    instance.current.params.onClick = click;
    instance.current.params.onError = onError;
  }

  function change(s) {
    if (onChange) onChange(s, s.activeDate.format('YYYY-MM-DD'), s.activeDate);
  }

  function click(s) {
    if (onClick) onClick(s, s.selectedDate.format('YYYY-MM-DD'), s.selectedDate);
  }

  (0, _react.useEffect)(function () {
    if (instance.current) return;
    instance.current = new _instance["default"](rootRef.current, {
      viewType: type,
      titleFormat: titleFormat,
      disableBeforeDate: disableBeforeDate,
      disableAfterDate: disableAfterDate,
      verticalDrag: verticalDrag,
      defaultDate: defaultDate,
      prevHTML: prevHTML,
      nextHTML: nextHTML,
      onChange: change,
      onClick: click,
      onError: onError // func(e, err)

    });
  }, []); // eslint-disable-line

  (0, _react.useEffect)(function () {
    if (!instance.current) return;

    if (type === 'month') {
      instance.current.showMonth();
    } else if (type === 'week') {
      instance.current.showWeek();
    }
  }, [type]);
  (0, _react.useEffect)(function () {
    if (!instance.current) return;

    if (activeTo instanceof Date) {
      instance.current.setDate(activeTo);
      return;
    }

    if (activeTo === 'today') {
      instance.current.setToday();
    } else if (activeTo === 'default') {
      instance.current.setDefaultDate();
    }
  }, [activeTo]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: rootRef,
    className: "calendar"
  });
});
var _default = Calendar;
exports["default"] = _default;
module.exports = exports.default;