"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 unique_id_exports = {};
__export(unique_id_exports, {
  uniqueId: () => uniqueId
});
module.exports = __toCommonJS(unique_id_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_editor_common = require("./../editor-common/index.js");
var import_create_ricos_extension = require("./create-ricos-extension/index.js");
var import_text_style_converters = require("./text-style-converters.js");
function minMax(value = 0, min = 0, max = 0) {
  return Math.min(Math.max(value, min), max);
}
const handleAndroidEnterBug = (tr, pos) => {
  const { doc } = tr;
  const { from, to } = { from: pos + 1, to: pos + 1 };
  const minPos = import_state.TextSelection.atStart(doc).from;
  const maxPos = import_state.TextSelection.atEnd(doc).to;
  const resolvedFrom = minMax(from, minPos, maxPos);
  const resolvedEnd = minMax(to, minPos, maxPos);
  const selection = import_state.TextSelection.create(doc, resolvedFrom, resolvedEnd);
  tr.setSelection(selection);
};
const handleBlockquoteParagraphId = (node, usedIds) => {
  let paragraphId;
  let nodeParagraphId;
  let shouldUpdateBlockquoteParagraphId = false;
  if (node.type.name === import_v1.Node_Type.BLOCKQUOTE) {
    nodeParagraphId = node.attrs.paragraphId;
    paragraphId = nodeParagraphId && !usedIds[nodeParagraphId] ? nodeParagraphId : (0, import_ricos_content.generateId)();
    shouldUpdateBlockquoteParagraphId = nodeParagraphId !== paragraphId;
  }
  return { paragraphId, shouldUpdateBlockquoteParagraphId };
};
const uniqueId = (0, import_create_ricos_extension.createRicosFunctionalExtension)({
  type: "extension",
  groups: [],
  name: "unique-id",
  reconfigure(config, extensions) {
    return {
      ...config,
      addGlobalAttributes() {
        return [
          {
            types: extensions.filter(
              (extension) => (extension.type === "react-node" || extension.type === "html-node") && !extension.groups.includes("text")
            ).map(({ name }) => name),
            attributes: {
              id: {
                default: null
              }
            }
          }
        ];
      }
    };
  },
  createExtensionConfig() {
    return {
      name: this.name,
      priority: 2,
      // should be before contentDiff
      addProseMirrorPlugins() {
        return [
          new import_state.Plugin({
            key: new import_state.PluginKey("unique-id"),
            appendTransaction: (_transactions, oldState, newState) => {
              if (newState.doc === oldState.doc) {
                return;
              }
              const tr = newState.tr;
              const usedIds = {};
              const { selection } = oldState;
              const marks = selection.$to.parentOffset && selection.$from.marks();
              newState.doc.descendants((node, pos) => {
                const nodeId = node.attrs.id;
                const id = nodeId && !usedIds[nodeId] ? nodeId : (0, import_ricos_content.generateId)();
                usedIds[id] = true;
                const shouldUpdate = nodeId !== id;
                const { paragraphId, shouldUpdateBlockquoteParagraphId } = handleBlockquoteParagraphId(node, usedIds);
                if (paragraphId) {
                  usedIds[paragraphId] = true;
                }
                if (node.isBlock && (shouldUpdate || shouldUpdateBlockquoteParagraphId)) {
                  if (import_editor_common.browser.android && node.attrs.style) {
                    const { style, textStyle } = (0, import_text_style_converters.nodeElementStyleToTiptapTextStyle)(
                      node.attrs.style
                    );
                    tr.setNodeMarkup(pos, void 0, {
                      ...node.attrs,
                      id,
                      marks,
                      style,
                      textStyle,
                      ...paragraphId ? { paragraphId } : {}
                    });
                  } else {
                    tr.setNodeMarkup(pos, void 0, {
                      ...node.attrs,
                      id,
                      marks,
                      ...paragraphId ? { paragraphId } : {}
                    });
                  }
                  tr.setMeta("unique-id", true);
                  if (import_editor_common.browser.android && !tr.getMeta("trailingNodeInsertion")) {
                    handleAndroidEnterBug(tr, pos);
                  }
                  marks && tr.ensureMarks(marks);
                }
              });
              return tr;
            }
          })
        ];
      }
    };
  }
});
//# sourceMappingURL=unique-id.js.map
