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 { VegaInternalIconManager } from '../../../../../helpers/icon/internal-icon-manager';
import textSize from '../../../assets/text-size';
import { MapToComponentField } from 'vega-slimmer/core';
import { TextStyleAnnotationAction } from '../../../dto/actions/text-style-annotation-action';
import { RTETextBlock } from '../../../dto/blocks/text-block';
import { TextStyleAnnotation } from '../../../dto/annotations/text-style-annotation';
import { BoldAnnotation } from '../../../dto/annotations/bold-annotation';
import { BlockAnnotationTypeEnum } from '../../../dto/annotations/block-annotation.abstract';
import { DomNodeSubjectObserverFactory } from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaDropdownClick } from '../../../../../helpers/event-manager/event-id/vega-event-id';
import { VisualModeToolbarButtonSlimmer } from './visual-mode-toolbar-button-slimmer.abstract';
export class TextStylesToolbarButtonSlimmer extends VisualModeToolbarButtonSlimmer {
  constructor() {
    super({
      icon: 'text-size',
      tooltip: { text: 'Text Styles' },
    });
    this.handleDropdownItemClick = (payload) => {
      const selectedType = payload.detail;
      const boldByDefault = TextStylesToolbarButtonSlimmer.items.find((item) => item.key === selectedType).bold;
      const selectedTextBlocks = this.selectionController
        .getSelectedBlocks()
        .filter((block) => block instanceof RTETextBlock);
      if (selectedTextBlocks.length > 0) {
        selectedTextBlocks.forEach((item) => {
          item.apply(new TextStyleAnnotationAction(selectedType, boldByDefault));
        });
      }
      else {
        void this.focusController.focusOnLastTextNode({ newLine: true }).then(() => {
          this.selectionController
            .getSelectedBlocks()
            .filter((node) => node instanceof RTETextBlock)
            .forEach((item) => {
            item.apply(new TextStyleAnnotationAction(selectedType, boldByDefault));
          });
        });
      }
    };
  }
  renderBtnWrapper(renderBtn) {
    return (h("vega-dropdown", { selectType: "single", trigger: "click", alignment: "center", selectedSourceKey: this.getSelectedSourceKey(), ref: (ref) => {
        this.addDropdownObserver(ref);
      } },
      renderBtn({ showArrowIcon: true }),
      h("div", Object.assign({ slot: "items" }, super.preventLostFocus()), this.renderDropdownContent())));
  }
  renderDropdownContent() {
    return (h(Fragment, null, TextStylesToolbarButtonSlimmer.items.map((item) => {
      const textStyleAnnotation = new TextStyleAnnotation(item.key);
      const boldAnnotation = new BoldAnnotation(item.bold);
      return (h("vega-dropdown-item", { key: item.key, itemKey: item.key },
        h("vega-box", { padding: { right: 'size-20' } },
          h("div", { style: Object.assign(Object.assign({}, textStyleAnnotation.renderStyle({ standalone: true })), boldAnnotation.renderStyle({ standalone: true })) }, this.translationSlimmer.t(item.label)))));
    })));
  }
  isSelected() {
    return false;
  }
  isDisabled() {
    const selectedBlocks = this.selectionController.getSelectedBlocks();
    return (super.isDisabled() ||
      selectedBlocks.some((node) => this.shouldDisabledToolbar(node, TextStyleAnnotationAction.name)));
  }
  addDropdownObserver(dropdownRef) {
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(dropdownRef, VegaDropdownClick, this.handleDropdownItemClick);
  }
  getSelectedSourceKey() {
    var _a;
    const currentSelectedBlocks = this.selectionController
      .getSelectedBlocks()
      .filter((block) => block instanceof RTETextBlock);
    const isSameType = currentSelectedBlocks.length > 0 &&
      currentSelectedBlocks.every((block) => block.type === currentSelectedBlocks[0].type);
    if (isSameType) {
      const blockAnnotation = currentSelectedBlocks[0].getAnnotationByType(BlockAnnotationTypeEnum.TEXT_STYLE);
      return (_a = blockAnnotation === null || blockAnnotation === void 0 ? void 0 : blockAnnotation.textStyle) !== null && _a !== void 0 ? _a : '';
    }
    return '';
  }
}
(() => {
  VegaInternalIconManager.register({
    'text-size': textSize,
  });
})();
TextStylesToolbarButtonSlimmer.items = [
  {
    key: 'title',
    label: 'Title',
    bold: true,
  },
  {
    key: 'subtitle',
    label: 'Subtitle',
  },
  {
    key: 'heading-1',
    label: 'Heading 1',
    bold: true,
  },
  {
    key: 'heading-2',
    label: 'Heading 2',
    bold: true,
  },
  {
    key: 'heading-3',
    label: 'Heading 3',
    bold: true,
  },
  {
    key: 'paragraph',
    label: 'Paragraph',
  },
];
__decorate([
  MapToComponentField()
], TextStylesToolbarButtonSlimmer.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], TextStylesToolbarButtonSlimmer.prototype, "focusController", void 0);
