import lodash from "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 (lodash.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 = `${lodash.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;
  }
}
export {
  StylesQuery
};
//# sourceMappingURL=style-query.js.map
