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, VegaSlimmer } from 'vega-slimmer/core';
import { Fragment, h } from '@stencil/core';
import { VegaInternalIconManager } from '../../../../helpers/icon/internal-icon-manager';
import trashCan from '../../../../global/icons/trash-can';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaChange, VegaClick } from '../../../../helpers/event-manager/event-id/vega-event-id';
import { CodeLanguage } from '../../types';
/**
 * The code block editor renderer class.
 */
export class VegaCodeBlockRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.languageDropdownProps = {
      searchable: false,
    };
    this.languageOptions = Object.keys(CodeLanguage).map((key) => {
      return { id: CodeLanguage[key], displayName: key };
    });
    /**
     * Handle the code language update.
     *
     * @param {HTMLVegaInputSelectElement} node - The language select element.
     */
    this.handleLanguageChange = (node) => {
      DomNodeSubjectObserverFactory.addUniqueObserverToNode(node, VegaChange, (payload) => {
        var _a;
        this.valueController.updateValue((_a = this.value) === null || _a === void 0 ? void 0 : _a.code, payload.detail);
      });
    };
    /**
     * Handle the code language update.
     *
     * @param {HTMLVegaButtonCircleElement} node - The delete button.
     */
    this.handleDeleteClick = (node) => {
      DomNodeSubjectObserverFactory.addUniqueObserverToNode(node, VegaClick, () => {
        const event = this.removeEventEmitter.emit();
        if (!event.defaultPrevented) {
          this.host.remove();
        }
      });
    };
  }
  /**
   * Get the toolbar container element ref.
   *
   * @returns {HTMLDivElement} - The div element.
   */
  getToolbarContainerRef() {
    return this.toolbarContainerRef;
  }
  /**
   * Render the code block editor toolbar element or copy element.
   *
   * @returns {HTMLElement} - The code block operate action button.
   */
  render() {
    return (h(Fragment, null,
      h("div", { part: "editor-toolbar", contenteditable: false, class: {
          'editor-toolbar': !this.readOnly,
          'copy-button-container': this.showCopyButton && this.readOnly,
        }, ref: (node) => (this.toolbarContainerRef = node) },
        this.renderEditorToolbar(),
        this.copyButtonRenderer.render()),
      this.contentRenderer.render()));
  }
  /**
   * Render a language select and delete button.
   *
   * @returns {Nullable<HTMLElement>} The editor toolbar container element.
   */
  renderEditorToolbar() {
    var _a, _b;
    if (!this.readOnly) {
      return (h(Fragment, null,
        h("vega-input-select", { source: this.languageOptions, value: (_b = (_a = this.value) === null || _a === void 0 ? void 0 : _a.language) !== null && _b !== void 0 ? _b : '', disabled: this.disabled, placeholder: this.translationSlimmer.t('Select Language'), size: "small", vegaDropdownProps: this.languageDropdownProps, exportparts: "select-container, selected-box, placeholder, toggle-icon-container, toggle-icon", ref: this.handleLanguageChange }),
        h("vega-button-circle", { key: "delete-button", disabled: this.disabled, label: this.translationSlimmer.t('Delete Code'), icon: VegaInternalIconManager.getIconKey('trash-can'), iconColor: "text-secondary", variant: "icon-only", size: "extra-small", ref: this.handleDeleteClick })));
    }
  }
}
(() => {
  VegaInternalIconManager.register({
    'trash-can': trashCan,
  });
})();
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "readOnly", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "disabled", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "showCopyButton", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "contentRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "removeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "translationSlimmer", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockRenderer.prototype, "copyButtonRenderer", void 0);
