"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, {
  contentDiff: () => contentDiff
});
module.exports = __toCommonJS(extension_exports);
var import_state = require("./../../tiptap/pm/state.js");
var import_create_ricos_extension = require("../create-ricos-extension/index.js");
var import_diff_tools = require("./diff-tools.js");
const reportDiff = (log, onPluginAdded, onPluginDeleted, onLinkAdded) => (transaction, state) => {
  setTimeout(() => {
    if (!transaction.docChanged || (0, import_diff_tools.isIgnoredTransaction)(transaction)) {
      log("changes are filtered (docs are equal)");
      return true;
    }
    const { newRange, oldRange } = (0, import_diff_tools.getDiffRanges)(state.doc.content, transaction.doc.content);
    if (newRange.isEmpty() && oldRange.isEmpty()) {
      log("changes are filtered (empty ranges)");
      return true;
    }
    log("DIFF RANGES", { oldRange, newRange });
    let contentChanges;
    try {
      contentChanges = (0, import_diff_tools.getContentChange)(state.doc, transaction.doc, {
        oldRange,
        newRange
      });
    } catch (e) {
      log("CONTENT CHANGE ERROR", e);
      return true;
    }
    log("CONTENT CHANGE", contentChanges.toJSON());
    const diffs = contentChanges.getDiff();
    log("CONTENT DIFF", diffs);
    (0, import_diff_tools.reportGenericDiff)(diffs, onPluginAdded, onPluginDeleted);
    (0, import_diff_tools.reportLinkDiff)(diffs, onLinkAdded, onPluginDeleted);
    (0, import_diff_tools.reportTextStyleDiff)(diffs, onPluginAdded, onPluginDeleted);
  }, 0);
  return true;
};
const contentDiff = (0, import_create_ricos_extension.createRicosFunctionalExtension)({
  name: "contentDiff",
  type: "extension",
  groups: [],
  reconfigure(config, _extensions, _props, _settings, services) {
    return {
      ...config,
      addOptions() {
        return {
          logger: services.context.debugMode?.includes("all") || services.context.debugMode?.includes("extensions") ? (message, ...args) => console.debug(message, ...args) : () => {
          },
          publishPluginAddSuccess(pluginId, params) {
            return services.pluginsEvents.publishPluginAddSuccess({ pluginId, params });
          },
          publishPluginDelete(pluginId, _params) {
            return services.pluginsEvents.publishPluginDelete({ pluginId });
          },
          publishPluginLinkable(data) {
            return services.pluginsEvents.publishPluginLinkable(data);
          }
        };
      }
    };
  },
  createExtensionConfig() {
    return {
      name: "contentDiff",
      priority: 1,
      addProseMirrorPlugins() {
        return [
          new import_state.Plugin({
            key: new import_state.PluginKey("contentDiff"),
            filterTransaction: reportDiff(
              this.options.logger,
              this.options.publishPluginAddSuccess,
              this.options.publishPluginDelete,
              this.options.publishPluginLinkable
            )
          })
        ];
      }
    };
  }
});
//# sourceMappingURL=extension.js.map
