var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, MapToComponentMethod } from 'vega-slimmer/core';
import { RTEEditorState } from '../../dto/editor-state';
import { RTESelectionState } from '../../dto/selection-state';
import { ComponentValueHistoryControllerSlimmer } from '../../../../helpers/slimmers/component-value-history-controller-slimmer.abstract';
/**
 * History controller for RTE.
 */
export class HistoryController extends ComponentValueHistoryControllerSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Gets the effective target elements for the RTE history.
     *
     * @returns {HTMLElement[]} - An array of effective target elements.
     */
    this.getEffectiveTargets = () => {
      return [this.vegaRichTextEditorRenderer.getRTEEditableElementRef()];
    };
    /**
     * Gets the ineffective target elements for the RTE history.
     *
     * @returns {HTMLElement[]} - An array of ineffective target elements.
     */
    this.getIneffectiveTargets = () => {
      const editorElementRef = this.vegaRichTextEditorRenderer.getRTEEditableElementRef();
      let result = [];
      if (editorElementRef) {
        result = Array.from(editorElementRef.querySelectorAll('.code-block-container'));
      }
      return result;
    };
  }
  /**
   * Component lifecycle - [componentDidLoad]
   */
  registerInitState() {
    if (this.value) {
      this.registerHistory(new RTEEditorState(this.value, new RTESelectionState(this.selectionController.getCurrentRange())), true);
    }
  }
  /**
   * Restores the previous state from the undo stack.
   */
  async undo() {
    if (this.undoStack.length > 1) {
      const clonedState = this.undoStack.pop().clone();
      this.redoStack.push(clonedState);
      const prev = this.undoStack[this.undoStack.length - 1];
      if (prev) {
        await this.restoreState(prev);
      }
    }
  }
  /**
   * Restores the next state from the redo stack.
   */
  async redo() {
    if (this.redoStack.length > 0) {
      const clonedState = this.redoStack.pop().clone();
      this.undoStack.push(clonedState);
      await this.restoreState(clonedState);
    }
  }
  /**
   * Restores the editor state based on the provided history state.
   *
   * @param {RTEEditorState} state - The state to restore to.
   */
  async restoreState(state) {
    const { content, selection } = state.clone();
    this.selectionController.setSelectionState(selection);
    await this.valueController.flushChanges(content, false);
  }
}
__decorate([
  MapToComponentField()
], HistoryController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], HistoryController.prototype, "vegaRichTextEditorRenderer", void 0);
__decorate([
  MapToComponentField()
], HistoryController.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], HistoryController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], HistoryController.prototype, "selectionController", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], HistoryController.prototype, "registerInitState", null);
