"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var decorations_exports = {};
__export(decorations_exports, {
  Decorations: () => Decorations
});
module.exports = __toCommonJS(decorations_exports);
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_bold_decoration = require("./bold-decoration.js");
var import_color_decoration = require("./color-decoration.js");
var import_empty_decoration = require("./empty-decoration.js");
var import_font_size_decoration = require("./font-size-decoration.js");
var import_italic_decoration = require("./italic-decoration.js");
var import_sketch_decoration = require("./sketch-decoration.js");
var import_utils = require("./utils.js");
const _Decorations = class _Decorations {
  constructor(textDecorations) {
    this.decorations = textDecorations;
  }
  static of(decorations) {
    const textDecorations = (decorations || []).map(_Decorations.toTextDecoration);
    return new _Decorations(textDecorations);
  }
  static fromCustomStyle(customStyle) {
    const { color, backgroundColor, ...rest } = customStyle;
    const colorStyle = {
      color: color ?? "",
      backgroundColor: backgroundColor ?? ""
    };
    const styles = Object.entries(rest).map(([k, v]) => ({ [k]: v })).concat([colorStyle]);
    const textDecorations = styles.map(_Decorations.styleToDecoration);
    return new _Decorations(textDecorations);
  }
  static styleToDecoration(style) {
    return (0, import_utils.firstRight)(style, _Decorations.empty, [
      [(s) => !!s.fontWeight, (s) => import_bold_decoration.BoldDecoration.fromCustomStyle(s)],
      [(s) => !!s.fontSize, (s) => import_font_size_decoration.FontSizeDecoration.fromCustomStyle(s)],
      [(s) => !!s.fontStyle, (s) => import_italic_decoration.ItalicDecoration.fromCustomStyle(s)],
      [(s) => !!s.color || !!s.backgroundColor, (s) => import_color_decoration.ColorDecoration.fromCustomStyle(s)]
    ]);
  }
  static toTextDecoration(decoration) {
    return (0, import_utils.firstRight)(decoration, _Decorations.empty, [
      [(d) => d.type === import_v1.Decoration_Type.BOLD, (d) => import_bold_decoration.BoldDecoration.of(d)],
      [(d) => d.type === import_v1.Decoration_Type.ITALIC, (d) => import_italic_decoration.ItalicDecoration.of(d)],
      [(d) => d.type === import_v1.Decoration_Type.FONT_SIZE, (d) => import_font_size_decoration.FontSizeDecoration.of(d)],
      [(d) => d.type === import_v1.Decoration_Type.COLOR, (d) => import_color_decoration.ColorDecoration.of(d)],
      [(d) => d.type === import_v1.Decoration_Type.SKETCH, (d) => import_sketch_decoration.SketchDecoration.of(d)]
    ]);
  }
  toDecorationArray() {
    return this.decorations.filter((d) => d.type !== import_v1.Decoration_Type.UNRECOGNIZED).map((d) => d.getDecoration());
  }
  byType(type) {
    return this.decorations.find((d) => d.type === type) || _Decorations.empty;
  }
  toCustomStyle() {
    return this.decorations.reduce(
      (style, decoration) => ({
        ...style,
        ...decoration.toCustomStyle()
      }),
      {}
    );
  }
  overrideWith(decorations) {
    const textDecorations = decorations.map(_Decorations.toTextDecoration);
    const overridenDecorations = textDecorations.map(
      (decoration) => this.byType(decoration.type).overrideWith(decoration)
    );
    const types = overridenDecorations.map((decoration) => decoration.type);
    const mergedDecorations = this.decorations.filter((decoration) => !types.includes(decoration.type)).concat(overridenDecorations);
    return new _Decorations(mergedDecorations);
  }
};
_Decorations.empty = new import_empty_decoration.EmptyDecoration();
let Decorations = _Decorations;
//# sourceMappingURL=decorations.js.map
