"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var style_query_exports = {};
__export(style_query_exports, {
  StylesQuery: () => StylesQuery
});
module.exports = __toCommonJS(style_query_exports);
var import_lodash = __toESM(require("lodash"));
class StylesQuery {
  // oxlint-disable-next-line no-useless-constructor
  constructor(styles) {
    this.styles = styles;
  }
  /**
   * It returns the decoration of a text block.
   * @param {ITreeNodeQuery} node - ITreeNodeQuery - the node you want to get the decoration from
   * @param {Decoration_Type} decorationType - Decoration_Type
   * @returns A Decoration object
   */
  getTextBlockDecoration(node, decorationType) {
    if (!node.isTextBlock()) {
      throw "getTextBlockDecoration: node is not a text block";
    }
    const ricosNode = node.toNode();
    const decoration = this.styles.getDecoration(ricosNode, decorationType);
    if (import_lodash.default.isEmpty(decoration)) {
      return void 0;
    }
    return decoration;
  }
  /**
   * `getTextDecoration` returns the decoration of the given type found in the given node
   * @param {ITreeNodeQuery} node - ITreeNodeQuery - the node you want to get the decoration from
   * @param {Decoration_Type} decorationType - Decoration_Type - The type of decoration you want to
   * get.
   * @returns A Decoration object
   */
  getTextDecoration(node, decorationType) {
    if (!node.isText()) {
      throw "getTextDecoration: node is not a text";
    }
    const ricosNode = node.toNode();
    return ricosNode?.textData?.decorations?.find(
      (decoration) => decoration.type === decorationType
    );
  }
  /**
   * It returns the text style of a text block node
   * @param {ITreeNodeQuery} node - ITreeNodeQuery - the node that you want to get the style for
   * @returns A partial of the TextStyle object.
   */
  getTextBlockStyle(node) {
    const ricosNode = node.toNode();
    if (!node.isTextBlock()) {
      throw "getTextBlockStyle: node is not a textBlock";
    }
    const dataProperty = `${import_lodash.default.camelCase(ricosNode?.type)}Data`;
    const getValue = (key) => (
      // @ts-ignore
      ricosNode?.[dataProperty]?.textStyle?.[key] || node.getAttributes().textStyle?.[key] || this.styles.getTextStyle(ricosNode)[key]
    );
    return {
      lineHeight: getValue("lineHeight"),
      // use default value of `AUTO` instead of `undefined` for all resolvers to correctly
      // work in RTL
      textAlignment: getValue("textAlignment") ?? "AUTO"
    };
  }
  /**
   * "return the text style property of the closest text block parent."
   *
   * @param {ITreeNodeQuery} node - ITreeNodeQuery - The node you want to get the style from.
   * @param {string} textStylePropery - The property of the text style you want to get.
   * @returns The computed text style of the node.
   */
  getComputedTextStyle(node, textStylePropery) {
    const textBlockNode = node.closest((node2) => node2.isTextBlock());
    if (textBlockNode) {
      return this.getTextBlockStyle(textBlockNode)?.[textStylePropery];
    }
  }
  /**
   * If the node is a text node, get the decoration for that node. If the node is not a text node, get
   * the decoration for the closest text block parent
   * @param {ITreeNodeQuery} node - ITreeNodeQuery - The node you want to get the decoration for.
   * @param {Decoration_Type} decorationType - Decoration_Type
   * @returns The computed decoration for the node.
   */
  getComputedDecoration(node, decorationType) {
    let parentNodeDecoration;
    let decoration;
    const parentNode = node.parent?.closest((node2) => node2.isTextBlock());
    if (node.isText()) {
      decoration = this.getTextDecoration(node, decorationType);
    }
    if (!parentNode) {
      return decoration;
    } else {
      parentNodeDecoration = this.getTextBlockDecoration(parentNode, decorationType);
    }
    return decoration || parentNodeDecoration;
  }
}
//# sourceMappingURL=style-query.js.map
