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, VegaSlimmer } from 'vega-slimmer/core';
import { DomNodeSubjectObserverFactory } from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaClick } from '../../../../../helpers/event-manager/event-id/vega-event-id';
export class ToolbarButtonSlimmer extends VegaSlimmer {
  constructor({ icon, tooltip, autoFocusOnClick = true }) {
    super();
    this.renderBtn = ({ showArrowIcon, iconColor } = {}) => {
      var _a;
      return (h("vega-tooltip", { text: this.translationSlimmer.t(this.tooltip.text), placement: ['bottom', 'top'] },
        h("vega-rich-text-editor-toolbar-button", Object.assign({ showArrowIcon: showArrowIcon, icon: this.icon, iconColor: iconColor, ref: (ref) => {
            DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaClick, () => {
              this.handleClick();
            });
          }, selected: this.isSelected(), disabled: this.isDisabled(), hidden: this.isHidden() }, this.preventLostFocus()), (_a = this.renderSlotContent) === null || _a === void 0 ? void 0 : _a.call(this))));
    };
    /**
     * Handle the click event on the toolbar button.
     * If there are no selected nodes, the focus will be set on the content and the click will be triggered after the selection change.
     */
    this.handleClick = () => {
      if (typeof this.onClick === 'function') {
        const selectedNodes = this.selectionController.getSelectedNodes();
        if (!selectedNodes.length && this.autoFocusOnClick) {
          void this.focusController.focusOnLastTextNode().then(() => {
            this.onClick();
          });
        }
        else {
          this.onClick();
        }
      }
    };
    this.icon = icon;
    this.tooltip = tooltip;
    this.autoFocusOnClick = autoFocusOnClick;
  }
  setIcon(icon) {
    this.icon = icon;
  }
  /**
   * Whether the button is hidden or not.
   */
  isHidden() {
    return false;
  }
  render() {
    return typeof this.renderBtnWrapper === 'function'
      ? this.renderBtnWrapper(this.renderBtn)
      : this.renderBtn();
  }
  /**
   * Whether the button is disabled or not.
   */
  isDisabled() {
    return this.disabled;
  }
  preventLostFocus() {
    return {
      onMouseDown: (e) => e.preventDefault(),
    };
  }
}
__decorate([
  MapToComponentField()
], ToolbarButtonSlimmer.prototype, "translationSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarButtonSlimmer.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], ToolbarButtonSlimmer.prototype, "focusController", void 0);
__decorate([
  MapToComponentField()
], ToolbarButtonSlimmer.prototype, "disabled", void 0);
