import { Decoration_Type, Node_Type } from "./../../ricos-types/index.js";
import { getDomAtPosOrParent } from "./get-dom-at-pos-or-parent.js";
const MANY_FONT_FAMILIES = "";
const UNKNOWN_FONT_FAMILY = "";
const getFontFamilyInSelection = (_content, { styles, converters }, editor) => {
  const getDefaultParagraphValue = (nodeDom) => {
    const decoration = styles.getDecoration(
      {
        paragraphData: {},
        type: Node_Type.PARAGRAPH
      },
      Decoration_Type.FONT_FAMILY,
      nodeDom
    );
    return decoration.fontFamilyData?.value ?? UNKNOWN_FONT_FAMILY;
  };
  if (!editor) {
    return getDefaultParagraphValue();
  }
  const { state } = editor;
  const { from, $from, to, empty } = state.selection;
  if (empty) {
    const storedFontFamily = state.storedMarks?.find(
      (m) => m.type.name === Decoration_Type.FONT_FAMILY
    );
    if (storedFontFamily?.attrs?.value) {
      return storedFontFamily.attrs.value;
    }
    const markUnderCursor = $from.marks().find((m) => m.type.name === Decoration_Type.FONT_FAMILY);
    if (markUnderCursor?.attrs?.value) {
      return markUnderCursor.attrs.value;
    }
    const parent = state.doc.resolve(from).parent;
    const parentDomNode = getDomAtPosOrParent(state.doc, editor.view, from);
    const ricosNode = converters.fromPmNode({
      ...parent.toJSON(),
      type: parent?.type?.name
    });
    const decoration = styles.getDecoration(
      ricosNode,
      Decoration_Type.FONT_FAMILY,
      parentDomNode
    );
    if (decoration.fontFamilyData?.value) {
      const fontFamilyInDocumentStyle = decoration.fontFamilyData?.value;
      return fontFamilyInDocumentStyle;
    }
    return getDefaultParagraphValue(parentDomNode);
  }
  const selectedFontFamilies = [];
  state.doc.nodesBetween(from, to, (node, pos) => {
    if (node.type.name === "text") {
      const fontFamilyMark = node.marks.find(
        (mark) => mark.type.name === Decoration_Type.FONT_FAMILY
      );
      if (fontFamilyMark) {
        selectedFontFamilies.push(fontFamilyMark?.attrs.value);
      } else {
        const parent = state.doc.resolve(pos).parent;
        const ricosNode = converters.fromPmNode({
          ...parent.toJSON(),
          type: parent?.type?.name
        });
        const decoration = styles.getDecoration(
          ricosNode,
          Decoration_Type.FONT_FAMILY
        );
        if (decoration.fontFamilyData?.value) {
          const fontFamilyInDocumentStyle = decoration.fontFamilyData?.value;
          selectedFontFamilies.push(fontFamilyInDocumentStyle);
        }
      }
    }
  });
  if (selectedFontFamilies.length === 0) {
    const nodeDom = editor.view.nodeDOM(from);
    const el = nodeDom instanceof HTMLElement ? nodeDom : nodeDom?.parentElement;
    return getDefaultParagraphValue(el);
  }
  return selectedFontFamilies.every((fontFamily) => fontFamily === selectedFontFamilies[0]) ? selectedFontFamilies[0] : MANY_FONT_FAMILIES;
};
export {
  getFontFamilyInSelection
};
//# sourceMappingURL=get-font-family-in-selection.js.map
