import {CookiebotUtils} from "../utils/CookiebotUtils";


class DynamicVideoContent extends HTMLElement {
  constructor() {
    super();
  };

  connectedCallback(){

    this.addEventListener("click", this.loadVideoFragment )
  }

  loadVideoFragment(event){
    event.preventDefault();
    this.removeEventListener("click", this.loadVideoFragment);
    const elem = this;
    const ajaxUrl = $(this).data('call-url');
    $.ajax({
      url : ajaxUrl,
      success : function(result) {
        $(elem).html(result);
        $(window).trigger('resize');
        // Das Event muss lieder so getrterigered werden.
        // So funktioniert es  wenn man den event in anderen Webpack-Modul erwartet
        // Event in timeout triggern weil das rendern von Ergebnissen länger dauern kann
        requestAnimationFrame(()=>{
          global.$('body').trigger('dynamic-component-loaded');
          CookiebotUtils.runScripts();
        });

      }
    });
  }

}


window.customElements.define('dynamic-video-content',DynamicVideoContent);
