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, VegaSlimmer } from 'vega-slimmer/core';
import stateEntityRenderingRegistry from './state-entity-rendering-registry';
import { RTETextBlock } from '../../dto/blocks/text-block';
import { generateUUID } from '../../../../utils/misc';
import { InsertChildrenAfterAction } from '../../dto/actions/insert-children-after-block';
/**
 * Focus controller for RTE
 */
export class FocusController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Do a focus on the last text node of the last block
     *
     * @param {RTEContentBlock} lastBlock - The last block of the content
     */
    this.doFocusOnLastTextNode = async (lastBlock) => {
      const lastNode = lastBlock.getLastNode();
      const dom = stateEntityRenderingRegistry.getDOMByEntity(lastNode);
      if (dom) {
        if (!lastNode.isContentEditable()) {
          this.setCaretRangeToNodeEnd(dom.parentElement, lastNode.getRangeEndOffset());
        }
        else {
          this.setCaretRangeToNodeEnd(dom.firstChild, dom.firstChild.nodeValue.length);
        }
        await this.vegaRichTextEditorRenderer.getRichTextContentRef().vegaFocus();
      }
    };
  }
  /**
   * Focus on the last text node of the last block.
   *
   * @param {{ newLine?: boolean }} options - The options of the method
   * @param {boolean} options.newLine - Should a new line be added before the last text node
   * @returns {Promise<void>} - A promise that resolves when the focus is set
   */
  async focusOnLastTextNode(options) {
    const lastBlock = this.value.children[this.value.children.length - 1];
    if (!(lastBlock instanceof RTETextBlock) || (options === null || options === void 0 ? void 0 : options.newLine)) {
      const newTextBlock = RTETextBlock.from({
        id: generateUUID(),
        type: 'paragraph',
        nodes: [{ id: generateUUID(), type: 'text', text: '' }],
      });
      lastBlock.parent.apply(new InsertChildrenAfterAction(lastBlock, newTextBlock));
      await this.valueController.flushChanges(this.value);
      await this.doFocusOnLastTextNode(newTextBlock);
    }
    else {
      await this.doFocusOnLastTextNode(lastBlock);
    }
  }
  /**
   * Move the caret to the end of the node
   *
   * @param {Node} targetNode - The node to move the caret to
   * @param {number} offset - The offset to move the caret to
   */
  setCaretRangeToNodeEnd(targetNode, offset) {
    const range = new Range();
    range.setStart(targetNode, offset);
    this.selectionController.setSelectionRange(range);
  }
}
__decorate([
  MapToComponentField()
], FocusController.prototype, "vegaRichTextEditorRenderer", void 0);
__decorate([
  MapToComponentField()
], FocusController.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], FocusController.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], FocusController.prototype, "value", void 0);
