'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } // require PrototypeArray.js


var MenuTree = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$selected = _ref.selected,
      selected = _ref$selected === undefined ? [] : _ref$selected,
      _ref$list = _ref.list,
      list = _ref$list === undefined ? [] : _ref$list,
      onChange = _ref.onChange,
      onClick = _ref.onClick,
      onClickLeaf = _ref.onClickLeaf,
      others = _objectWithoutProperties(_ref, ['selected', 'list', 'onChange', 'onClick', 'onClickLeaf']);

  var refEl = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });
  var instance = (0, _react.useRef)(null);

  (0, _react.useEffect)(function () {
    if (!instance.current) return;
    if (list && list.length) {
      if (selected && selected.length) {
        instance.current.setSelectedId(selected[0].id);
      }
      var data = Object.clone(list);
      if (JSON.stringify(data).indexOf('"children"') === -1) {
        data = data.deepTree();
      }
      instance.current.setData(data);
    } else {
      instance.current.setData([]);
    }
  }, [list]);

  (0, _react.useEffect)(function () {
    if (instance.current) return;
    var data = Object.clone(list);
    if (JSON.stringify(data).indexOf('"children"') === -1) {
      data = data.deepTree();
    }
    instance.current = new _instance2.default(refEl.current, {
      data: data,
      selectedId: selected && selected.length ? selected[0].id : '',
      onClick: click // (item, isActive, isExtend: true展开 | false收缩)
    });
  }, []);

  // 更新句柄, 防止synchronization模式, 每次组件在render的时候都生成上次render的state、function、effects
  if (instance.current) {
    instance.current.params.onClick = click;
  }
  function click(s, item, isActived, isExtend) {
    if (refEl.current) s.target = refEl.current;
    // childrenCount
    var childrenCount = item.children && item.children.length ? item.children.length : 0;

    // 点击回调
    if (onClick) onClick(s, item.name, item, isActived, isExtend, childrenCount);
    if (!isActived && onChange) onChange(s, item.name, [item]);
    if (item.isLeaf === true && onClickLeaf) onClickLeaf(s, item.name, item, isActived, isExtend, childrenCount);
  }

  return _react2.default.createElement('ul', _extends({ ref: refEl }, others, { className: 'menutree' + (others.className ? ' ' + others.className : '') }));
});

exports.default = _react2.default.memo(MenuTree, function (prevProps, nextProps) {
  if (JSON.stringify(prevProps.list) === JSON.stringify(nextProps.list)) return true;
  return false;
});
module.exports = exports['default'];