import { getMarkRange, getMarkType } from "./../../tiptap/index.js";
const getActiveType = (typeNames, schema, selection) => {
  const types = typeNames.map((typeName) => 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 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 });
  }
};
export {
  cleanAndSetSelection,
  getSelectedMarkRangeByTypeNames
};
//# sourceMappingURL=utils.js.map
