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, VegaSlimmer } from 'vega-slimmer/core';
/**
 * The class used to control the code block insert composition text action.
 */
export class InsertCompositionTextController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.insertCompositionTextEndFlag = true;
    /**
     * An event handler function that is triggered when the composition of text input starts. In this case,
     *  it sets the `insertCompositionTextEndFlag` property of the `UserInputController` class to `false`.
     *
     * @param {CompositionEvent} e The composition event instance
     */
    this.handleCompositionStart = (e) => {
      this.insertCompositionTextEndFlag = false;
      this.stopEventPropagation(e);
    };
    /**
     * An event handler function that is triggered when the composition of text input ends. In this case,
     *  it sets the `insertCompositionTextEndFlag` property of the `UserInputController` class to `true`.
     *
     * @param {CompositionEvent} e The composition event instance
     */
    this.handleCompositionEnd = (e) => {
      var _a;
      this.insertCompositionTextEndFlag = true;
      this.stopEventPropagation(e);
      const range = this.selectionController.getCurrentRange();
      if (range) {
        this.selectionController.setNextCodeBlockRange(range.contentOffset + e.data.length);
      }
      const newContent = this.contentRenderer.getCodeContentContainerRef().innerText;
      this.valueController.updateValue(newContent, (_a = this.value) === null || _a === void 0 ? void 0 : _a.language);
    };
  }
  /**
   * The method is to get the composition text insert end or not.
   *
   * @returns {boolean} The false is mean that insert composition text process is end.
   */
  isComposing() {
    return !this.insertCompositionTextEndFlag;
  }
  /**
   * Stop `beforeinput` and `input` event bubbles to parent element.
   */
  registerInputEvent() {
    const codeContentContainerRef = this.contentRenderer.getCodeContentContainerRef();
    if (codeContentContainerRef) {
      codeContentContainerRef.addEventListener('compositionstart', this.handleCompositionStart);
      codeContentContainerRef.addEventListener('compositionend', this.handleCompositionEnd);
    }
  }
  /**
   * Remove the event listener.
   */
  unregisterEventListener() {
    const codeContentContainerRef = this.contentRenderer.getCodeContentContainerRef();
    if (codeContentContainerRef) {
      codeContentContainerRef.removeEventListener('compositionstart', this.handleCompositionStart);
      codeContentContainerRef.removeEventListener('compositionend', this.handleCompositionEnd);
    }
  }
  /**
   * Stop the event bubbles.
   *
   * @param {Event} e - The event object.
   */
  stopEventPropagation(e) {
    e.stopPropagation();
  }
}
__decorate([
  MapToComponentField()
], InsertCompositionTextController.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], InsertCompositionTextController.prototype, "contentRenderer", void 0);
__decorate([
  MapToComponentField()
], InsertCompositionTextController.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], InsertCompositionTextController.prototype, "contentParseController", void 0);
__decorate([
  MapToComponentField()
], InsertCompositionTextController.prototype, "value", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], InsertCompositionTextController.prototype, "registerInputEvent", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], InsertCompositionTextController.prototype, "unregisterEventListener", null);
