'use strict';

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

var vue = require('vue');
var legacyUtil = require('../utils/legacyUtil.cjs');

function parseSimpleTreeData(treeData, { id, pId, rootPId }) {
  const keyNodes = {};
  const rootNodeList = [];
  const nodeList = treeData.map((node) => {
    const clone = { ...node };
    const key = clone[id];
    keyNodes[key] = clone;
    clone.key = clone.key || key;
    return clone;
  });
  nodeList.forEach((node) => {
    const parentKey = node[pId];
    const parent = keyNodes[parentKey];
    if (parent) {
      parent.children = parent.children || [];
      parent.children.push(node);
    }
    if (parentKey === rootPId || !parent && rootPId === null)
      rootNodeList.push(node);
  });
  return rootNodeList;
}
function useTreeData(treeData, children, simpleMode) {
  const mergedTreeData = vue.shallowRef();
  vue.watch(
    [simpleMode, treeData, children],
    () => {
      const simpleModeValue = simpleMode.value;
      if (treeData.value) {
        mergedTreeData.value = simpleMode.value ? parseSimpleTreeData(vue.toRaw(treeData.value), {
          id: "id",
          pId: "pId",
          rootPId: null,
          ...simpleModeValue !== true ? simpleModeValue : {}
        }) : vue.toRaw(treeData.value).slice();
      } else {
        mergedTreeData.value = legacyUtil.convertChildrenToData(vue.toRaw(children.value));
      }
    },
    { immediate: true, deep: true }
  );
  return mergedTreeData;
}

exports.default = useTreeData;
//# sourceMappingURL=useTreeData.cjs.map
