"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = layeredFilter;

var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));

var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _isLeafNode = _interopRequireDefault(require("./isLeafNode"));

function layeredFilter(array, filter) {
  return dfs(array);

  function dfs(rows) {
    var _context;

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

    return (0, _filter.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, filter);
  }
}