"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.translateUsersToTreeNode = exports.translateGroupsToTreeNode = exports.translateDepartsToTreeNode = exports.toTree = exports.orderPersonBeforeDeparts = exports.loadPersonTreeNodes = void 0;

var _cloneDeep = _interopRequireDefault(require("clone-deep"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// 人员节点排序在部分节点前面
var orderPersonBeforeDeparts = function orderPersonBeforeDeparts(nodes) {
  // 排序，人员在上，部分在下
  for (var i = 0; i < nodes.length; i++) {
    if (nodes[i].nodeType == "depart" || nodes[i].nodeType == "group") {
      for (var j = i + 1; j < nodes.length; j++) {
        if (nodes[j].nodeType == "person") {
          // 找到需要替换位置的人员节点
          var temp = nodes[i];
          nodes[i] = nodes[j];
          nodes[j] = temp;
        }
      }
    }
  }
}; // 扁平数据转换为树


exports.orderPersonBeforeDeparts = orderPersonBeforeDeparts;

var toTree = function toTree(data) {
  var cloneData = (0, _cloneDeep["default"])(data);
  var map = {};
  var res = [];
  cloneData.forEach(function (item) {
    return map[item.id] = item;
  });
  cloneData.forEach(function (item) {
    var parent = map[item.pid];

    if (parent) {
      if (!parent.children) parent.children = [];
      parent.children.push(item);
    } else {
      res.push(item);
    }
  });
  return res;
};

exports.toTree = toTree;

var translateDepartsToTreeNode = function translateDepartsToTreeNode() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var res = [];
  data.forEach(function (item) {
    var node = {
      key: "".concat(item.id),
      id: "".concat(item.id),
      pid: "".concat(item.pid),
      title: item.name,
      nodeType: "depart",
      async: item.hasChild,
      isLeaf: !item.hasChild,
      data: _objectSpread({}, item)
    };
    res.push(node);
  });
  return res;
};

exports.translateDepartsToTreeNode = translateDepartsToTreeNode;

var translateUsersToTreeNode = function translateUsersToTreeNode() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var pid = arguments.length > 1 ? arguments[1] : undefined;
  var res = [];
  data.forEach(function (item) {
    var node = {
      key: "".concat(item.userId),
      id: "".concat(item.userId),
      pid: "".concat(pid),
      title: item.name,
      nodeType: "person",
      isLeaf: true,
      // 告诉Tree组件这个Node需要异步加载子节点
      data: _objectSpread({}, item)
    };
    res.push(node);
  });
  return res;
};

exports.translateUsersToTreeNode = translateUsersToTreeNode;

var loadPersonTreeNodes = function loadPersonTreeNodes(data, _ref) {
  var pid = _ref.pid;
  var res = [];
  data.forEach(function (item) {
    res.push({
      key: "".concat(item.Id),
      id: "".concat(item.Id),
      pid: "".concat(pid),
      nodeType: "person",
      isLeaf: true,
      data: _objectSpread({}, item)
    });
  });
  return res;
};

exports.loadPersonTreeNodes = loadPersonTreeNodes;

var translateGroupsToTreeNode = function translateGroupsToTreeNode() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var res = [];
  data.forEach(function (item) {
    var node = {
      key: "".concat(item.id),
      id: "".concat(item.id),
      pid: "+0",
      title: item.name,
      nodeType: "group",
      isLeaf: true,
      data: _objectSpread({}, item)
    };
    res.push(node);
  });
  return res;
};

exports.translateGroupsToTreeNode = translateGroupsToTreeNode;