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 { CodeInputPaste } from './code-input-paste.abstract';
/**
 * Controller for handling paste events in VegaCodeBlock.
 * Handles insertion, selection, and content update logic for pasted code.
 */
export class CodeBlockPasteController extends CodeInputPaste {
  constructor() {
    super(...arguments);
    /**
     * Handle the paste event.
     *
     * @param {ClipboardEvent} e - The clipboard event object.
     * @returns {void}
     */
    this.handlePasteEvent = (e) => {
      var _a, _b;
      e.preventDefault();
      this.stopEventPropagation(e);
      const pasteData = e.clipboardData;
      if ((_a = pasteData === null || pasteData === void 0 ? void 0 : pasteData.files) === null || _a === void 0 ? void 0 : _a.length) {
        // Prevent paste if the clipboard contains files.
        return;
      }
      const pastedContent = (_b = pasteData === null || pasteData === void 0 ? void 0 : pasteData.getData('text/plain')) !== null && _b !== void 0 ? _b : '';
      if (!pastedContent) {
        return;
      }
      this.updateCodeBlockRangeAndContent(pastedContent);
    };
  }
  /**
   *  Register paste event listener on component load.
   */
  registerInputEvent() {
    const codeContentContainerRef = this.contentRenderer.getCodeContentContainerRef();
    if (codeContentContainerRef) {
      codeContentContainerRef.addEventListener('paste', this.handlePasteEvent);
    }
  }
  /**
   * Remove the event listener.
   */
  unregisterEventListener() {
    const codeContentContainerRef = this.contentRenderer.getCodeContentContainerRef();
    if (codeContentContainerRef) {
      codeContentContainerRef.removeEventListener('paste', this.handlePasteEvent);
    }
  }
  /**
   * Update the code block range based on pasted data.
   *
   * @param {string} pastedContent - The content that has been pasted.
   * @returns {void}
   */
  updateCodeBlockRangeAndContent(pastedContent) {
    const { nextOffset: updatedOffset, updatedContent } = this.getLatestRangeAndContent(pastedContent);
    this.selectionController.setNextCodeBlockRange(updatedOffset);
    this.valueController.updateValue(updatedContent, this.value.language, {
      recordHistory: true,
      offset: this.selectionController.getCurrentRange().contentOffset,
    });
  }
  /**
   * Get the latest range and content after paste operation.
   *
   * @param {string} pastedContent - The content that has been pasted.
   * @returns {NextOffsetAndContentResult}} The next offset and updated content.
   */
  getLatestRangeAndContent(pastedContent) {
    const range = this.selectionController.getCurrentRange();
    const currentNodes = this.contentParseController.getCurrentCodeNodes();
    const content = this.getCodeNodesContent(currentNodes);
    return this.reCalculateRangeAndContent(range, content, pastedContent);
  }
}
__decorate([
  MapToComponentField()
], CodeBlockPasteController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], CodeBlockPasteController.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], CodeBlockPasteController.prototype, "contentRenderer", void 0);
__decorate([
  MapToComponentField()
], CodeBlockPasteController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], CodeBlockPasteController.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], CodeBlockPasteController.prototype, "contentParseController", void 0);
__decorate([
  MapToComponentField()
], CodeBlockPasteController.prototype, "inputController", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], CodeBlockPasteController.prototype, "registerInputEvent", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], CodeBlockPasteController.prototype, "unregisterEventListener", null);
