import { useNormalModel } from '../_util/use/useModel';
import { getCascadeChildrenByKeys } from './helper';

function useState(props, _ref) {
  let {
    emit
  } = _ref;
  const [currentExpandedKeys, updateExpandedKeys] = useNormalModel(props, emit, {
    prop: 'expandedKeys'
  });
  const [currentCheckedKeys, updateCheckedKeys] = useNormalModel(props, emit, {
    prop: 'checkedKeys'
  });
  const [currentSelectedKeys, updateSelectedKeys] = useNormalModel(props, emit, {
    prop: 'selectedKeys'
  });
  const hasSelected = value => currentSelectedKeys.value.includes(value);
  const hasChecked = value => currentCheckedKeys.value.includes(value);
  const hasLoaded = node => {
    // 兼容异步加载，未匹配到节点的情况
    if (node && !node.isLeaf && props.remote && props.loadData && !node.hasChildren) {
      return false;
    }
    return true;
  };
  // 选中需要判断子节点是否全部加载完毕
  const hasCheckLoaded = (value, nodeList) => {
    return getCascadeChildrenByKeys(nodeList, [value]).every(key => hasLoaded(nodeList[key]));
  };
  // 当前节点是否在选中节点的路径中
  const hasActive = (value, nodeList) => {
    return !hasSelected(value) && currentSelectedKeys.value.some(key => {
      var _nodeList$key;
      return (_nodeList$key = nodeList[key]) === null || _nodeList$key === void 0 ? void 0 : _nodeList$key.indexPath.includes(value);
    });
  };
  return {
    currentExpandedKeys,
    updateExpandedKeys,
    currentCheckedKeys,
    updateCheckedKeys,
    currentSelectedKeys,
    updateSelectedKeys,
    hasSelected,
    hasChecked,
    hasLoaded,
    hasCheckLoaded,
    hasActive
  };
}

export { useState as default };
