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 { ComponentValueHistoryControllerSlimmer } from '../../../../helpers/slimmers/component-value-history-controller-slimmer.abstract';
/**
 * The class used to control the code block history.
 * It extends the ComponentValueHistoryControllerSlimmer to manage the history of code block states.
 */
export class VegaCodeBlockHistoryController extends ComponentValueHistoryControllerSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Get the effective target elements for the code block history.
     *
     * @returns {HTMLElement[]} - An array of effective target elements.
     */
    this.getEffectiveTargets = () => {
      return [this.contentRenderer.getCodeContentContainerRef()];
    };
  }
  /**
   * Invoke the method to back up the current code block state.
   */
  async undo() {
    if (this.undoStack.length > 1) {
      const clonedState = this.undoStack.pop();
      this.redoStack.push(clonedState);
      const prev = this.undoStack[this.undoStack.length - 1];
      if (prev) {
        await this.restoreState(prev);
      }
    }
  }
  /**
   * Invoke the redo action to restore the last undone state.
   */
  async redo() {
    const clonedState = this.redoStack.pop();
    this.undoStack.push(clonedState);
    await this.restoreState(clonedState);
  }
  /**
   * Set the initial state of the code block history when the component is loaded.
   */
  initialHistory() {
    this.registerHistory({
      value: this.value,
      offset: this.value.code.length,
    }, true);
  }
  /**
   * Get the ineffective target elements for the code block history.
   *
   * @returns { HTMLElement[]} - An empty array, as there are no ineffective targets for the code block.
   */
  getIneffectiveTargets() {
    return [];
  }
  /**
   * Restore the state of the code block from the given state.
   *
   * @param {CodeBlockState} state - The state to restore.
   */
  async restoreState(state) {
    const { value, offset } = state;
    this.selectionController.setNextCodeBlockRange(offset);
    this.valueController.updateValue(value.code, value.language);
  }
}
__decorate([
  MapToComponentField()
], VegaCodeBlockHistoryController.prototype, "contentRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockHistoryController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockHistoryController.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockHistoryController.prototype, "selectionController", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaCodeBlockHistoryController.prototype, "initialHistory", null);
