var _excluded = ["selected", "list", "onChange", "onExpandActive", "onClick", "onClickLeaf"];

function _extends() { _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; }; return _extends.apply(this, arguments); }

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

// require PrototypeArray.js
import React, { forwardRef, useRef, useImperativeHandle, useEffect } from 'react';
import Instance from './instance.js';
var MenuTree = forwardRef(function (_ref, ref) {
  var _ref$selected = _ref.selected,
      selected = _ref$selected === void 0 ? [] : _ref$selected,
      _ref$list = _ref.list,
      list = _ref$list === void 0 ? [] : _ref$list,
      onChange = _ref.onChange,
      onExpandActive = _ref.onExpandActive,
      onClick = _ref.onClick,
      onClickLeaf = _ref.onClickLeaf,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var rootRef = useRef(null);
  useImperativeHandle(ref, function () {
    return rootRef.current;
  });
  var instance = useRef(null);
  useEffect(function () {
    if (!instance.current) return;

    if (list && list.length) {
      if (selected && selected.length) {
        instance.current.params.selectedId = 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]); // eslint-disable-line

  useEffect(function () {
    if (instance.current) return;
    var data = Object.clone(list);

    if (JSON.stringify(data).indexOf('"children"') === -1) {
      data = data.deepTree();
    }

    instance.current = new Instance(rootRef.current, {
      data: data,
      selectedId: selected && selected.length ? selected[0].id : '',
      onExpandActive: onExpandActive ? true : null,
      onClick: click // (item, isActive, isExpand: true展开 | false收缩)

    });
    rootRef.current.instance = instance;
  }, []); // eslint-disable-line
  // 比较变化

  function usePrevious(value) {
    var ref = useRef();
    useEffect(function () {
      ref.current = value;
    });
    return ref.current;
  }

  var prevSelected = usePrevious(selected); // 比较选中是否不同, 不同更新树

  useEffect(function () {
    // console.log(prevSelected)
    // console.log(selected)
    if (!prevSelected || !selected || !Array.isArray(prevSelected)) return;
    if (!Array.isArray(selected) || !selected[0]) return;
    instance.current.params.selectedId = selected[0].id;
    instance.current.addSelected(selected[0]);
  }, [selected]); // eslint-disable-line
  // 更新句柄, 防止synchronization模式, 每次组件在render的时候都生成上次render的state、function、effects

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

  function click(s, item, isActived, isExpand) {
    if (rootRef.current) s.target = rootRef.current; // childrenCount

    var childrenCount = item.children && item.children.length ? item.children.length : 0; // 点击回调

    if (onClick) onClick(s, item.name, [item], isActived, isExpand, childrenCount);

    if (!isActived) {
      // 选中菜单变化
      if (onChange) onChange(s, item.name, [item]);
    } else if (!isExpand) {
      // 选中菜单展开
      if (onExpandActive) onExpandActive(s, item.name, [item]);
    }

    if (item.isLeaf === true && onClickLeaf) onClickLeaf(s, item.name, item, isActived, isExpand, childrenCount);
  }

  return /*#__PURE__*/React.createElement("ul", _extends({
    ref: rootRef
  }, others, {
    className: "menutree" + (others.className ? ' ' + others.className : '')
  }));
});
export default MenuTree;