import { parseDocStyle } from "./../ricos-content/lib/convert-doc-style-to-draft.js";
import { Decoration_Type, FontSizeData_fontType, Node_Type } from "./../ricos-schema/legacy/wix/rich_content/v1/index.js";
import {
  PmNodeType
} from "./../_shared/pm-node-types.js";
import { RicosDocumentStyle } from "./document-style/document-style.js";
import { getDocumentStyleKey } from "./text-node-transformer.js";
import { RicosTextualTheme as TextualTheme } from "./textual-theme/textual-theme.js";
const TOPICS = [
  "ricos.styles.functionality.themeUpdate",
  "ricos.styles.functionality.documentStyleUpdate"
];
class RicosStyles {
  constructor() {
    this.theme = new TextualTheme({});
    this.documentStyle = new RicosDocumentStyle({});
    this.id = "ricos-styles";
    this.topicsToPublish = TOPICS;
    this.editorElement = null;
    this.topicsToSubscribe = TOPICS;
    this.setEditorElement = (e) => {
      this.editorElement = e;
    };
    this.onThemeUpdate = (callback) => this.subscriptors?.byTopic("ricos.styles.functionality.themeUpdate").subscribe((_, data) => callback(data));
    this.onDocumentStyleUpdate = (callback) => this.subscriptors?.byTopic("ricos.styles.functionality.documentStyleUpdate").subscribe((_, data) => callback(data));
  }
  toStyleTags() {
    return [this.theme.toStyleTag(), this.documentStyle.toStyleTag()];
  }
  getDecoration(node, decorationType, nodeDom) {
    if (!node || ![
      Node_Type.PARAGRAPH,
      Node_Type.HEADING,
      Node_Type.BLOCKQUOTE,
      Node_Type.CODE_BLOCK,
      Node_Type.CAPTION
    ].includes(node?.type)) {
      return {};
    }
    const element = nodeDom || this.editorElement;
    if (element && decorationType === Decoration_Type.FONT_SIZE) {
      const styles = window.getComputedStyle(element);
      const fontSizeVar = styles.getPropertyValue(
        `--ricos-custom-${propertyNameKeyFromNodeType(node)}-font-size`
      );
      const parsedFontSizeVar = parseInt(fontSizeVar, 10);
      const computedFontSize = styles.getPropertyValue("font-size");
      const fontSizeValue = !isNaN(parsedFontSizeVar) ? parsedFontSizeVar : parseInt(computedFontSize, 10);
      return {
        type: Decoration_Type.FONT_SIZE,
        fontSizeData: {
          unit: FontSizeData_fontType.PX,
          value: fontSizeValue
        }
      };
    }
    if (element && decorationType === Decoration_Type.FONT_FAMILY) {
      const styles = window.getComputedStyle(element);
      const fontFamily = styles.getPropertyValue(
        `--ricos-custom-${propertyNameKeyFromNodeType(node)}-font-family`
      );
      const fallbackFontFamily = styles.getPropertyValue("font-family").split(",")[0];
      return {
        type: Decoration_Type.FONT_FAMILY,
        fontFamilyData: {
          value: fontFamily || fallbackFontFamily
        }
      };
    }
    const nodeType = getDocumentStyleKey(node);
    const documentStyleDecoration = this.documentStyle.getDecoration(nodeType, decorationType);
    const themeDecoration = this.theme.getDecoration(nodeType, decorationType);
    return themeDecoration.overrideWith(documentStyleDecoration).getDecoration();
  }
  getComputedLineHeight(node, nodeDom) {
    if (![
      PmNodeType.PARAGRAPH,
      PmNodeType.HEADING,
      PmNodeType.BLOCKQUOTE,
      PmNodeType.CODE_BLOCK,
      PmNodeType.CAPTION
    ].includes(node.type)) {
      return void 0;
    }
    const element = nodeDom || this.editorElement;
    if (element) {
      const styles = window.getComputedStyle(element);
      const { lineHeight, fontSize } = styles;
      return (parseFloat(lineHeight) / parseFloat(fontSize)).toFixed(1);
    }
  }
  getTextStyle(node) {
    const nodeType = getDocumentStyleKey(node);
    const documentStyleTextStyle = this.documentStyle.getTextStyle(nodeType);
    const themeTextStyle = this.theme.getTextStyle(nodeType);
    return themeTextStyle.overrideWith(documentStyleTextStyle.getTextStyle()).getTextStyle();
  }
  getNodeStyle(node) {
    const nodeType = getDocumentStyleKey(node);
    const documentStyleNodeStyle = this.documentStyle.getNodeStyle(nodeType);
    const themeTextStyle = this.theme.getNodeStyle(nodeType);
    return themeTextStyle.overrideWith(documentStyleNodeStyle.getNodeStyle()).getNodeStyle();
  }
  getTheme() {
    return this.theme;
  }
  setTheme(theme) {
    this.theme = new TextualTheme(theme);
    this.publishers?.byTopic("ricos.styles.functionality.themeUpdate").publish(theme);
    return this;
  }
  getDocumentStyle() {
    return this.documentStyle;
  }
  setDocumentStyle(documentStyle) {
    this.documentStyle = new RicosDocumentStyle(documentStyle);
    this.publishers?.byTopic("ricos.styles.functionality.documentStyleUpdate").publish(documentStyle);
    return this;
  }
  toDraftDocumentStyle() {
    return parseDocStyle(this.documentStyle.toContent());
  }
}
const propertyNameKeyFromNodeType = (node) => {
  switch (node.type) {
    case Node_Type.HEADING:
      return `h${node.headingData.level}`;
    case Node_Type.BLOCKQUOTE:
      return "quote";
    case Node_Type.CODE_BLOCK:
      return "code-block";
    default:
      return "p";
  }
};
export {
  RicosStyles
};
//# sourceMappingURL=styles.js.map
