'use strict';

var vue = require('vue');
require('../../../../utils/index.cjs');
var TreeNode = require('../TreeNode.cjs');
var props = require('../../../../utils/vue/props.cjs');
var log = require('../../../../utils/log.cjs');
var camelize = require('../../../../utils/lang/camelize.cjs');

function isTreeSelectNode(node) {
  return node && node.type && node.type.isTreeSelectNode;
}
function convertChildrenToData(rootNodes) {
  function dig(treeNodes = []) {
    return props.filterEmpty(treeNodes).map((treeNode) => {
      var _a, _b, _c;
      if (!isTreeSelectNode(treeNode)) {
        log.warning(!treeNode, "TreeSelect/TreeSelectNode can only accept TreeSelectNode as children.");
        return null;
      }
      const slots = treeNode.children || {};
      const key = treeNode.key;
      const props = {};
      for (const [k, v] of Object.entries(treeNode.props))
        props[camelize.camelize(k)] = v;
      const {
        isLeaf,
        checkable,
        selectable,
        disabled,
        disableCheckbox
      } = props;
      const newProps = {
        isLeaf: isLeaf || isLeaf === "" || void 0,
        checkable: checkable || checkable === "" || void 0,
        selectable: selectable || selectable === "" || void 0,
        disabled: disabled || disabled === "" || void 0,
        disableCheckbox: disableCheckbox || disableCheckbox === "" || void 0
      };
      const slotsProps = {
        ...props,
        ...newProps
      };
      const {
        title = (_a = slots.title) == null ? void 0 : _a.call(slots, slotsProps),
        switcherIcon = (_b = slots.switcherIcon) == null ? void 0 : _b.call(slots, slotsProps),
        ...rest
      } = props;
      const children = (_c = slots.default) == null ? void 0 : _c.call(slots);
      const dataNode = {
        ...rest,
        title,
        switcherIcon,
        key,
        isLeaf,
        ...newProps
      };
      const parsedChildren = dig(children);
      if (parsedChildren.length)
        dataNode.children = parsedChildren;
      return dataNode;
    });
  }
  return dig(rootNodes);
}
function fillLegacyProps(dataNode) {
  if (!dataNode)
    return dataNode;
  const cloneNode = {
    ...dataNode
  };
  if (!("props" in cloneNode)) {
    Object.defineProperty(cloneNode, "props", {
      get() {
        log.warning(false, "New `vc-tree-select` not support return node instance as argument anymore. Please consider to remove `props` access.");
        return cloneNode;
      }
    });
  }
  return cloneNode;
}
function fillAdditionalInfo(extra, triggerValue, checkedValues, treeData, showPosition, fieldNames) {
  let triggerNode = null;
  let nodeList = null;
  function generateMap() {
    function dig(list, level = "0", parentIncluded = false) {
      return list.map((option, index) => {
        const pos = `${level}-${index}`;
        const value = option[fieldNames.value];
        const included = checkedValues.includes(value);
        const children = dig(option[fieldNames.children] || [], pos, included);
        const node = vue.createVNode(TreeNode.default, option, {
          default: () => [children.map((child) => child.node)]
        });
        if (triggerValue === value)
          triggerNode = node;
        if (included) {
          const checkedNode = {
            pos,
            node,
            children
          };
          if (!parentIncluded)
            nodeList.push(checkedNode);
          return checkedNode;
        }
        return null;
      }).filter((node) => node);
    }
    if (!nodeList) {
      nodeList = [];
      dig(treeData);
      nodeList.sort(({
        node: {
          props: {
            value: val1
          }
        }
      }, {
        node: {
          props: {
            value: val2
          }
        }
      }) => {
        const index1 = checkedValues.indexOf(val1);
        const index2 = checkedValues.indexOf(val2);
        return index1 - index2;
      });
    }
  }
  Object.defineProperty(extra, "triggerNode", {
    get() {
      log.warning(false, "`triggerNode` is deprecated. Please consider decoupling data with node.");
      generateMap();
      return triggerNode;
    }
  });
  Object.defineProperty(extra, "allCheckedNodes", {
    get() {
      log.warning(false, "`allCheckedNodes` is deprecated. Please consider decoupling data with node.");
      generateMap();
      if (showPosition)
        return nodeList;
      return nodeList.map(({
        node
      }) => node);
    }
  });
}

exports.convertChildrenToData = convertChildrenToData;
exports.fillAdditionalInfo = fillAdditionalInfo;
exports.fillLegacyProps = fillLegacyProps;
//# sourceMappingURL=legacyUtil.cjs.map
