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 { MapToComponentField as MapToSlimmerField } from 'vega-slimmer/core';
import { RTEListItemBlock } from '../../../../dto/blocks/list-item-block';
import { RTEImageNode } from '../../../../dto/nodes/image-node';
import { TransformListAction } from '../../../../dto/actions/transform-list-action';
import { TransformParagraphAction } from '../../../../dto/actions/transform-paragraph-action';
import { RTECodeBlockNode } from '../../../../dto/nodes/code-block-node';
import { VisualModeToolbarButtonSlimmer } from '../visual-mode-toolbar-button-slimmer.abstract';
export class BulletsToolbarButtonSlimmer extends VisualModeToolbarButtonSlimmer {
  constructor() {
    super({
      icon: 'list-ul',
      tooltip: {
        text: 'Bullets',
      },
    });
  }
  isSelected() {
    const selectedBlocks = this.selectionController.getSelectedBlocks();
    return this.isListType(selectedBlocks);
  }
  onClick() {
    if (this.isSelected()) {
      this.deselectBulletList();
    }
    else {
      this.selectBulletList();
    }
    void this.popoverRef.hide();
  }
  isDisabled() {
    const selectedNodes = this.selectionController.getSelectedNodes();
    return (super.isDisabled() ||
      selectedNodes.some((node) => node instanceof RTEImageNode || node instanceof RTECodeBlockNode));
  }
  isListType(selectedBlocks, type = 'bullet-list') {
    return (selectedBlocks.length > 0 &&
      selectedBlocks.every((block) => block instanceof RTEListItemBlock && block.parent.type === type));
  }
  /**
   * Deselect bullet list item to convert it to default paragraph
   */
  deselectBulletList() {
    const selectedBlocks = this.selectionController.getSelectedBlocks();
    if (selectedBlocks[0]) {
      selectedBlocks[0].parent.apply(new TransformParagraphAction(selectedBlocks));
    }
  }
  /**
   * Select texts or list items or texts and list items to convert it to bullet list
   */
  selectBulletList() {
    const selectedBlocks = this.selectionController.getSelectedBlocks();
    if (selectedBlocks[0]) {
      const selectedFirstBlock = selectedBlocks[0].type === 'list-item'
        ? selectedBlocks[0].parent
        : selectedBlocks[0];
      selectedFirstBlock.apply(new TransformListAction('bullet-list', selectedBlocks));
    }
  }
}
__decorate([
  MapToSlimmerField()
], BulletsToolbarButtonSlimmer.prototype, "selectionController", void 0);
__decorate([
  MapToSlimmerField()
], BulletsToolbarButtonSlimmer.prototype, "popoverRef", void 0);
