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 rectangleCode from '../../../assets/rectangle-code';
import { VegaInternalIconManager } from '../../../../../helpers/icon/internal-icon-manager';
import { MapToComponentField } from 'vega-slimmer/core';
import { TransformToCodeBlockAction } from '../../../dto/actions/transform-to-code-block-action';
import { VisualModeToolbarButtonSlimmer } from './visual-mode-toolbar-button-slimmer.abstract';
import { ModifyContentActionType } from '../../../dto/actions/modify-content-action.abstract';
import { RTECodeBlock } from '../../../dto/blocks/code-block';
import { ActionHandleStrategyRegistry } from '../../../dto/action-handle-strategies/action-handle-strategy-registry';
import { RTETextBlock } from '../../../dto/blocks/text-block';
/**
 * The code block toolbar button slimmer.
 */
export class CodeBlockToolbarButtonSlimmer extends VisualModeToolbarButtonSlimmer {
  constructor() {
    super({
      icon: 'rectangle-code',
      tooltip: { text: 'Code Block' },
    });
  }
  /**
   * The selected blocks should convert to a code block.
   */
  onClick() {
    const selected = this.isSelected();
    if (!selected) {
      const selectedBlocks = this.selectionController.getSelectedBlocks();
      const parentBlock = selectedBlocks[0].parent;
      const transformToCodeBlockAction = new TransformToCodeBlockAction(selectedBlocks);
      parentBlock.apply(transformToCodeBlockAction);
      void this.valueController.flushChanges(this.value);
      this.selectionController.enqueueSelectionRangeFutureState(transformToCodeBlockAction.newCodeBlock.children[0], 0);
    }
  }
  /**
   * The button will be selected if the selected blocks all code block.
   *
   * @returns {boolean} - The code block should selected or not.
   */
  isSelected() {
    const selectBlocks = this.selectionController.getSelectedBlocks();
    if (selectBlocks.length && selectBlocks.every((block) => block.type === 'code-block')) {
      return true;
    }
    return false;
  }
  isDisabled() {
    const selectBlocks = this.selectionController.getSelectedBlocks();
    return (super.isDisabled() ||
      selectBlocks.some((block) => !(block instanceof RTECodeBlock) && this.shouldDisabledCodeBlockToolbar(block)));
  }
  shouldDisabledCodeBlockToolbar(block) {
    return (block &&
      !(block instanceof RTETextBlock &&
        ActionHandleStrategyRegistry.canHandle(ModifyContentActionType.TRANSFORM_TO_CODE_BLOCK, block.parent)));
  }
}
(() => {
  VegaInternalIconManager.register({ 'rectangle-code': rectangleCode });
})();
__decorate([
  MapToComponentField()
], CodeBlockToolbarButtonSlimmer.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], CodeBlockToolbarButtonSlimmer.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], CodeBlockToolbarButtonSlimmer.prototype, "value", void 0);
