import { methodPlaceholder } from 'vega-slimmer/core';
import { VegaRTEToolbarButtonRenderer } from '../base-toolbar-button-renderer';
/**
 * Toolbar button renderer for language selection in Vega Rich Text Editor.
 */
export class RTELanguageToolbarButtonRenderer extends VegaRTEToolbarButtonRenderer {
  constructor(languageList, languageChangeCallback, selectedLanguage) {
    super();
    this.languageList = [];
    this.selectedLanguage = 'Select Language';
    this.labelRef = null;
    /**
     * Handles the language selection event.
     *
     * @param {string} itemKey - The key of the selected language item.
     */
    this.handleDropdownClick = (itemKey) => {
      this.setSelectedLanguage(itemKey);
      if (typeof this.languageChangeCallback === 'function') {
        this.languageChangeCallback(this.selectedLanguage);
      }
    };
    this.languageList = languageList;
    this.selectedLanguage = selectedLanguage;
    this.languageChangeCallback = languageChangeCallback;
  }
  /**
   * Public method to set the selected language.
   *
   * @param {string} language The language key to set as selected.
   */
  setSelectedLanguage(language) {
    this.selectedLanguage = language;
    if (this.labelRef) {
      this.labelRef.textContent = this.getLanguageLabelByKey(language);
    }
  }
  /**
   * The source edit mode does not affect the language button visibility.
   *
   * @returns {boolean} True if the button is hidden, false otherwise.
   */
  isHidden() {
    return false;
  }
  /**
   * @inheritDoc
   */
  render(createElement, editorContext) {
    return this.renderDropdown(createElement, editorContext, { source: this.languageList }, [
      this.renderButton(createElement, editorContext, {
        label: this.getLanguageLabelByKey(this.selectedLanguage),
        showArrowIcon: true,
      }),
    ]);
  }
  /**
   * Override to render the button label with custom behavior.
   *
   * @inheritDoc
   */
  renderButtonLabel(h, editorContext, label) {
    const isDisabled = this.isDisabled(editorContext);
    return h('vega-font', {
      variant: 'font-field-label-sm',
      color: isDisabled ? 'text-disabled' : 'text-primary',
      // eslint-disable-next-line jsdoc/require-jsdoc
      ref: (el) => (this.labelRef = el),
    }, label);
  }
  /* istanbul ignore next */
  /**
   * Handles the button click event. No need this method for language button.
   *
   * @param {VegaRTEToolbarRenderContext} editorContext - The render context for the editor.
   */
  handleButtonClick(editorContext) {
    methodPlaceholder(editorContext);
  }
  /**
   * Gets the label of a language by its key.
   *
   * @param {string} key The language key to look up.
   * @returns {string} The label of the language, or the key if not found.
   */
  getLanguageLabelByKey(key) {
    const language = this.getListItemByKey(this.languageList, key);
    return language.label;
  }
}
