import { createButton } from './buttons_utils';

function _assertClassBrand(e, t, n) {
  if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
  throw new TypeError("Private element is not present on this object");
}
function _checkPrivateRedeclaration(e, t) {
  if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
}
function _classPrivateFieldGet2(s, a) {
  return s.get(_assertClassBrand(s, a));
}
function _classPrivateFieldInitSpec(e, t, a) {
  _checkPrivateRedeclaration(e, t), t.set(e, a);
}
function _classPrivateFieldSet2(s, a, r) {
  return s.set(_assertClassBrand(s, a), r), r;
}

const CODE_MARKDOWN_CLASS = 'js-markdown-code';
var _actionButton = /*#__PURE__*/new WeakMap();
var _codeBlock = /*#__PURE__*/new WeakMap();
var _handleClick = /*#__PURE__*/new WeakMap();
class InsertCodeSnippetElement extends HTMLElement {
  constructor(codeBlock) {
    super();
    _classPrivateFieldInitSpec(this, _actionButton, void 0);
    _classPrivateFieldInitSpec(this, _codeBlock, void 0);
    _classPrivateFieldInitSpec(this, _handleClick, () => {
      if (_classPrivateFieldGet2(_codeBlock, this)) {
        _classPrivateFieldGet2(_codeBlock, this).dispatchEvent(new CustomEvent('insert-code-snippet', {
          bubbles: true,
          cancelable: true,
          detail: {
            code: _classPrivateFieldGet2(_codeBlock, this).textContent.trim()
          }
        }));
      }
    });
    _classPrivateFieldSet2(_actionButton, this, createButton());

    // we handle two possible cases here:
    // 1. we use constructor parameter if the element is created in Javscript and inserted in the document
    // 2. we find the wrapping element containing code if the element is received from the server
    _classPrivateFieldSet2(_codeBlock, this, codeBlock !== null && codeBlock !== void 0 ? codeBlock : this.closest(`.${CODE_MARKDOWN_CLASS}`));
  }
  connectedCallback() {
    this.appendChild(_classPrivateFieldGet2(_actionButton, this));
    _classPrivateFieldGet2(_actionButton, this).addEventListener('click', _classPrivateFieldGet2(_handleClick, this));
  }
  disconnectedCallback() {
    _classPrivateFieldGet2(_actionButton, this).removeEventListener('click', _classPrivateFieldGet2(_handleClick, this));
  }
}

export { InsertCodeSnippetElement };
