'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _instance = require('./instance.js');

var _instance2 = _interopRequireDefault(_instance);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// require (PrototypeDate.js), 使用了format
var Calendar = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$type = _ref.type,
      type = _ref$type === undefined ? 'month' : _ref$type,
      _ref$activeTo = _ref.activeTo,
      activeTo = _ref$activeTo === undefined ? '' : _ref$activeTo,
      defaultDate = _ref.defaultDate,
      titleFormat = _ref.titleFormat,
      disableBeforeDate = _ref.disableBeforeDate,
      disableAfterDate = _ref.disableAfterDate,
      verticalDrag = _ref.verticalDrag,
      _ref$prevHTML = _ref.prevHTML,
      prevHTML = _ref$prevHTML === undefined ? '&lt' : _ref$prevHTML,
      _ref$nextHTML = _ref.nextHTML,
      nextHTML = _ref$nextHTML === undefined ? '&gt' : _ref$nextHTML,
      onChange = _ref.onChange,
      onClick = _ref.onClick,
      onError = _ref.onError;

  var refEl = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.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 _instance2.default(refEl.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 _react2.default.createElement('div', { ref: refEl, className: 'calendar' });
});

exports.default = Calendar;
module.exports = exports['default'];