'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
var List = require('../../vc-virtual-list/src/List.cjs');
var contextTypes = require('./contextTypes.cjs');
var MotionTreeNode = require('./MotionTreeNode.cjs');
var props = require('./props.cjs');
var diffUtil = require('./utils/diffUtil.cjs');
var treeUtil = require('./utils/treeUtil.cjs');
var omit = require('../../../utils/lang/omit.cjs');

const HIDDEN_STYLE = {
  width: 0,
  height: 0,
  display: "flex",
  overflow: "hidden",
  opacity: 0,
  border: 0,
  padding: 0,
  margin: 0
};
function noop() {
}
const MOTION_KEY = `RC_TREE_MOTION_${Math.random()}`;
const MotionNode = {
  key: MOTION_KEY
};
const MotionEntity = {
  key: MOTION_KEY,
  level: 0,
  index: 0,
  pos: "0",
  node: MotionNode,
  nodes: [MotionNode]
};
const MotionFlattenData = {
  parent: null,
  children: [],
  pos: MotionEntity.pos,
  data: MotionNode,
  title: null,
  key: MOTION_KEY,
  /** Hold empty list here since we do not use it */
  isStart: [],
  isEnd: []
};
function getMinimumRangeTransitionRange(list, virtual, height, itemHeight) {
  if (virtual === false || !height)
    return list;
  return list.slice(0, Math.ceil(height / itemHeight) + 1);
}
function itemKey(item) {
  const {
    key,
    pos
  } = item;
  return treeUtil.getKey(key, pos);
}
function getAccessibilityPath(item) {
  let path = String(item.key);
  let current = item;
  while (current.parent) {
    current = current.parent;
    path = `${current.key} > ${path}`;
  }
  return path;
}
var NodeList = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "NodeList",
  inheritAttrs: false,
  props: props.nodeListProps,
  setup(props, {
    expose,
    attrs
  }) {
    const listRef = vue.ref();
    const indentMeasurerRef = vue.ref();
    const {
      expandedKeys,
      flattenNodes
    } = contextTypes.useInjectKeysState();
    expose({
      scrollTo: (scroll) => {
        listRef.value.scrollTo(scroll);
      },
      getIndentWidth: () => indentMeasurerRef.value.offsetWidth
    });
    const transitionData = vue.shallowRef(flattenNodes.value);
    const transitionRange = vue.shallowRef([]);
    const motionType = vue.ref(null);
    function onMotionEnd() {
      transitionData.value = flattenNodes.value;
      transitionRange.value = [];
      motionType.value = null;
      props.onListChangeEnd();
    }
    const context = contextTypes.useInjectTreeContext();
    vue.watch([() => expandedKeys.value.slice(), flattenNodes], ([expandedKeys2, data], [prevExpandedKeys, prevData]) => {
      const diffExpanded = diffUtil.findExpandedKeys(prevExpandedKeys, expandedKeys2);
      if (diffExpanded.key !== null) {
        const {
          virtual,
          height,
          itemHeight
        } = props;
        if (diffExpanded.add) {
          const keyIndex = prevData.findIndex(({
            key
          }) => key === diffExpanded.key);
          const rangeNodes = getMinimumRangeTransitionRange(diffUtil.getExpandRange(prevData, data, diffExpanded.key), virtual, height, itemHeight);
          const newTransitionData = prevData.slice();
          newTransitionData.splice(keyIndex + 1, 0, MotionFlattenData);
          transitionData.value = newTransitionData;
          transitionRange.value = rangeNodes;
          motionType.value = "show";
        } else {
          const keyIndex = data.findIndex(({
            key
          }) => key === diffExpanded.key);
          const rangeNodes = getMinimumRangeTransitionRange(diffUtil.getExpandRange(data, prevData, diffExpanded.key), virtual, height, itemHeight);
          const newTransitionData = data.slice();
          newTransitionData.splice(keyIndex + 1, 0, MotionFlattenData);
          transitionData.value = newTransitionData;
          transitionRange.value = rangeNodes;
          motionType.value = "hide";
        }
      } else if (prevData !== data) {
        transitionData.value = data;
      }
    });
    vue.watch(() => context.value.dragging, (dragging) => {
      if (!dragging)
        onMotionEnd();
    });
    const mergedData = vue.computed(() => props.motion === void 0 ? transitionData.value : flattenNodes.value);
    const onActiveChange = () => {
      props.onActiveChange(null);
    };
    return () => {
      const {
        prefixCls,
        selectable,
        checkable,
        disabled,
        motion,
        height,
        itemHeight,
        virtual,
        focusable,
        activeItem,
        focused,
        tabindex,
        onKeydown,
        onFocus,
        onBlur,
        onListChangeStart,
        onListChangeEnd,
        ...domProps
      } = {
        ...props,
        ...attrs
      };
      return vue.createVNode(vue.Fragment, null, [focused && activeItem && vue.createVNode("span", {
        "style": HIDDEN_STYLE,
        "aria-live": "assertive"
      }, [getAccessibilityPath(activeItem)]), vue.createVNode("div", null, [vue.createVNode("input", {
        "style": HIDDEN_STYLE,
        "disabled": focusable === false || disabled,
        "tabindex": focusable !== false ? tabindex : null,
        "onKeydown": onKeydown,
        "onFocus": onFocus,
        "onBlur": onBlur,
        "value": "",
        "onChange": noop,
        "aria-label": "for screen reader"
      }, null)]), vue.createVNode("div", {
        "class": `${prefixCls}-treenode`,
        "aria-hidden": true,
        "style": {
          position: "absolute",
          pointerEvents: "none",
          visibility: "hidden",
          height: 0,
          overflow: "hidden"
        }
      }, [vue.createVNode("div", {
        "class": `${prefixCls}-indent`
      }, [vue.createVNode("div", {
        "ref": indentMeasurerRef,
        "class": `${prefixCls}-indent-unit`
      }, null)])]), vue.createVNode(List.default, {
        ...omit.omit(domProps, ["onActiveChange"]),
        "data": mergedData.value,
        "itemKey": itemKey,
        "height": height,
        "fullHeight": false,
        "virtual": virtual,
        "itemHeight": itemHeight,
        "prefixCls": `${prefixCls}-list`,
        "ref": listRef,
        "onVisibleChange": (originList, fullList) => {
          const originSet = new Set(originList);
          const restList = fullList.filter((item) => !originSet.has(item));
          if (restList.some((item) => itemKey(item) === MOTION_KEY))
            onMotionEnd();
        }
      }, {
        default: (treeNode) => {
          const {
            pos,
            data: {
              ...restProps
            },
            title,
            key,
            isStart,
            isEnd
          } = treeNode;
          const mergedKey = treeUtil.getKey(key, pos);
          delete restProps.key;
          delete restProps.children;
          return vue.createVNode(MotionTreeNode.default, {
            ...restProps,
            "eventKey": mergedKey,
            "title": title,
            "active": !!activeItem && key === activeItem.key,
            "data": treeNode.data,
            "isStart": isStart,
            "isEnd": isEnd,
            "motion": motion,
            "motionNodes": key === MOTION_KEY ? transitionRange.value : null,
            "motionType": motionType.value,
            "onMotionStart": onListChangeStart,
            "onMotionEnd": onMotionEnd,
            "onMousemove": onActiveChange
          }, null);
        }
      })]);
    };
  }
});

exports.MOTION_KEY = MOTION_KEY;
exports.MotionEntity = MotionEntity;
exports.default = NodeList;
exports.getMinimumRangeTransitionRange = getMinimumRangeTransitionRange;
//# sourceMappingURL=NodeList.cjs.map
