import _extends from "@babel/runtime-corejs3/helpers/extends";
import _sortInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/sort";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
import isLeafNode from './isLeafNode';
/** 对树状结构的数据进行排序.
 * layeredSort 是一个递归的过程，针对树上的每一个父节点，该函数都会重新对其子节点数组（children) 进行排序.
 * */
export default function layeredSort(array, compare) {
  return dfs(array);
  function dfs(rows) {
    var _context;
    if (!Array.isArray(array)) {
      return array;
    }
    return _sortInstanceProperty(_context = _mapInstanceProperty(rows).call(rows, function (row) {
      if (isLeafNode(row)) {
        return row;
      }
      return _extends(_extends({}, row), {
        children: dfs(row.children)
      });
    })).call(_context, compare);
  }
}