import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { warning } from '../../vc-util/warning';
import { computed } from 'vue';
import { convertChildrenToData } from '../utils/legacyUtil';
var MAX_WARNING_TIMES = 10;

function parseSimpleTreeData(treeData, _ref) {
  var id = _ref.id,
      pId = _ref.pId,
      rootPId = _ref.rootPId;
  var keyNodes = {};
  var rootNodeList = []; // Fill in the map

  var nodeList = treeData.map(function (node) {
    var clone = _objectSpread({}, node);

    var key = clone[id];
    keyNodes[key] = clone;
    clone.key = clone.key || key;
    return clone;
  }); // Connect tree

  nodeList.forEach(function (node) {
    var parentKey = node[pId];
    var parent = keyNodes[parentKey]; // Fill parent

    if (parent) {
      parent.children = parent.children || [];
      parent.children.push(node);
    } // Fill root tree node


    if (parentKey === rootPId || !parent && rootPId === null) {
      rootNodeList.push(node);
    }
  });
  return rootNodeList;
}
/**
 * Format `treeData` with `value` & `key` which is used for calculation
 */


function formatTreeData(treeData, getLabelProp, fieldNames) {
  var warningTimes = 0;
  var valueSet = new Set(); // Field names

  var fieldValue = fieldNames.value,
      fieldChildren = fieldNames.children;

  function dig(dataNodes) {
    return (dataNodes || []).map(function (node) {
      var key = node.key,
          disableCheckbox = node.disableCheckbox,
          disabled = node.disabled;
      var value = node[fieldValue];
      var mergedValue = fieldValue in node ? value : key;
      var dataNode = {
        disableCheckbox: disableCheckbox,
        disabled: disabled,
        key: key !== null && key !== undefined ? key : mergedValue,
        value: mergedValue,
        title: getLabelProp(node),
        node: node,
        dataRef: node
      };

      if (node.slots) {
        dataNode.slots = node.slots;
      } // Check `key` & `value` and warning user


      if (process.env.NODE_ENV !== 'production') {
        if (key !== null && key !== undefined && value !== undefined && String(key) !== String(value) && warningTimes < MAX_WARNING_TIMES) {
          warningTimes += 1;
          warning(false, "`key` or `value` with TreeNode must be the same or you can remove one of them. key: ".concat(key, ", value: ").concat(value, "."));
        }

        warning(!valueSet.has(value), "Same `value` exist in the tree: ".concat(value));
        valueSet.add(value);
      }

      if (fieldChildren in node) {
        dataNode.children = dig(node[fieldChildren]);
      }

      return dataNode;
    });
  }

  return dig(treeData);
}
/**
 * Convert `treeData` or `children` into formatted `treeData`.
 * Will not re-calculate if `treeData` or `children` not change.
 */


export default function useTreeData(treeData, children, _ref2) {
  var getLabelProp = _ref2.getLabelProp,
      simpleMode = _ref2.simpleMode,
      fieldNames = _ref2.fieldNames;
  return computed(function () {
    if (treeData.value) {
      return formatTreeData(simpleMode.value ? parseSimpleTreeData(treeData.value, _objectSpread({
        id: 'id',
        pId: 'pId',
        rootPId: null
      }, simpleMode.value !== true ? simpleMode.value : {})) : treeData.value, getLabelProp, fieldNames.value);
    } else {
      return formatTreeData(convertChildrenToData(children.value), getLabelProp, fieldNames.value);
    }
  });
}