"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 utils_exports = {};
__export(utils_exports, {
  cleanAndSetSelection: () => cleanAndSetSelection,
  getSelectedMarkRangeByTypeNames: () => getSelectedMarkRangeByTypeNames
});
module.exports = __toCommonJS(utils_exports);
var import_tiptap = require("./../../tiptap/index.js");
const getActiveType = (typeNames, schema, selection) => {
  const types = typeNames.map((typeName) => (0, import_tiptap.getMarkType)(typeName, schema));
  const type = types.find((type2) => {
    return getSelectedMarkByType(selection, type2);
  });
  return type;
};
const getSelectedMarkByType = (selection, type) => {
  const { $from } = selection;
  const mark = $from.marks().find((mark2) => mark2.type === type);
  return mark;
};
const getSelectedMarkRangeByTypeNames = (typeNames, schema, selection) => {
  const { $from, from, to } = selection;
  const type = getActiveType(typeNames, schema, selection);
  if (!type) {
    return { from, to };
  }
  const attrs = getSelectedMarkByType(selection, type)?.attrs;
  return (0, import_tiptap.getMarkRange)($from, type, attrs) || { from, to };
};
const isMarkInSelection = (mark, selection, doc) => {
  const { from, to } = selection;
  const marks = [];
  doc.nodesBetween(from, to, (node) => {
    node.marks.forEach(({ type: { name } }) => {
      marks.push(name);
    });
  });
  return marks.includes(mark);
};
const cleanAndSetSelection = (extensionName, extensionNameToRemove, unsetCommand, schema, selection, doc, commands, collapsedSelectionPlaceholder) => {
  const { from, to } = getSelectedMarkRangeByTypeNames(
    [extensionName, extensionNameToRemove],
    schema,
    selection
  );
  if (from === to) {
    commands.insertContent(collapsedSelectionPlaceholder);
    commands.setTextSelection({ from, to: from + collapsedSelectionPlaceholder.length });
  } else {
    isMarkInSelection(extensionNameToRemove, selection, doc) && unsetCommand();
    commands.setTextSelection({ from, to });
  }
};
//# sourceMappingURL=utils.js.map
