/**
 * Ported from Gutenberg code
 * @see README.md
 */

/**
 * Internal dependencies
 */
import * as node from './node';

/**
 * A representation of a block's rich text value.
 *
 * @typedef {WPBlockNode[]} WPBlockChildren
 */

/**
 * Given an iterable set of DOM nodes, returns equivalent block children.
 * Ignores any non-element/text nodes included in set.
 * @param {Iterable.<Node>} domNodes - Iterable set of DOM nodes to convert.
 * @returns {WPBlockChildren} Block children equivalent to DOM nodes.
 */
export function fromDOM(domNodes) {
  const result = [];
  for (let i = 0; i < domNodes.length; i++) {
    try {
      result.push(node.fromDOM(domNodes[ i ]));
    } catch (error) {
      // Simply ignore if DOM node could not be converted.
    }
  }

  return result;
}

/**
 * Object of utility functions used in managing block attribute values of
 * source `children`.
 *
 * @see https://github.com/WordPress/gutenberg/pull/10439
 *
 * @deprecated since 4.0. The `children` source should not be used, and can be
 *             replaced by the `html` source.
 *
 * @private
 */
export default { fromDOM };
