import { inject, computed } from 'vue';
import { CASCADER_PROVIDE_KEY } from './props';
import { ROOT_MENU_KEY } from './const';

function useCascaderMenu(props) {
  const root = inject(CASCADER_PROVIDE_KEY);
  const menuNodes = computed(() => {
    let nodes = [];
    if (props.menuKey === ROOT_MENU_KEY) {
      nodes = root.transformData.value.filter(value => {
        const node = root.nodeList[value];
        return node.indexPath.length === 1;
      }).map(value => root.nodeList[value]);
    } else {
      var _root$nodeList$props$;
      nodes = ((_root$nodeList$props$ = root.nodeList[props.menuKey]) === null || _root$nodeList$props$ === void 0 ? void 0 : _root$nodeList$props$.childrenValues.map(value => root.nodeList[value])) || [];
    }
    return nodes;
  });
  const isCascaderOpened = computed(() => root.props.isOpened);
  const menuScrollNode = computed(() => {
    return menuNodes.value.find(node => {
      if (root.props.selectable) {
        return root.hasActive(node.value, root.nodeList) || root.hasSelected(node.value);
      }
      if (root.props.checkable) {
        return root.hasChecked(node.value);
      }
      return false;
    });
  });
  return {
    menuNodes,
    isCascaderOpened,
    menuScrollNode
  };
}

export { useCascaderMenu as default };
