// unfortunately `_iub` must be available in global scope
// This code comes from the embed section of iubenda.com's dashboard
var _iub = _iub || [];

const initIubenda = () => {
  const hostname = window.location.hostname;

  // this will usually be xata.io
  const rootDomain = hostname.split('.').reverse().splice(0, 2).reverse().join('.');
  const cookieDomain = hostname.includes('vercel.app') ? hostname : rootDomain;

  // Docs: https://www.iubenda.com/en/help/1205-how-to-configure-your-cookie-solution-advanced-guide

  _iub.csConfiguration = {
    askConsentAtCookiePolicyUpdate: true,
    ccpaAcknowledgeOnDisplay: true,
    countryDetection: true,
    enableGdpr: true,
    enableLgpd: true,
    enableCcpa: false,
    enableUspr: false,
    // We don't need uspr https://www.iubenda.com/en/help/65132-us-privacy-legislations-overview
    usprApplies: false,
    ccpaApplies: false,
    ccpaNoticeDisplay: false,
    floatingPreferencesButtonDisplay: false,
    gdprAppliesGlobally: false,
    invalidateConsentWithoutLog: true,
    lang: 'en',
    lgpdAppliesGlobally: false,
    localConsentDomain: cookieDomain,
    perPurposeConsent: true,
    siteId: 2943903,
    usprPurposes: undefined,
    showBannerForUS: false,
    cookiePolicyId: 36368498,
    cookiePolicyUrl: 'https://xata.io/privacy',
    privacyPolicyUrl: 'https://xata.io/privacy',
    privacyPolicyNoticeAtCollectionUrl: 'https://xata.io/privacy',
    banner: {
      acceptButtonDisplay: true,
      backgroundOverlay: false,
      closeButtonDisplay: false,
      customizeButtonDisplay: true,
      explicitWithdrawal: true,
      listPurposes: true,
      position: 'float-bottom-left',
      rejectButtonDisplay: false,
      applyStyles: false,
      acceptButtonCaption: 'Accept all',
      content: 'Xata uses cookies to offer you a better experience. See %{cookie_policy_link} for details.',
      customizeButtonCaption: 'More options',
      rejectButtonCaption: 'Reject'
    },
    callback: {
      onPreferenceNotNeeded: (payload) => {
        window.postMessage({ type: 'onPreferenceNotNeeded', payload }, '*');
      },
      onConsentGiven: (payload) => {
        window.postMessage({ type: 'onConsentGiven', payload }, '*');
      },
      onConsentFirstGiven: (payload) => {
        window.postMessage({ type: 'onConsentFirstGiven', payload }, '*');
      },
      onReady: (payload) => {
        window.postMessage({ type: 'onCookieBannerReady', payload }, '*');
      },
      // Copied from: https://www.iubenda.com/en/help/1235-google-tag-manager-blocking-cookies
      // This tells google tag manager about cookie consent
      onPreferenceExpressedOrNotNeeded: function (preference) {
        dataLayer.push({
          iubenda_ccpa_opted_out: _iub.cs.api.isCcpaOptedOut()
        });
        if (!preference) {
          dataLayer.push({
            event: 'iubenda_preference_not_needed'
          });
        } else {
          if (preference.consent === true) {
            dataLayer.push({
              event: 'iubenda_consent_given'
            });
          } else if (preference.consent === false) {
            dataLayer.push({
              event: 'iubenda_consent_rejected'
            });
          } else if (preference.purposes) {
            for (var purposeId in preference.purposes) {
              if (preference.purposes[purposeId]) {
                dataLayer.push({
                  event: 'iubenda_consent_given_purpose_' + purposeId
                });
              }
            }
          }
        }
      }
    }
  };
}

initIubenda();
