"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var trailing_node_exports = {};
__export(trailing_node_exports, {
  trailingNode: () => trailingNode
});
module.exports = __toCommonJS(trailing_node_exports);
var import_ricos_content = require("./../ricos-content/index.js");
var import_v1 = require("./../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_state = require("./../tiptap/pm/state.js");
var import_create_ricos_extension = require("./create-ricos-extension/index.js");
const trailingNode = (0, import_create_ricos_extension.createRicosFunctionalExtension)({
  type: "extension",
  groups: [],
  name: "trailingNode",
  reconfigure(config, extensions) {
    return {
      ...config,
      addOptions: () => ({
        node: import_v1.Node_Type.PARAGRAPH,
        trailingInCard: !!extensions.find((e) => e.name === import_ricos_content.TIPTAP_CARD_TYPE),
        notAfter: extensions.filter(({ groups }) => groups.includes("no-trailing-node")).map(({ name }) => name)
      })
    };
  },
  createExtensionConfig() {
    return {
      name: this.name,
      addProseMirrorPlugins() {
        const plugin = new import_state.PluginKey(this.name);
        return [
          new import_state.Plugin({
            key: plugin,
            appendTransaction: (_, __, state) => {
              const { doc, tr, schema } = state;
              if (this.options.trailingInCard) {
                const type2 = schema.nodes[this.options.node];
                let hasChanges = false;
                const insertPositions = [];
                doc.forEach((node, pos) => {
                  if (node.type.name !== import_v1.Node_Type.CARD) {
                    return;
                  }
                  node.forEach((wrapper, wrapperOffset) => {
                    if (wrapper.type.name !== import_ricos_content.TIPTAP_CARD_CONTENT_TYPE) {
                      return;
                    }
                    const lastChild = wrapper.lastChild;
                    if (this.options.notAfter.includes(lastChild?.type.name)) {
                      return;
                    }
                    const wrapperEndInner = pos + 1 + wrapperOffset + wrapper.nodeSize - 1;
                    insertPositions.push(wrapperEndInner);
                  });
                });
                for (let i = insertPositions.length - 1; i >= 0; i--) {
                  tr.insert(insertPositions[i], type2.create());
                  hasChanges = true;
                }
                if (hasChanges) {
                  return tr.setMeta("trailingNodeInsertion", true);
                }
                return;
              }
              const lastNode = state.tr.doc.lastChild;
              if (this.options.notAfter.includes(lastNode?.type.name)) {
                return;
              }
              const endPosition = doc.content.size;
              const type = schema.nodes[this.options.node];
              return tr.insert(endPosition, type.create()).setMeta("trailingNodeInsertion", true);
            }
          })
        ];
      }
    };
  }
});
//# sourceMappingURL=trailing-node.js.map
