import { methodPlaceholder } from 'vega-slimmer/core';
import { VegaRTEToolbarButtonRenderer } from '../base-toolbar-button-renderer';
import { ActionHandleStrategyRegistry } from '../../dto/action-handle-strategies/action-handle-strategy-registry';
import { InsertTokenNodeAction } from './insert-token-node-action';
/**
 * Renderer for the token toolbar button in the Vega Rich Text Editor.
 */
export class RTETokenToolbarButtonRenderer extends VegaRTEToolbarButtonRenderer {
  constructor(tokenList) {
    super();
    this.tokenList = [];
    this.tokenList = tokenList;
  }
  /**
   * Renders the token toolbar button.
   *
   * @param {VegaRTECreateElementFunction} createElement Function to create elements in the rich text editor.
   * @param {VegaRTEToolbarRenderContext} editorContext The render context for the editor.
   * @returns {VegaRTERenderResult} Token toolbar button.
   */
  render(createElement, editorContext) {
    return this.renderDropdown(createElement, editorContext, {}, [
      this.renderButton(createElement, editorContext, { label: 'Tokens', showArrowIcon: true }),
      this.renderDropdownListTemplate(createElement, this.tokenList),
    ]);
  }
  /**
   * Checks if the token toolbar button is disabled.
   *
   * @param {VegaRTEToolbarRenderContext} editorContext The render context for the editor.
   * @returns {boolean} True if the button is disabled, false otherwise.
   */
  isDisabled(editorContext) {
    const context = this.getExtensionContext(editorContext.host);
    if (context) {
      const { host, getSelectedNodes } = context;
      const [startNode] = getSelectedNodes();
      return startNode
        ? host.disabled ||
          !ActionHandleStrategyRegistry.canHandle('INSERT_TOKEN_NODE', startNode.parent)
        : host.disabled;
    }
    return false;
  }
  /* istanbul ignore next */
  /**
   * Handles the button click event. No need this method for token button.
   *
   * @param {VegaRTEToolbarRenderContext} editorContext - The render context for the editor.
   */
  handleButtonClick(editorContext) {
    methodPlaceholder(editorContext);
  }
  /**
   * Handles the token item click event from the dropdown.
   *
   * @param {string} itemKey - The key of the clicked token item.
   * @param {VegaRTEToolbarRenderContext} editorContext - The render context for the editor.
   */
  handleDropdownClick(itemKey, editorContext) {
    const token = this.getListItemByKey(this.tokenList, itemKey);
    const { rteRange } = editorContext;
    const { startNode } = rteRange;
    if (startNode && startNode.parent) {
      const action = new InsertTokenNodeAction(rteRange, token);
      startNode.parent.apply(action);
      if (action.actionHandleResult) {
        this.flushEditorUI(editorContext);
      }
    }
  }
  /**
   * Refreshes the editor UI.
   *
   * @param {VegaRTEToolbarRenderContext} editorContext - The render context for the editor.
   */
  flushEditorUI(editorContext) {
    const context = this.getExtensionContext(editorContext.host);
    if (context) {
      const { host, flushValue } = context;
      flushValue(host.value);
    }
  }
}
