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 { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaDropdownClick, VegaDropdownHide, VegaDropdownShow, VegaReset, } from '../../../../helpers/event-manager/event-id/vega-event-id';
/**
 * Renderer class for the Vega App Header Button Dropdown.
 * This class extends the VegaSlimmer and is responsible for rendering the dropdown component and managing its state and events.
 */
export class VegaAppHeaderButtonDropdownRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * emit dropdown click event when component get dropdown item click
     *
     * @param {NotifyObserverPayload<string>} input the click detail
     */
    this.triggerDropdownClickEvent = (input) => {
      this.dropdownClickEventEmitter.emit(input.detail);
    };
    /**
     * emit dropdown reset event when component get dropdown selected items reset
     *
     * @param {NotifyObserverPayload<string>} input the reset detail
     */
    this.triggerResetEvent = (input) => {
      this.resetEventEmitter.emit(input.detail);
    };
    /**
     * Shows the dropdown component.
     */
    this.onDropdownShow = () => {
      this.isDropdownShow = true;
    };
    /**
     * Hides the dropdown component.
     */
    this.onDropdownHide = () => {
      this.isDropdownShow = false;
    };
  }
  connectedCallback() {
    this.isTemplateMode = this.hasSlotItems();
  }
  /**
   * The function appends host items to a dropdown element.
   */
  appendHostItemsToDropdown() {
    const items = this.host.querySelector('[slot=items]');
    if (items && this.itemsSlotRef && this.dropdownRef) {
      this.itemsSlotRef.remove();
      this.dropdownRef.append(items);
    }
  }
  /**
   * Determines if the dropdown should be rendered based on the presence of the dropdown configuration.
   *
   * @returns {boolean} True if the dropdown configuration is present, otherwise false.
   */
  shouldRenderDropdown() {
    return !!((this.dropdown && this.dropdown.source && this.dropdown.source.length) ||
      this.isTemplateMode);
  }
  getDropdownRef() {
    return this.dropdownRef;
  }
  /**
   * Renders the dropdown component with the provided children.
   *
   * @param {HTMLElement} children HTML element children to be rendered inside the dropdown.
   * @returns {HTMLElement} The rendered dropdown component.
   */
  render(children) {
    return (h("vega-dropdown", Object.assign({ trigger: "click" }, this.dropdown, { ref: (ref) => {
        this.dropdownRef = ref;
        this.dropdownEventPrevent.setup();
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaDropdownShow, () => {
          this.onDropdownShow();
        });
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaDropdownHide, () => {
          this.onDropdownHide();
        });
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaDropdownClick, this.triggerDropdownClickEvent);
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaReset, this.triggerResetEvent);
      } }),
      children,
      this.renderItemsSlot()));
  }
  /**
   * Renders the items slot.
   *
   * @returns {Nullable<HTMLElement>} The rendered items slot.
   */
  renderItemsSlot() {
    return (this.isTemplateMode && (h("div", { slot: "items", ref: (ref) => {
        this.itemsSlotRef = ref;
      } })));
  }
  /**
   * Checks if the dropdown has items.
   *
   * @returns {boolean} True if the dropdown has items, otherwise false.
   */
  hasSlotItems() {
    return !!this.host.querySelector('[slot=items]');
  }
}
__decorate([
  MapToComponentField()
], VegaAppHeaderButtonDropdownRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaAppHeaderButtonDropdownRenderer.prototype, "dropdown", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaAppHeaderButtonDropdownRenderer.prototype, "isDropdownShow", void 0);
__decorate([
  MapToComponentField()
], VegaAppHeaderButtonDropdownRenderer.prototype, "dropdownEventPrevent", void 0);
__decorate([
  MapToComponentField()
], VegaAppHeaderButtonDropdownRenderer.prototype, "dropdownClickEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaAppHeaderButtonDropdownRenderer.prototype, "resetEventEmitter", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaAppHeaderButtonDropdownRenderer.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaAppHeaderButtonDropdownRenderer.prototype, "appendHostItemsToDropdown", null);
