"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.transformSliceEnsureListItemParagraphFirst = void 0;
var _model = require("@atlaskit/editor-prosemirror/model");
var _slice = require("../utils/slice");
/**
 * Ensures every `listItem` in a slice starts with a paragraph.
 *
 * @param slice - The slice to transform
 * @param schema - The editor schema, used to create new paragraph nodes
 * @returns A new slice with the transformation applied
 */
var transformSliceEnsureListItemParagraphFirst = exports.transformSliceEnsureListItemParagraphFirst = function transformSliceEnsureListItemParagraphFirst(slice, schema) {
  var _schema$nodes = schema.nodes,
    listItem = _schema$nodes.listItem,
    paragraph = _schema$nodes.paragraph;
  if (!listItem || !paragraph) {
    return slice;
  }
  return (0, _slice.mapSlice)(slice, function (node) {
    if (node.type === listItem) {
      var firstChild = node.firstChild;
      if (firstChild && firstChild.type !== paragraph) {
        var emptyParagraph = paragraph.createAndFill();
        if (emptyParagraph) {
          var children = [emptyParagraph];
          for (var i = 0; i < node.childCount; i++) {
            children.push(node.child(i));
          }
          return node.copy(_model.Fragment.from(children));
        }
      }
    }
    return node;
  });
};