"use strict";

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

var React = _interopRequireWildcard(require("react"));

var _reactIs = require("react-is");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

var flattenFragments = function flattenFragments(children, ChildWrapper) {
  var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  return React.Children.toArray(children).reduce(function (acc, child, i) {
    if ((0, _reactIs.isFragment)(child)) {
      acc.push.apply(acc, _toConsumableArray(flattenFragments(child.props.children, ChildWrapper, depth + 1)));
    } else if (React.isValidElement(child)) {
      if (ChildWrapper) {
        acc.push(React.createElement(ChildWrapper, {
          key: "".concat(depth, ".").concat(i)
        }, child));
      } else {
        acc.push(child);
      }
    }

    return acc;
  }, []);
};

exports.flattenFragments = flattenFragments;