"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 autolink_exports = {};
__export(autolink_exports, {
  autolink: () => autolink
});
module.exports = __toCommonJS(autolink_exports);
var import_linkifyjs = require("linkifyjs");
var import_tiptap = require("./../tiptap/index.js");
var import_core = require("./../tiptap/core/index.js");
var import_state = require("./../tiptap/pm/state.js");
function autolink(options) {
  return new import_state.Plugin({
    key: new import_state.PluginKey("autolink"),
    appendTransaction: (transactions, oldState, newState) => {
      const docChanges = transactions.some((transaction) => transaction.docChanged) && !oldState.doc.eq(newState.doc);
      const preventAutolink = transactions.some(
        (transaction) => transaction.getMeta("preventAutolink")
      );
      if (!docChanges || preventAutolink) {
        return;
      }
      const { tr } = newState;
      const transform = (0, import_core.combineTransactionSteps)(oldState.doc, [...transactions]);
      const { mapping } = transform;
      const changes = (0, import_core.getChangedRanges)(transform);
      let isLink = false;
      changes.forEach(({ oldRange, newRange }) => {
        (0, import_tiptap.getMarksBetween)(oldRange.from, oldRange.to, oldState.doc).filter((item) => item.mark.type === options.type).forEach((oldMark) => {
          const newFrom = mapping.map(oldMark.from);
          const newTo = mapping.map(oldMark.to);
          const newMarks = (0, import_tiptap.getMarksBetween)(newFrom, newTo, newState.doc).filter(
            (item) => item.mark.type === options.type
          );
          if (!newMarks.length) {
            return;
          }
          const newMark = newMarks[0];
          const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, void 0, " ");
          const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, void 0, " ");
          const wasLink = (0, import_linkifyjs.test)(oldLinkText);
          isLink = (0, import_linkifyjs.test)(newLinkText);
          if (wasLink && !isLink) {
            tr.removeMark(newMark.from, newMark.to, options.type);
          }
        });
        if (isLink) {
          return;
        }
        (0, import_tiptap.findChildrenInRange)(newState.doc, newRange, (node) => node.isTextblock).forEach((textBlock) => {
          const text = newState.doc.textBetween(
            textBlock.pos,
            textBlock.pos + textBlock.node.nodeSize,
            void 0,
            " "
          );
          (0, import_linkifyjs.find)(text).filter((link) => link.isLink).map((link) => ({
            ...link,
            from: textBlock.pos + link.start + 1,
            to: textBlock.pos + link.end + 1
          })).filter((link) => {
            const fromIsInRange = newRange.from >= link.from && newRange.from <= link.to;
            const toIsInRange = newRange.to >= link.from && newRange.to <= link.to;
            return fromIsInRange || toIsInRange;
          }).forEach((link) => {
            tr.addMark(
              link.from,
              link.to,
              options.type.create({
                link: options.defaultLink.setUrl(link.href).toLink()
              })
            );
            tr.setMeta("autoLink", true);
          });
        });
      });
      if (!tr.steps.length) {
        return;
      }
      return tr;
    }
  });
}
//# sourceMappingURL=autolink.js.map
