"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getRichTextValues = getRichTextValues;
var _element = require("@wordpress/element");
var _blocks = require("@wordpress/blocks");
var _richText = require("@wordpress/rich-text");
var _innerBlocks = _interopRequireDefault(require("../inner-blocks"));
var _content = require("./content");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

/*
 * This function is similar to `@wordpress/element`'s `renderToString` function,
 * except that it does not render the elements to a string, but instead collects
 * the values of all rich text `Content` elements.
 */function addValuesForElement(element, values, innerBlocks) {
  if (null === element || undefined === element || false === element) {
    return;
  }
  if (Array.isArray(element)) {
    return addValuesForElements(element, values, innerBlocks);
  }
  switch (typeof element) {
    case 'string':
    case 'number':
      return;
  }
  const {
    type,
    props
  } = element;
  switch (type) {
    case _element.StrictMode:
    case _element.Fragment:
      return addValuesForElements(props.children, values, innerBlocks);
    case _element.RawHTML:
      return;
    case _innerBlocks.default.Content:
      return addValuesForBlocks(values, innerBlocks);
    case _content.Content:
      values.push(props.value);
      return;
  }
  switch (typeof type) {
    case 'string':
      if (typeof props.children !== 'undefined') {
        return addValuesForElements(props.children, values, innerBlocks);
      }
      return;
    case 'function':
      const el = type.prototype && typeof type.prototype.render === 'function' ? new type(props).render() : type(props);
      return addValuesForElement(el, values, innerBlocks);
  }
}
function addValuesForElements(children, ...args) {
  children = Array.isArray(children) ? children : [children];
  for (let i = 0; i < children.length; i++) {
    addValuesForElement(children[i], ...args);
  }
}
function addValuesForBlocks(values, blocks) {
  for (let i = 0; i < blocks.length; i++) {
    const {
      name,
      attributes,
      innerBlocks
    } = blocks[i];
    const saveElement = (0, _blocks.getSaveElement)(name, attributes,
    /*#__PURE__*/
    // Instead of letting save elements use `useInnerBlocksProps.save`,
    // force them to use InnerBlocks.Content instead so we can intercept
    // a single component.
    (0, _jsxRuntime.jsx)(_innerBlocks.default.Content, {}));
    addValuesForElement(saveElement, values, innerBlocks);
  }
}
function getRichTextValues(blocks = []) {
  _blocks.__unstableGetBlockProps.skipFilters = true;
  const values = [];
  addValuesForBlocks(values, blocks);
  _blocks.__unstableGetBlockProps.skipFilters = false;
  return values.map(value => value instanceof _richText.RichTextData ? value : _richText.RichTextData.fromHTMLString(value));
}
//# sourceMappingURL=get-rich-text-values.js.map