import _extends from "@babel/runtime-corejs3/helpers/extends";
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
import isLeafNode from './isLeafNode';
export default function layeredFilter(array, filter) {
  return dfs(array);

  function dfs(rows) {
    var _context;

    if (!Array.isArray(array)) {
      return array;
    }

    return _filterInstanceProperty(_context = _mapInstanceProperty(rows).call(rows, function (row) {
      if (isLeafNode(row)) {
        return row;
      }

      return _extends(_extends({}, row), {
        children: dfs(row.children)
      });
    })).call(_context, filter);
  }
}