import { nodeToJSON } from '@atlaskit/editor-json-transformer';
import { getSliceFromSelection } from './context-helpers';

/**
 * Get the fragments from the selection.
 * @param selection The selection to get the fragments from.
 * @param schema The schema to use to convert the nodes to JSON.
 * @returns The fragments as an array of JSON nodes.
 */
export var getFragmentsFromSelection = function getFragmentsFromSelection(selection) {
  if (!selection || selection.empty) {
    return null;
  }
  var slice = getSliceFromSelection(selection);
  var content = slice.content;
  var fragment = [];
  content.forEach(function (node) {
    fragment.push(nodeToJSON(node));
  });
  return fragment;
};