"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toTree = void 0;

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

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

// 扁平数据转换为树
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;