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 { h } from '@stencil/core';
import { VegaInternalIconManager } from '../../../../helpers/icon/internal-icon-manager';
import copy from '../../assets/copy';
import { VegaClick } from '../../../../helpers/event-manager/event-id/vega-event-id';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
export class VegaCodeBlockCopyButtonRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Copy the code.
     *
     * @param {HTMLVegaButtonCircleElement} node - The copy button.
     */
    this.handleCopyClick = (node) => {
      DomNodeSubjectObserverFactory.addUniqueObserverToNode(node, VegaClick, async () => {
        var _a;
        const content = ((_a = this.value) === null || _a === void 0 ? void 0 : _a.code) || '';
        if (content && globalThis.navigator && globalThis.navigator.clipboard) {
          clearTimeout(this.updateLabelTimer);
          await globalThis.navigator.clipboard.writeText(content);
          const oldTooltip = node.label;
          node.label = this.translationSlimmer.t('Copied');
          this.updateLabelTimer = setTimeout(() => {
            if (node) {
              node.label = oldTooltip;
            }
          }, VegaCodeBlockCopyButtonRenderer.RESET_TOOLTIP_TIME);
        }
      });
    };
  }
  /**
   * Render a copy button.
   *
   * @returns {Nullable<HTMLElement>} The button element.
   */
  render() {
    if (this.showCopyButton && this.readOnly) {
      return (h("vega-button-circle", { key: "copy-button", label: this.translationSlimmer.t('Copy Code'), class: "copy-code-button", icon: VegaInternalIconManager.getIconKey('copy'), iconColor: "text-secondary", variant: "icon-only", size: "extra-small", ref: this.handleCopyClick }));
    }
  }
}
(() => {
  VegaInternalIconManager.register({
    copy: copy,
  });
})();
VegaCodeBlockCopyButtonRenderer.RESET_TOOLTIP_TIME = 1000;
__decorate([
  MapToComponentField()
], VegaCodeBlockCopyButtonRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockCopyButtonRenderer.prototype, "readOnly", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockCopyButtonRenderer.prototype, "showCopyButton", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockCopyButtonRenderer.prototype, "translationSlimmer", void 0);
