"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 extension_exports = {};
__export(extension_exports, {
  notOvertypable: () => notOvertypable
});
module.exports = __toCommonJS(extension_exports);
var import_tiptap = require("./../../tiptap/index.js");
var import_state = require("./../../tiptap/pm/state.js");
var import_transform = require("./../../tiptap/pm/transform.js");
var import_plugin_commons = require("./../../plugin-commons/index.js");
const notOvertypable = (0, import_plugin_commons.createRicosFunctionalExtension)({
  type: "extension",
  groups: [],
  name: "ricos-not-overtypable",
  reconfigure(config, extensions, _ricosProps) {
    const types = extensions.filter((extension) => extension.groups.includes("not-overtypable")).map(({ name }) => name);
    const getIsTypingDisabled = (selection) => (0, import_tiptap.isNodeSelection)(selection) && types.includes(selection.node.type.name);
    return {
      ...config,
      priority: 70,
      addProseMirrorPlugins() {
        return [
          new import_state.Plugin({
            key: new import_state.PluginKey("ricos-not-overtypable"),
            /**
             * This place sets the disableTyping state based on the current selection.
             * init is called on initialization and apply is called on every transaction.
             */
            state: {
              init(_, { selection }) {
                return {
                  disableTyping: getIsTypingDisabled(selection)
                };
              },
              apply(_tr, _value, _oldState, { selection }) {
                return {
                  disableTyping: getIsTypingDisabled(selection)
                };
              }
            },
            /**
             * This place filters transactions that are not allowed to change the document.
             * If doc is not changed or disableTyping is false, the transaction is allowed.
             * If doc is changed and disableTyping is true, the transaction is allowed only if it
             * DOESNT replace a node with text.
             *
             * This is done to prevent overtyping nodes that are not supposed to be overtyped. (e.g. images, polls, etc.)
             * It means that when you have a node selected that has this plugin,
             * and you start typing, the text wont be replaced instead of the node.
             */
            filterTransaction(tr, state) {
              const { disableTyping } = this.getState(state);
              const allowTextReplace = tr.getMeta("allowTextReplace");
              if (!tr.docChanged || !disableTyping || allowTextReplace) {
                return true;
              }
              const isReplacingWithText = tr.steps.some((step) => {
                let isText = false;
                if (step instanceof import_transform.ReplaceStep) {
                  step.slice.content.descendants((node) => {
                    if (node.isText) {
                      isText = true;
                      return false;
                    }
                    return !isText;
                  });
                }
                return isText;
              });
              return !isReplacingWithText;
            }
          })
        ];
      }
    };
  },
  createExtensionConfig() {
    return {
      name: this.name
    };
  }
});
//# sourceMappingURL=extension.js.map
