import { Decoration_Type } from "./../../../../ricos-schema/legacy/wix/rich_content/v1/index.js";
const cssToRicosDecoration = {
  color: (style) => {
    return { type: Decoration_Type.COLOR, colorData: { foreground: style } };
  },
  "background-color": (style) => {
    return { type: Decoration_Type.COLOR, colorData: { background: style } };
  },
  "font-weight": (style) => {
    return { type: Decoration_Type.BOLD, fontWeightValue: style === "bold" ? 700 : 400 };
  },
  "font-style": (style) => {
    return { type: Decoration_Type.ITALIC, italicData: style === "italic" };
  },
  "text-decoration": (style) => {
    return { type: Decoration_Type.UNDERLINE, underlineData: style === "underline" };
  },
  "font-size": (style) => {
    const [value, unit] = style.split(/(px|em)/gi);
    return {
      type: Decoration_Type.FONT_SIZE,
      fontSizeData: { unit: unit.toUpperCase(), value: parseInt(value) }
    };
  }
};
const convertHeaderToRicosDecorations = (styles) => Object.entries(styles).filter(([key, _]) => cssToRicosDecoration[key]).map(
  ([key, style]) => cssToRicosDecoration[key](style)
);
const convertCssToNodeStyle = (styles) => {
  return styles["padding-top"] || styles["padding-bottom"] ? {
    paddingTop: styles["padding-top"],
    paddingBottom: styles["padding-bottom"]
  } : void 0;
};
const normalizeColorDecorations = (decorations) => {
  const background = decorations.find((decoration) => decoration?.colorData?.background)?.colorData?.background;
  const foreground = decorations.find((decoration) => decoration?.colorData?.foreground)?.colorData?.foreground;
  if (background && foreground) {
    const colorDecoration = { type: Decoration_Type.COLOR, colorData: { background, foreground } };
    return decorations.filter((decoration) => decoration.type !== Decoration_Type.COLOR).concat([colorDecoration]);
  }
  return decorations;
};
const parseDocStyle = (documentStyle) => {
  if (documentStyle) {
    const ricosDocumentStyle = {};
    Object.entries(documentStyle).forEach(([header, styles]) => {
      if (header) {
        const decorations = normalizeColorDecorations(
          convertHeaderToRicosDecorations(styles)
        );
        const nodeStyle = convertCssToNodeStyle(styles);
        const lineHeight = styles["line-height"];
        ricosDocumentStyle[header] = { decorations: [] };
        const style = ricosDocumentStyle[header];
        if (style) {
          decorations?.length > 0 && (style.decorations = decorations);
          nodeStyle && (style.nodeStyle = nodeStyle);
          lineHeight && (style.lineHeight = lineHeight);
        }
      }
    });
    return ricosDocumentStyle;
  }
};
export {
  parseDocStyle
};
//# sourceMappingURL=parse-doc-style.js.map
