import { _ as _applyDecoratedDescriptor, a as _initializerDefineProperty, b as _defineProperty } from '../_rollupPluginBabelHelpers-9wjJaosZ.js';
import { setComponentTemplate } from '@ember/component';
import { precompileTemplate } from '@ember/template-compilation';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import didInsert from '@ember/render-modifiers/modifiers/did-insert';
import didUpdate from '@ember/render-modifiers/modifiers/did-update';
import { scheduleOnce } from '@ember/runloop';
import set from 'ember-set-helper/helpers/set';
import { highlight } from 'refractor';
import argOrDefault from '../helpers/arg-or-default.js';
import classNames from '../helpers/class-names.js';
import { checkSupportedLanguage, getHtmlContent } from '../utils/code/utils.js';

var _class, _descriptor, _descriptor2;
let EuiCodeComponent = setComponentTemplate(precompileTemplate(`
    {{#in-element this.codeTarget}}
      {{yield}}
    {{/in-element}}
    <code
      class={{classNames
        "euiCode"
        (if @transparentBackground "euiCode--transparentBackground")
      }}
      data-code-language={{argOrDefault @language "text"}}
      {{didInsert (set this "code")}}
      {{didInsert this.update}}
      {{didUpdate this.update this.language}}
      {{didUpdate this.update this.language}}
      ...attributes
    ></code>
  `, {
  strictMode: true,
  scope: () => ({
    classNames,
    argOrDefault,
    didInsert,
    set,
    didUpdate
  })
}), (_class = class EuiCodeComponent extends Component {
  constructor(owner, args) {
    super(owner, args);
    _initializerDefineProperty(this, "codeTarget", _descriptor, this);
    _initializerDefineProperty(this, "code", _descriptor2, this);
    _defineProperty(this, "observer", null);
    this.codeTarget = document.createElement('div');
    this.setupObserver();
  }
  get language() {
    return checkSupportedLanguage(this.args.language || '');
  }
  setupObserver() {
    this.observer?.disconnect();
    this.observer = new MutationObserver(mutationsList => {
      if (mutationsList.length) this.update();
    });
    if (this.codeTarget) {
      this.update();
      this.observer.observe(this.codeTarget, {
        characterData: true,
        subtree: true,
        childList: true
      });
    }
  }
  update() {
    const render = () => {
      const html = (this.codeTarget?.textContent ? this.codeTarget.textContent : '').trim();
      let data;
      if (typeof html !== 'string') {
        data = [];
      } else {
        data = highlight(html, this.language);
      }
      const code = this.code;
      if (code) {
        code.innerHTML = getHtmlContent(data).element.innerHTML;
      }
    };
    scheduleOnce('afterRender', this, render);
  }
  willDestroy() {
    super.willDestroy();
    this.observer?.disconnect();
  }
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "codeTarget", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "code", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _applyDecoratedDescriptor(_class.prototype, "update", [action], Object.getOwnPropertyDescriptor(_class.prototype, "update"), _class.prototype)), _class));

export { EuiCodeComponent as default };
//# sourceMappingURL=eui-code.js.map
