import { Decoration_Type, Node_Type } from "./../../ricos-types/index.js";
import { getDomAtPosOrParent } from "./get-dom-at-pos-or-parent.js";
const MANY_FONT_SIZES = "";
const UNKNOWN_FONT_SIZE = "";
const getFontSizeInSelection = (_content, { styles, converters }, editor) => {
  const getDefaultParagraphValue = (nodeDom) => {
    const decoration = styles.getDecoration(
      {
        paragraphData: {},
        type: Node_Type.PARAGRAPH
      },
      Decoration_Type.FONT_SIZE,
      nodeDom
    );
    return decoration.fontSizeData?.value?.toString() ?? UNKNOWN_FONT_SIZE;
  };
  if (!editor) {
    return getDefaultParagraphValue();
  }
  const { state } = editor;
  const { from, $from, to, empty } = state.selection;
  if (empty) {
    const storedFontSize = state.storedMarks?.find((m) => m.type.name === Decoration_Type.FONT_SIZE);
    if (storedFontSize?.attrs?.value) {
      return storedFontSize.attrs.value.toString();
    }
    const markUnderCursor = $from.marks().find((m) => m.type.name === Decoration_Type.FONT_SIZE);
    if (markUnderCursor?.attrs?.value) {
      return markUnderCursor.attrs.value.toString();
    }
    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_SIZE,
      parentDomNode
    );
    if (decoration.fontSizeData?.value) {
      const fontSizeInDocumentStyle = decoration.fontSizeData?.value;
      return fontSizeInDocumentStyle.toString();
    }
    return getDefaultParagraphValue(parentDomNode);
  }
  const selectedFontSizes = [];
  state.doc.nodesBetween(from, to, (node, pos) => {
    if (node.type.name === "text") {
      const fontSizeMark = node.marks.find((mark) => mark.type.name === Decoration_Type.FONT_SIZE);
      if (fontSizeMark) {
        selectedFontSizes.push(`${fontSizeMark?.attrs.value}`);
      } else {
        const parent = state.doc.resolve(pos).parent;
        const ricosNode = converters.fromPmNode({
          ...parent.toJSON(),
          type: parent?.type?.name
        });
        const parentDomNode = getDomAtPosOrParent(state.doc, editor.view, pos);
        const decoration = styles.getDecoration(
          ricosNode,
          Decoration_Type.FONT_SIZE,
          parentDomNode
        );
        if (decoration.fontSizeData?.value) {
          const fontSizeInDocumentStyle = decoration.fontSizeData?.value;
          selectedFontSizes.push(`${fontSizeInDocumentStyle}`);
        }
      }
    }
  });
  if (selectedFontSizes.length === 0) {
    const nodeDom = editor.view.nodeDOM(from);
    const el = nodeDom instanceof HTMLElement ? nodeDom : nodeDom?.parentElement;
    return getDefaultParagraphValue(el);
  }
  return selectedFontSizes.every((fontSize) => fontSize === selectedFontSizes[0]) ? selectedFontSizes[0] : MANY_FONT_SIZES;
};
export {
  getFontSizeInSelection
};
//# sourceMappingURL=get-font-size-in-selection.js.map
