import { VegaRTEToolbarButtonRenderer } from '../base-toolbar-button-renderer';
import { methodPlaceholder } from 'vega-slimmer/core';
import { VegaInternalIconManager } from '../../../../helpers/icon/internal-icon-manager';
import { INSERT_TABLE_BLOCK } from './actions/insert-table-block-action';
import tableIcon from './table-icon';
import { ActionHandleStrategyRegistry } from '../../dto/action-handle-strategies/action-handle-strategy-registry';
import { ModifyContentActionType } from '../../dto/actions/modify-content-action.abstract';
import { RTETableToolbarPopoverRenderer } from './renderers/table-toolbar-popover-renderer';
/**
 * Function toolbar button renderer class.
 */
export class RTETableToolbarButtonRenderer extends VegaRTEToolbarButtonRenderer {
  /**
   * @inheritDoc
   */
  render(h, editorContext) {
    const createTablePopoverRenderer = new RTETableToolbarPopoverRenderer(editorContext);
    return createTablePopoverRenderer.render(h, super.renderButton(h, editorContext, {
      showArrowIcon: true,
      icon: 'rte-table',
      tooltip: { text: 'Insert table' },
    }));
  }
  /**
   * Overrides the isDisabled method to determine if the button should be disabled.
   *
   * @param {VegaRTEToolbarRenderContext} editorContext - The render context for the editor.
   * @returns {boolean} - true if the button should be disabled, false otherwise.
   */
  isDisabled(editorContext) {
    let result = super.isDisabled(editorContext);
    const extensionContext = this.getExtensionContext(editorContext.host);
    if (extensionContext) {
      const { host, getSelectedNodes } = extensionContext;
      const focusNode = getSelectedNodes()[0];
      let enableInsert = true;
      if (focusNode) {
        enableInsert =
          ActionHandleStrategyRegistry.canHandle(INSERT_TABLE_BLOCK, focusNode.parent) &&
            ActionHandleStrategyRegistry.canHandle(ModifyContentActionType.INSERT_CHILDREN_AFTER, focusNode.parent.parent);
      }
      result = host.disabled || !enableInsert;
    }
    return result;
  }
  /**
   * @inheritDoc
   */
  handleButtonClick() {
    methodPlaceholder();
  }
  /* istanbul ignore next */
  /**
   * @inheritDoc
   */
  handleDropdownClick() {
    methodPlaceholder();
  }
}
(() => {
  VegaInternalIconManager.register({ 'rte-table': tableIcon });
})();
