import * as A from "fp-ts-esm/Array";
import { flow, pipe } from "fp-ts-esm/function";
import * as R from "fp-ts-esm/Record";
import * as T from "fp-ts-esm/Tuple";
import { fromEntries } from "./../ricos-content/lib/utils.js";
import { Decorations } from "./decorations/index.js";
import { RicosNodeStyle } from "./document-style/node-style.js";
import { RicosTextStyle } from "./document-style/text-style.js";
import { CustomStyles } from "./textual-theme/custom-styles.js";
const documentToThemeKeyMap = {
  headerOne: "h1",
  headerTwo: "h2",
  headerThree: "h3",
  headerFour: "h4",
  headerFive: "h5",
  headerSix: "h6",
  paragraph: "p",
  blockquote: "quote",
  codeBlock: "codeBlock"
};
const themeToDocumentKeyMap = pipe(
  documentToThemeKeyMap,
  Object.entries,
  A.map(T.swap),
  fromEntries
);
const toCustomStyle = (node) => ({
  ...Decorations.of(node?.decorations).toCustomStyle(),
  ...RicosNodeStyle.of(node?.nodeStyle).toCustomStyle(),
  ...RicosTextStyle.of({ lineHeight: node?.lineHeight }).toCustomStyle()
});
const toCustomStyleKey = (nodeType) => documentToThemeKeyMap[nodeType];
const toTuples = (documentStyle) => R.toArray(documentStyle);
const fromTuples = (tuple) => fromEntries(tuple);
const toRicosCustomStyles = flow(
  toTuples,
  A.map(T.bimap(toCustomStyle, toCustomStyleKey)),
  fromTuples
);
const toTextNodeStyle = (customStyle) => ({
  decorations: Decorations.fromCustomStyle(customStyle).toDecorationArray(),
  nodeStyle: RicosNodeStyle.fromCustomStyle(customStyle).getNodeStyle(),
  lineHeight: RicosTextStyle.fromCustomStyle(customStyle).getTextStyle().lineHeight
});
const toTextNodeType = (customStyleKey) => themeToDocumentKeyMap[customStyleKey];
const toDocumentStyle = flow(
  // @ts-expect-error $TSFixMe
  Object.entries,
  A.map(T.bimap(toTextNodeStyle, toTextNodeType)),
  fromEntries,
  JSON.stringify,
  JSON.parse
);
class TextStyleTransformer {
  constructor(theme) {
    this.theme = theme;
  }
  static fromTheme(theme) {
    return new TextStyleTransformer(theme || {});
  }
  static fromDocumentStyle(documentStyle) {
    const customStyles = toRicosCustomStyles(documentStyle);
    return new TextStyleTransformer({ customStyles });
  }
  toTheme() {
    return this.theme;
  }
  toDocumentStyle() {
    return toDocumentStyle(CustomStyles.fromTheme(this.theme).toCustomStyles());
  }
}
export {
  TextStyleTransformer
};
//# sourceMappingURL=text-style-transformer.js.map
