"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = layeredSort;
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));
var _sort = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/sort"));
var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));
var _isLeafNode = _interopRequireDefault(require("./isLeafNode"));
/** 对树状结构的数据进行排序.
 * layeredSort 是一个递归的过程，针对树上的每一个父节点，该函数都会重新对其子节点数组（children) 进行排序.
 * */
function layeredSort(array, compare) {
  return dfs(array);
  function dfs(rows) {
    var _context;
    if (!Array.isArray(array)) {
      return array;
    }
    return (0, _sort.default)(_context = (0, _map.default)(rows).call(rows, function (row) {
      if ((0, _isLeafNode.default)(row)) {
        return row;
      }
      return (0, _extends2.default)((0, _extends2.default)({}, row), {
        children: dfs(row.children)
      });
    })).call(_context, compare);
  }
}