(function() {
var EmbedHelpers = {
  addElementToDom: function(content) {
    var element;

    if (typeof content === 'string') {
      element = document.createElement('article');
      element.innerHTML = content;
    } else {
      element = content;
    }

    var scriptRegEx = /script\/production\/105949/;
    var scripts = document.scripts;

    for (var i = 0; i < scripts.length; i++) {
      var script = scripts[i];
      if (scriptRegEx.test(script.src)) {
        var parent = script.parentNode;
        parent.insertBefore(element, script);
      }
    }
  },

  addLinkedDocument: function(fileName, position, type) {
    if (!window.mychannels) window.mychannels = {};
    if (!window.mychannels.registry) window.mychannels.registry = [];
    if (~window.mychannels.registry.indexOf(fileName)) return;

    var element = document.createElement(type);

    if (type === 'link') {
      element.type = 'text/css';
      element.rel = 'stylesheet';
      element.href = fileName;
    }

    if (type === 'script') {
      element.src = fileName;
      element.type = 'application/javascript';
    }

    switch (position) {
      case 'head':
        document.head.appendChild(element);
        break;
      case 'body':
        document.body.appendChild(element);
        break;
    }
    window.mychannels.registry.push(fileName);
  },

  attachIframeResizeHandler: function(className) {
    var resize = function() {
      if (window.iFrameResize) {
        window.iFrameResize({ checkOrigin: false }, '.' + className);
        clearInterval(window.mychannels.resizeInterval);
      }
    };
    if (!window.mychannels.resizeInterval) window.mychannels.resizeInterval = setInterval(resize, 50);
  },

  createIframe: function(src, className, styles) {
    var iframe = document.createElement('iframe');

    iframe.className = className;
    iframe.setAttribute('allowfullscreen', '');
    iframe.setAttribute('style', styles);
    iframe.setAttribute('src', src);

    return iframe;
  },

  getCookie: function(name) {
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2) return parts.pop().split(";").shift();
  },

  getQueryParams: function(callback) {
    var params = '';

    var gdprApplies = localStorage.getItem('mychannels-gdprApplies');
    var consentData = localStorage.getItem('mychannels-consentData');

    var ksgBe = localStorage.getItem('kxmedialaan_allsegs');
    var ksgNl = localStorage.getItem('kxdepersgroep_allsegs');

    var kuidBe = localStorage.getItem('kxmedialaan_kuid');
    var kuidNl = localStorage.getItem('kxdepersgroep_kuid');

    const cxsg = localStorage.getItem('cxSegments');
    const cxid = localStorage.getItem('_cX_G');

    var pws = EmbedHelpers.getCookie('pws');

    gdprApplies && (params += '&gdprApplies=' + encodeURIComponent(gdprApplies));
    consentData && (params += '&consentData=' + encodeURIComponent(consentData));

    ksgBe && (params += '&ksgBe=' + encodeURIComponent(ksgBe));
    ksgNl && (params += '&ksgNl=' + encodeURIComponent(ksgNl));
    kuidBe && (params += '&kuidBe=' + encodeURIComponent(kuidBe));
    kuidNl && (params += '&kuidNl=' + encodeURIComponent(kuidNl));
    cxsg && (params += '&cxsg=' + encodeURIComponent(cxsg));
    cxid && (params += '&cxid=' + encodeURIComponent(cxid));

    pws && (params += '&pws=' + encodeURIComponent(pws));

    // Try TCF 2.0
    // eslint-disable-next-line no-underscore-dangle
    if (!window._privacy) window._privacy = [];
    // eslint-disable-next-line no-underscore-dangle
    window._privacy.push([
      'targetedadvertising',
      function setTargetedAdvertisingFlagLocalStorage() {
        // eslint-disable-next-line no-underscore-dangle
        window._privacy && window._privacy.tcfApi &&
          // eslint-disable-next-line no-underscore-dangle
          window._privacy.tcfApi('getTCData', 2, tcData => {
            localStorage.setItem('mychannels-gdprApplies', true);
            localStorage.setItem('mychannels-consentData', tcData.tcString);
          });
      },
    ]);

    // Fallback non TCF 2.0
    // eslint-disable-next-line no-underscore-dangle
    window.__cmp &&
      // eslint-disable-next-line no-underscore-dangle
      window.__cmp('getConsentData', [], cmpData => {
        localStorage.setItem('mychannels-gdprApplies', cmpData.gdprApplies);
        localStorage.setItem('mychannels-consentData', cmpData.consentData);
      });

    callback(params);
  }
};

  var callback = function(queryParams) {
    var embed_uri = 'https://embed.mychannels.video/production/105949?options=TUFF_default';
    var src = queryParams ? embed_uri + queryParams : embed_uri;
    var className = 'mc-embed';
    var styles = 'border: 0; position: absolute; left:0; top: 0; overflow:hidden; height: 100%; width: 100%;';

    var iframe = EmbedHelpers.createIframe(src, className, styles);
    var article = document.createElement('article');

    article.setAttribute('style', 'padding-top: 56.25%; position: relative; height: 0; width: 100%;');
    article.appendChild(iframe);

    EmbedHelpers.addElementToDom(article);
  };

  EmbedHelpers.getQueryParams(callback); 
  
})();