"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 character_count_exports = {};
__export(character_count_exports, {
  characterCount: () => characterCount
});
module.exports = __toCommonJS(character_count_exports);
var import_state = require("./../tiptap/pm/state.js");
var import_create_ricos_functional_extension = require("./create-ricos-extension/create-ricos-functional-extension.js");
const MAX_TEXT_LENGTH = 15e5;
const characterCount = (0, import_create_ricos_functional_extension.createRicosFunctionalExtension)({
  type: "extension",
  groups: [],
  name: "characterCount",
  reconfigure: (config, _extensions, ricosProps) => ({
    ...config,
    addOptions() {
      return {
        maxTextLength: Math.min(ricosProps.maxTextLength || MAX_TEXT_LENGTH, MAX_TEXT_LENGTH),
        mode: "textSize"
      };
    }
  }),
  createExtensionConfig() {
    return {
      name: this.name,
      addStorage() {
        return {
          characters: () => 0,
          words: () => 0
        };
      },
      onBeforeCreate() {
        this.storage.characters = (options) => {
          const node = options?.node || this.editor.state.doc;
          const mode = options?.mode || this.options.mode;
          if (mode === "textSize") {
            const text = node.textBetween(0, node.content.size, void 0, " ");
            return text.length;
          }
          return node.nodeSize;
        };
        this.storage.words = (options) => {
          const node = options?.node || this.editor.state.doc;
          const text = node.textBetween(0, node.content.size, " ", " ");
          const words = text.split(" ").filter((word) => word !== "");
          return words.length;
        };
      },
      addProseMirrorPlugins() {
        return [
          new import_state.Plugin({
            key: new import_state.PluginKey("characterCount"),
            filterTransaction: (transaction, state) => {
              const limit = this.options.maxTextLength;
              if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) {
                return true;
              }
              const oldSize = this.storage.characters({ node: state.doc, mode: this.options.mode });
              const newSize = this.storage.characters({
                node: transaction.doc,
                mode: this.options.mode
              });
              if (newSize <= limit) {
                return true;
              }
              if (oldSize > limit && newSize > limit && newSize <= oldSize) {
                return true;
              }
              if (oldSize > limit && newSize > limit && newSize > oldSize) {
                return false;
              }
              const isPaste = transaction.getMeta("paste");
              if (!isPaste) {
                return false;
              }
              const pos = transaction.selection.$head.pos;
              const over = newSize - limit;
              const from = pos - over;
              const to = pos;
              transaction.deleteRange(from, to);
              const updatedSize = this.storage.characters({
                node: transaction.doc,
                mode: this.options.mode
              });
              if (updatedSize > limit) {
                return false;
              }
              return true;
            }
          })
        ];
      }
    };
  }
});
//# sourceMappingURL=character-count.js.map
