var _excluded = ["onSelect", "onUserSelect", "routerConfig", "treeColWidth", "request", "renderSearchExtra"],
  _excluded2 = ["request", "extendModuleMap"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 _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 React, { useContext } from 'react';
// import { RouteComponentProps, Switch, Route } from 'react-router-dom';
import { Routes, Route } from 'react-router-dom';
import Tree from "./tree";
import { PropTypes } from "./interface";
import { Spin, Permission, EmptyData } from "../blank";
import useComposeTree, { SPLITTER, ComposeTreeContext } from "./hooks/use-compose-tree";
import "./index.less";
function ComposeTree(_ref) {
  var onSelect = _ref.onSelect,
    onUserSelect = _ref.onUserSelect,
    routerConfig = _ref.routerConfig,
    _ref$treeColWidth = _ref.treeColWidth,
    treeColWidth = _ref$treeColWidth === void 0 ? '260px' : _ref$treeColWidth,
    request = _ref.request,
    renderSearchExtra = _ref.renderSearchExtra,
    others = _objectWithoutProperties(_ref, _excluded);
  var _useContext = useContext(ComposeTreeContext),
    goToModule = _useContext.goToModule,
    code = _useContext.code,
    dataSource = _useContext.dataSource,
    getRoutePath = _useContext.getRoutePath;
  // 之所以要传入 dataSource，是因为有可能内部展示的是搜索的结果，与当前页面
  var handleSelect = function handleSelect(nextSelectedKeys, event, currentDataSource) {
    // onSelect && onSelect(selectedKeys, event);
    // 禁止反选
    if (nextSelectedKeys.length === 0) return;
    onSelect && onSelect(nextSelectedKeys, event);
    var _event$node = event.node,
      type = _event$node.type,
      pos = _event$node.pos;
    // console.log(event, 'event');
    // setSelectedTreeNode({ key: selectedKeys[0], node: event.node});
    if (!type) {
      console.error('树节点缺乏 type 属性', event.node);
    }
    // 拼接路径
    var position = pos.split('-').slice(1);
    var traverser = currentDataSource;
    // console.log(traverser, 'traverser', position);
    // 将下标路径转换为 key 的路径，放入 url
    try {
      var treePath = position.map(function (pos) {
        var _traverser$pos = traverser[+pos],
          id = _traverser$pos.id,
          children = _traverser$pos.children;
        traverser = children;
        return id;
      }).join(SPLITTER);
      goToModule(event.node, treePath);
    } catch (error) {
      console.log(error);
    }
  };
  var $routes = (routerConfig || []).reduce(function (result, _ref2) {
    var modulePath = _ref2.path,
      Com = _ref2.Com;
    var _getRoutePath = getRoutePath(modulePath),
      path = _getRoutePath.path,
      purePath = _getRoutePath.purePath;
    // debugger;
    if (!Com) return null;
    return [].concat(_toConsumableArray(result), [/*#__PURE__*/React.createElement(Route, {
      key: purePath,
      path: path,
      render: function render(props) {
        return /*#__PURE__*/React.createElement(React.Suspense, {
          fallback: /*#__PURE__*/React.createElement(Spin, null)
        }, /*#__PURE__*/React.createElement(Com, props));
      }
    }), /*#__PURE__*/React.createElement(Route, {
      key: purePath,
      path: "".concat(path, "/*"),
      render: function render(props) {
        return /*#__PURE__*/React.createElement(React.Suspense, {
          fallback: /*#__PURE__*/React.createElement(Spin, null)
        }, /*#__PURE__*/React.createElement(Com, props));
      }
    })]);
  }, []);

  // console.log(dataSource, 'dataSource', code);
  return /*#__PURE__*/React.createElement("div", {
    className: "cf-compose-tree"
  }, /*#__PURE__*/React.createElement("div", {
    className: "tree-col",
    style: {
      width: treeColWidth
    }
  }, /*#__PURE__*/React.createElement(Tree, _extends({}, others, {
    request: request,
    dataSource: dataSource,
    permissionCode: code,
    onSelect: handleSelect,
    onUserSelect: onUserSelect,
    renderSearchExtra: renderSearchExtra
  }))), /*#__PURE__*/React.createElement("div", {
    className: "content-col"
  }, /*#__PURE__*/React.createElement(Routes, null, $routes, /*#__PURE__*/React.createElement(Route, {
    render: code === 30512 ? Permission : code === 9000 ? EmptyData : Spin
  }))));
}
export default (function (_ref3) {
  var request = _ref3.request,
    extendModuleMap = _ref3.extendModuleMap,
    others = _objectWithoutProperties(_ref3, _excluded2);
  // const { treePath } = useParams<{ treePath: string }>();
  var composeTreeContext = useComposeTree({
    request: {
      getTreeNodes: request.getTreeNodes
    },
    extendModuleMap: extendModuleMap
    // treePath
  });
  return /*#__PURE__*/React.createElement(ComposeTreeContext.Provider, {
    value: composeTreeContext
  }, /*#__PURE__*/React.createElement(ComposeTree, _extends({}, others, {
    request: request
  })));
});
export { SPLITTER, ComposeTreeContext, PropTypes };