// const $ = require('jquery');
const ihk = require('../ihk');
const Cookies = require('js-cookie');

ihk.CookieConsent = function(banner) {
  const t = this;

  banner.addClass('initiated');

  if (Cookies.get('ihk_cookieconsent', {domain: window.location.host})) {
    ihk.cookies = JSON.parse(Cookies.get('ihk_cookieconsent'));
  }
  else {
    ihk.cookies = {
      okclicked: false
    };
  }

  if (!ihk.cookies.okclicked) {
    setTimeout(function () {
      banner.addClass('show');
    }, 500)
  }

  banner.find('.btn').on('click', function (e) {
    e.preventDefault();

    ihk.cookies.okclicked = true;

    Cookies.set('ihk_cookieconsent', JSON.stringify(ihk.cookies), {domain: window.location.host, expires: 365});
    banner.removeClass('show');
  })
}

global.$('body').on('ihk-init dynamic-component-loaded', function () {
  $('.cookieconsent:not(.initiated)').each(function(i) {
    new ihk.CookieConsent($(this));
  });
})
