"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 span_wrapper_exports = {};
__export(span_wrapper_exports, {
  createSpanWrapperDecorationPlugin: () => createSpanWrapperDecorationPlugin
});
module.exports = __toCommonJS(span_wrapper_exports);
var import_state = require("./../../tiptap/pm/state.js");
var import_transform = require("./../../tiptap/pm/transform.js");
var import_view = require("./../../tiptap/pm/view.js");
var import_is_inline_text_only = require("../transaction-predicates/is-inline-text-only.js");
var import_only_changes_unrelated_attrs = require("../transaction-predicates/only-changes-unrelated-attrs.js");
const createSpanWrapperDecorationPlugin = (targetNodeTypes) => {
  const targetTypeNames = new Set(targetNodeTypes);
  const key = new import_state.PluginKey("span-wrapper");
  return new import_state.Plugin({
    key,
    state: {
      init: (_config, state) => buildFullSet(state.doc, targetTypeNames),
      apply: (tr, prev) => {
        if (!tr.docChanged) {
          return prev;
        }
        if ((0, import_is_inline_text_only.isInlineTextOnly)(tr)) {
          return rebuildTouchedBlocks(tr, prev, targetTypeNames);
        }
        if ((0, import_only_changes_unrelated_attrs.onlyChangesUnrelatedAttrs)(tr)) {
          return prev;
        }
        return buildFullSet(tr.doc, targetTypeNames);
      }
    },
    props: {
      decorations(state) {
        return key.getState(state);
      }
    }
  });
};
const buildSpanWrappersForBlock = (block, blockStart) => {
  const decorations = [];
  block.forEach((childNode, childOffset) => {
    if (childNode.type.name === "hardBreak") {
      return;
    }
    const childStart = blockStart + childOffset + 1;
    const childEnd = childStart + childNode.nodeSize;
    decorations.push(import_view.Decoration.inline(childStart, childEnd, { nodeName: "span" }));
  });
  return decorations;
};
const buildFullSet = (doc, targetTypeNames) => {
  const decorations = [];
  doc.descendants((node, pos) => {
    if (targetTypeNames.has(node.type.name)) {
      decorations.push(...buildSpanWrappersForBlock(node, pos));
      return false;
    }
    if (node.isTextblock) {
      return false;
    }
  });
  return import_view.DecorationSet.create(doc, decorations);
};
const rebuildTouchedBlocks = (tr, prev, targetTypeNames) => {
  let next = prev.map(tr.mapping, tr.doc);
  const rebuiltBlockStarts = /* @__PURE__ */ new Set();
  tr.steps.forEach((step, stepIndex) => {
    if (!(step instanceof import_transform.ReplaceStep)) {
      return;
    }
    const finalFrom = tr.mapping.slice(stepIndex).map(step.from);
    const $from = tr.doc.resolve(finalFrom);
    if (!targetTypeNames.has($from.parent.type.name)) {
      return;
    }
    const blockStart = $from.before($from.depth);
    if (rebuiltBlockStarts.has(blockStart)) {
      return;
    }
    rebuiltBlockStarts.add(blockStart);
    const blockEnd = blockStart + $from.parent.nodeSize;
    next = next.remove(next.find(blockStart, blockEnd));
    next = next.add(tr.doc, buildSpanWrappersForBlock($from.parent, blockStart));
  });
  return next;
};
//# sourceMappingURL=span-wrapper.js.map
