/**
 * Represents the state of the RTE
 */
export class RTEEditorState {
  constructor(content, selection) {
    this.content = content.clone(true);
    this.selection = selection.clone();
    this.refreshSelectionNodes();
  }
  /**
   * Clones the editor state, updating the content and selection based on the provided content.
   *
   * @returns {RTEEditorState} A new instance of RTEEditorState with the updated content and selection.
   */
  clone() {
    return new RTEEditorState(this.content, this.selection);
  }
  /**
   * Refreshes the selection nodes in the editor state.
   */
  refreshSelectionNodes() {
    const { startNode: oldStartNode, endNode: oldEndNode, startOffset, endOffset, } = this.selection.range;
    if (oldStartNode && oldEndNode) {
      const startNode = this.content.findNodeById(oldStartNode.id);
      const endNode = this.content.findNodeById(oldEndNode.id);
      if (startNode && endNode) {
        this.selection.range.updateRTERange({
          startNode,
          endNode,
          startOffset,
          endOffset,
        });
      }
    }
  }
}
