"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "findCodeBlock", {
  enumerable: true,
  get: function get() {
    return _findCodeBlock.findCodeBlock;
  }
});
Object.defineProperty(exports, "transformSingleLineCodeBlockToCodeMark", {
  enumerable: true,
  get: function get() {
    return _transformSingleLineCodeBlockToCodeMark.transformSingleLineCodeBlockToCodeMark;
  }
});
exports.transformSliceToJoinAdjacentCodeBlocks = transformSliceToJoinAdjacentCodeBlocks;
var _model = require("@atlaskit/editor-prosemirror/model");
var _slice = require("../utils/slice");
var _transformSingleLineCodeBlockToCodeMark = require("./transformSingleLineCodeBlockToCodeMark");
var _findCodeBlock = require("./findCodeBlock");
function joinCodeBlocks(left, right) {
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  var textContext = "".concat(left.textContent, "\n").concat(right.textContent);
  return left.type.create(left.attrs, left.type.schema.text(textContext));
}
function mergeAdjacentCodeBlocks(fragment) {
  var children = [];
  fragment.forEach(function (maybeCodeBlock) {
    if (maybeCodeBlock.type === maybeCodeBlock.type.schema.nodes.codeBlock) {
      var peekAtPrevious = children[children.length - 1];
      if (peekAtPrevious && peekAtPrevious.type === maybeCodeBlock.type) {
        // Ignored via go/ees005
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return children.push(joinCodeBlocks(children.pop(), maybeCodeBlock));
      }
    }
    return children.push(maybeCodeBlock);
  });
  return _model.Fragment.from(children);
}
function transformSliceToJoinAdjacentCodeBlocks(slice) {
  slice = (0, _slice.mapSlice)(slice, function (node) {
    return node.isBlock && !node.isTextblock ? node.copy(mergeAdjacentCodeBlocks(node.content)) : node;
  });
  // mapSlice won't be able to merge adjacent top-level code-blocks
  return new _model.Slice(mergeAdjacentCodeBlocks(slice.content), slice.openStart, slice.openEnd);
}
// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export