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 { h } from '@stencil/core';
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { arrayToClassString, findParent } from '../../../../../../../utils/ui';
export class SelectionWidgetRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Handles the selection of the widget.
     *
     * @param {MouseEvent} e - The mouse event.
     */
    this.handleWidgetSelection = (e) => {
      e.stopPropagation();
      this.isSelected = true;
    };
    /**
     * Handles clicks outside the widget to deselect it.
     */
    this.handleEditorContentClick = () => {
      this.isSelected = false;
    };
    /**
     * Registers the editor content click listener.
     */
    this.registerEditorContentClickListener = () => {
      if (this.editorContentContainerRef) {
        this.editorContentContainerRef.addEventListener('mousedown', this.handleEditorContentClick);
      }
    };
    /**
     * Un-registers the editor content click listener.
     */
    this.unRegisterEditorContentClickListener = () => {
      if (this.editorContentContainerRef) {
        this.editorContentContainerRef.removeEventListener('mousedown', this.handleEditorContentClick);
      }
    };
  }
  connectedCallback() {
    this.editorContentContainerRef = findParent(this.host, '.rich-text-editor-container');
  }
  disconnectedCallback() {
    this.unRegisterEditorContentClickListener();
  }
  watchIsSelected(newValue) {
    if (newValue) {
      this.registerEditorContentClickListener();
    }
    else {
      this.unRegisterEditorContentClickListener();
    }
  }
  getWidgetHandleRef() {
    return this.widgetHandleRef;
  }
  render() {
    return (h("div", { class: arrayToClassString(['v-rte-widget__selection', this.isSelected ? 'selected' : '']), contentEditable: false },
      h("button", { tabIndex: 0, class: "v-rte-widget__selection-handle", ref: (el) => (this.widgetHandleRef = el), onClick: this.handleWidgetSelection }),
      h("div", { class: "v-rte-widget__selection-content" },
        h("slot", null))));
  }
}
__decorate([
  MapToComponentField()
], SelectionWidgetRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], SelectionWidgetRenderer.prototype, "isSelected", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], SelectionWidgetRenderer.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], SelectionWidgetRenderer.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('watchIsSelected')
], SelectionWidgetRenderer.prototype, "watchIsSelected", null);
