"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useTreeData;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _warning = require("../../vc-util/warning");

var _vue = require("vue");

var _legacyUtil = require("../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 = (0, _objectSpread2.default)({}, 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;
          (0, _warning.warning)(false, "`key` or `value` with TreeNode must be the same or you can remove one of them. key: ".concat(key, ", value: ").concat(value, "."));
        }

        (0, _warning.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.
 */


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