"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

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

var _treeNode = _interopRequireDefault(require("./treeNode"));

var _treeFunctions = require("./utils/treeFunctions");

var _traverseUtils = require("./utils/traverseUtils");

var _utils = require("./utils/_utils");

function getNextLevelArray(treeNodesLevelArray, expandedNode) {
  var nextLevel = expandedNode.level + 1; // 往下找children，展开当前节点的下一个节点

  if (expandedNode.children) {
    treeNodesLevelArray[nextLevel] = expandedNode.children;
  } else {
    treeNodesLevelArray[nextLevel] = [];
  }

  treeNodesLevelArray[nextLevel + 1] = [];
  return treeNodesLevelArray;
}

function getTreeNodesLevelArray(treeData) {
  var fieldNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _utils.__DEFAULT_FIELDNAMES__;
  var keyToTreeNodeMapRef = arguments.length > 2 ? arguments[2] : undefined;
  var numberKeysSetRef = arguments.length > 3 ? arguments[3] : undefined;
  var expandedKey = arguments.length > 4 ? arguments[4] : undefined;
  var temp = [];
  var data = (0, _traverseUtils.traverseTreeNode)(treeData, fieldNames, keyToTreeNodeMapRef.current, numberKeysSetRef.current, true);
  temp[0] = data;
  if (!expandedKey) return temp;
  var expandedNode = keyToTreeNodeMapRef.current.get(expandedKey);

  if (expandedNode) {
    var curNode = expandedNode;
    var curLevel = expandedNode.level; // 往下找children，展开当前节点的下一个节点

    if (curNode.children) {
      temp[curLevel + 1] = curNode.children;
    } // 往上找parent，展开当前节点的所有父节点


    while (curNode.parent && curLevel > 0) {
      temp[curLevel] = curNode.parent.children;
      curNode = curNode.parent;
      curLevel -= 1;
    }
  }

  return temp;
}

var HorizontalTreeRenderFunction = function HorizontalTreeRenderFunction(props, ref) {
  var treeData = props.treeData,
      style = props.style,
      className = props.className,
      columnWidth = props.columnWidth,
      showIcon = props.showIcon,
      _props$fieldNames = props.fieldNames,
      fieldNames = _props$fieldNames === void 0 ? _utils.__DEFAULT_FIELDNAMES__ : _props$fieldNames,
      _props$checkedKeys = props.checkedKeys,
      outerCheckedKeys = _props$checkedKeys === void 0 ? [[]] : _props$checkedKeys,
      _props$defaultChecked = props.defaultCheckedKeys,
      defaultCheckedKeys = _props$defaultChecked === void 0 ? [[]] : _props$defaultChecked,
      _props$checkable = props.checkable,
      checkable = _props$checkable === void 0 ? true : _props$checkable,
      _props$columnStyle = props.columnStyle,
      columnStyle = _props$columnStyle === void 0 ? {} : _props$columnStyle,
      onSelect = props.onSelect,
      onCheck = props.onCheck,
      loadData = props.loadData,
      prefixCls = props.prefixCls;
  var endFieldNames = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _utils.__DEFAULT_FIELDNAMES__), fieldNames); // 每一层保存一个selectedkey

  var _React$useState = _react.default.useState([]),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      selectedKeys = _React$useState2[0],
      setSelectedKeys = _React$useState2[1];

  var _React$useState3 = _react.default.useState(new Set()),
      _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
      halfCheckedKeys = _React$useState4[0],
      setHalfCheckedKeys = _React$useState4[1];

  var _React$useState5 = _react.default.useState((0, _utils.calcEndCheckedKeys)(outerCheckedKeys || defaultCheckedKeys)),
      _React$useState6 = (0, _slicedToArray2.default)(_React$useState5, 2),
      checkedKeys = _React$useState6[0],
      setCheckedKeys = _React$useState6[1]; // const [expandedKeys, setExpandedKeys] = React.useState<Key[]>(
  //   outerExpandedKeys || defaultExpandedKeys,
  // );


  var _React$useState7 = _react.default.useState(null),
      _React$useState8 = (0, _slicedToArray2.default)(_React$useState7, 2),
      curLoadingKey = _React$useState8[0],
      setCurLoadingKey = _React$useState8[1];

  var keyToTreeNodeMapRef = _react.default.useRef(new Map());

  var numberKeysSetRef = _react.default.useRef(new Set()); // 保存每个层级的节点数


  var _React$useState9 = _react.default.useState(treeData ? getTreeNodesLevelArray(treeData, endFieldNames, keyToTreeNodeMapRef, numberKeysSetRef) : []),
      _React$useState10 = (0, _slicedToArray2.default)(_React$useState9, 2),
      treeNodesLevelArray = _React$useState10[0],
      setTreeNodesLevelArray = _React$useState10[1]; // 当前操作的key


  var currentKeyRef = _react.default.useRef(); // 保存上一次的treeData


  var prevTreeDataRef = _react.default.useRef(); // 保存上一次的checkedKeys


  var prevCheckedKeysRef = _react.default.useRef(); // 保存当前选中的keys


  var checkedKeysRef = _react.default.useRef(checkedKeys); // tree dom


  var horizontalTreeRef = _react.default.useRef(null);

  if (ref) {
    horizontalTreeRef = ref;
  } // 按需加载时更新数据


  var updateNodesLevelArray = function updateNodesLevelArray(expandeds) {
    var lastKey = expandeds ? expandeds[expandeds.length - 1] : undefined;
    var levelArr = getTreeNodesLevelArray(treeData || [], endFieldNames, keyToTreeNodeMapRef, numberKeysSetRef, lastKey);
    setTreeNodesLevelArray(levelArr);
  };

  var updateKeys = function updateKeys(updateCheckedKeys) {
    var _getNodeKeys = (0, _treeFunctions.getNodeKeys)(updateCheckedKeys, keyToTreeNodeMapRef.current),
        _getNodeKeys2 = (0, _slicedToArray2.default)(_getNodeKeys, 2),
        endKeys = _getNodeKeys2[0],
        halfKeys = _getNodeKeys2[1];

    setCheckedKeys(endKeys);
    setHalfCheckedKeys(halfKeys);
    return endKeys;
  };

  if ('treeData' in props && prevTreeDataRef.current !== treeData) {
    updateNodesLevelArray([currentKeyRef.current]);
  }

  if ('checkedKeys' in props && outerCheckedKeys !== prevCheckedKeysRef.current) {
    var endCheckedKeys = (0, _utils.calcEndCheckedKeys)(outerCheckedKeys); // if (endCheckedKeys.size === 0) {
    //   setCheckedKeys(endCheckedKeys);
    // } else {

    updateKeys(endCheckedKeys); // }
  } // if ('expandedKeys' in props && expandedKeys !== outerExpandedKeys) {
  //   setExpandedKeys(outerExpandedKeys);
  // }


  prevTreeDataRef.current = treeData;
  prevCheckedKeysRef.current = outerCheckedKeys;
  checkedKeysRef.current = checkedKeys;

  var nodeSelect = function nodeSelect(e, treeNode, checkedKeysSelected) {
    var cKeysSelected = [];

    if (checkedKeysSelected) {
      var _getExportCheckedKeys = (0, _treeFunctions.getExportCheckedKeys)(checkedKeysSelected, keyToTreeNodeMapRef.current),
          _getExportCheckedKeys2 = (0, _slicedToArray2.default)(_getExportCheckedKeys, 2),
          exportCheckedKeys = _getExportCheckedKeys2[0],
          exportFilterAnCheckedKeys = _getExportCheckedKeys2[1]; // cKeysSelected = [...cKeysSelected].map(
      //   (key) => getTrulyKey(key, numberKeysSetRef.current)[0],
      // );


      cKeysSelected = exportFilterAnCheckedKeys.map(function (key) {
        return (0, _utils.getTrulyKey)(key, numberKeysSetRef.current)[0];
      });
    }

    var _onTreeNodeSelect = (0, _treeFunctions.onTreeNodeSelect)(treeNode, new Set((0, _toConsumableArray2.default)(selectedKeys)), endFieldNames, keyToTreeNodeMapRef.current, false, numberKeysSetRef.current),
        _onTreeNodeSelect2 = (0, _slicedToArray2.default)(_onTreeNodeSelect, 2),
        keys = _onTreeNodeSelect2[0],
        selectedTreeNodes = _onTreeNodeSelect2[1];

    selectedKeys[treeNode.level || 0] = keys[0]; // 清除当前层级后面所有的key

    var newSelectedKeys = selectedKeys.slice(0, treeNode.level + 1);
    setSelectedKeys(newSelectedKeys);

    var _getTrulyKey = (0, _utils.getTrulyKey)(treeNode.nodeKey, numberKeysSetRef.current),
        _getTrulyKey2 = (0, _slicedToArray2.default)(_getTrulyKey, 1),
        exportKeys = _getTrulyKey2[0];

    onSelect === null || onSelect === void 0 ? void 0 : onSelect(exportKeys, {
      event: 'select',
      selected: !treeNode.selected,
      nativeEvent: e.nativeEvent,
      selectedNodes: selectedTreeNodes
    }, cKeysSelected);
  };

  var onNodeCheck = function onNodeCheck(e, treeNode) {
    var newCheckedKeysSet = new Set((0, _toConsumableArray2.default)(checkedKeys));

    var _onTreeNodeCheck = (0, _treeFunctions.onTreeNodeCheck)(treeNode, e.target.checked, newCheckedKeysSet, keyToTreeNodeMapRef.current),
        _onTreeNodeCheck2 = (0, _slicedToArray2.default)(_onTreeNodeCheck, 3),
        exportCheckedKeys = _onTreeNodeCheck2[0],
        exportFilterAnCheckedKeys = _onTreeNodeCheck2[1],
        halfKeysSet = _onTreeNodeCheck2[2];

    var getTargetKeys = function getTargetKeys(targetKeys) {
      return targetKeys.map(function (key) {
        return (0, _utils.getTrulyKey)(key, numberKeysSetRef.current)[0];
      });
    };

    var _getCheckedNodes = (0, _utils.getCheckedNodes)(treeNode, newCheckedKeysSet, fieldNames, keyToTreeNodeMapRef.current, numberKeysSetRef.current),
        _getCheckedNodes2 = (0, _slicedToArray2.default)(_getCheckedNodes, 1),
        checkedTreeNode = _getCheckedNodes2[0];

    if (!('checkedKeys' in props)) {
      setCheckedKeys(newCheckedKeysSet);
    }

    setHalfCheckedKeys(halfKeysSet);
    onCheck === null || onCheck === void 0 ? void 0 : onCheck(getTargetKeys(exportCheckedKeys), {
      checked: e.target.checked,
      checkedNodes: checkedTreeNode,
      event: 'check',
      nativeEvent: e.nativeEvent,
      checkedChildrenKeys: getTargetKeys(exportFilterAnCheckedKeys)
    });
  };

  var onNodeSelect = function onNodeSelect(e, treeNode) {
    var copyTreeNode = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, treeNode), {}, {
      key: (0, _utils.getTrulyKey)(treeNode.nodeKey, numberKeysSetRef.current)[1]
    });
    currentKeyRef.current = copyTreeNode.nodeKey; // 如果有children就代表数据已经加载过了

    if (loadData && !copyTreeNode.children && !copyTreeNode.isLeaf) {
      setCurLoadingKey(copyTreeNode.nodeKey);
      loadData(copyTreeNode, copyTreeNode.level + 1).then(function () {
        // 数据异步回来之后这里需要获取到最新的treeNode
        var newTreeNode = keyToTreeNodeMapRef.current.get(copyTreeNode.nodeKey);
        setCurLoadingKey(null);
        if (!newTreeNode) return;
        var endCheckedKeys = updateKeys(checkedKeysRef.current);
        nodeSelect(e, newTreeNode, endCheckedKeys);
      });
      return;
    }

    setTreeNodesLevelArray(getNextLevelArray((0, _toConsumableArray2.default)(treeNodesLevelArray), copyTreeNode));
    nodeSelect(e, copyTreeNode, checkedKeys);
  };

  var renderTreeNodes = function renderTreeNodes(treeNodes) {
    if (!treeNodes || !treeNodes.length) return null;
    return treeNodes === null || treeNodes === void 0 ? void 0 : treeNodes.map(function (item, index) {
      return /*#__PURE__*/_react.default.createElement(_treeNode.default, Object.assign({}, item, {
        // isLeaf={(!item.children && !loadData) || item.isLeaf}
        isLeaf: (0, _utils.getNodeIsLeaf)(item, !!loadData),
        key: item.nodeKey,
        showIcon: showIcon,
        // icon={icon}
        checkable: checkable,
        loading: loadData && item.nodeKey === curLoadingKey && !item.children,
        selected: selectedKeys.includes(item.nodeKey),
        checked: checkedKeys.has(item.nodeKey),
        indeterminate: halfCheckedKeys.has(item.nodeKey),
        onNodeSelect: onNodeSelect,
        onNodeCheck: onNodeCheck
      }));
    });
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: className,
    style: style,
    ref: horizontalTreeRef
  }, (treeNodesLevelArray === null || treeNodesLevelArray === void 0 ? void 0 : treeNodesLevelArray.length) ? treeNodesLevelArray.map(function (levelNodes, index) {
    return (levelNodes === null || levelNodes === void 0 ? void 0 : levelNodes.length) ? /*#__PURE__*/_react.default.createElement("div", {
      // eslint-disable-next-line react/no-array-index-key
      key: index,
      className: "".concat(prefixCls, "-nodes"),
      style: typeof columnWidth !== 'undefined' ? (0, _objectSpread2.default)((0, _objectSpread2.default)({}, columnStyle), {}, {
        width: columnWidth
      }) : columnStyle
    }, renderTreeNodes(levelNodes)) : null;
  }) : null);
};

var HorizontalTree = /*#__PURE__*/_react.default.forwardRef(HorizontalTreeRenderFunction);

var _default = HorizontalTree;
exports.default = _default;