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 { Fragment, h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import RTEDTORendererManager from '../../../../helpers/rte-manager/dto-renderer-manager';
import { isNonNullable } from '../../../../types/type-guard';
export class ToolbarRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.getToolbarBtnRenderer = (type) => {
      switch (type) {
        case 'clearStyle':
          return this.clearStyleToolbarButtonSlimmer;
        case 'bold':
          return this.boldToolbarButtonSlimmer;
        case 'textStyles':
          return this.textStylesToolbarButtonSlimmer;
        case 'italic':
          return this.italicToolbarButtonSlimmer;
        case 'underline':
          return this.underlineToolbarButtonSlimmer;
        case 'textColor':
          return this.textColorToolbarButtonSlimmer;
        case 'moreIndent':
          return this.moreIndentToolbarButtonSlimmer;
        case 'lessIndent':
          return this.lessIndentToolbarButtonSlimmer;
        case 'code':
          return this.codeToolbarButtonSlimmer;
        case 'strikethrough':
          return this.strikethroughToolbarButtonSlimmer;
        case 'link':
          return this.linkToolbarButtonSlimmer;
        case 'image':
          return this.imageToolbarButtonSlimmer;
        case 'bulletsNumbers':
          return this.bulletsNumbersToolbarButtonSlimmer;
        case 'horizontalAlignment':
          return this.horizontalAlignmentToolbarButtonSlimmer;
        case 'codeBlock':
          return this.codeBlockToolbarButtonSlimmer;
      }
    };
  }
  render() {
    const [renderedItems, totalVisibleCount] = this.renderToolbarItems();
    return (h("vega-flex", { gap: "size-8", alignItems: "center", flexWrap: "wrap" },
      this.allowSourceEdit && (h(Fragment, null,
        h("div", { class: "toolbar-btn-group" }, this.sourceEditToolbarButtonSlimmer.render()),
        this.toolbarItems.length > 0 && totalVisibleCount > 0 && this.renderVegaDivider())),
      renderedItems));
  }
  renderVegaDivider() {
    return (h("vega-box", { height: "size-32" },
      h("vega-divider", { direction: "vertical" })));
  }
  /**
   * Renders the toolbar items based on the configured toolbar groups and items.
   *
   * @returns {[HTMLElement[], number]} A tuple containing the rendered toolbar items and the count of displayed items.
   */
  renderToolbarItems() {
    let totalVisibleCount = 0;
    const renderedItems = this.toolbarItems.map((group) => {
      let visibleCountPerGroup = 0;
      const groupElement = group
        .map((item) => {
        const presetRenderer = this.getToolbarBtnRenderer(item);
        const customRenderer = RTEDTORendererManager.getRTEToolbarButtonRenderer(item, this.host);
        if (presetRenderer) {
          if (!presetRenderer.isHidden()) {
            totalVisibleCount++;
            visibleCountPerGroup++;
          }
          return presetRenderer.render();
        }
        else if (customRenderer) {
          const renderContext = {
            sourceEditMode: this.sourceView,
            host: this.host,
            rteRange: this.selectionController.getCurrentRange(),
          };
          if (!customRenderer.isHidden(renderContext)) {
            totalVisibleCount++;
            visibleCountPerGroup++;
          }
          return customRenderer.render(h, renderContext);
        }
        return null;
      })
        .filter(isNonNullable);
      return (h("div", { class: { 'toolbar-btn-group': true, 'vega-hidden': visibleCountPerGroup < 1 } }, groupElement));
    });
    return [renderedItems, totalVisibleCount];
  }
}
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "toolbarItems", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "allowSourceEdit", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "clearStyleToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "boldToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "textStylesToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "italicToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "underlineToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "textColorToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "bulletsNumbersToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "horizontalAlignmentToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "strikethroughToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "imageToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "moreIndentToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "lessIndentToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "codeToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "linkToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "codeBlockToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "sourceEditToolbarButtonSlimmer", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], ToolbarRenderer.prototype, "sourceView", void 0);
