/* eslint-disable */
var StravaCookieBanner = {
  /**
   *   _savedAnswer tells us if athlete has allowed or denied nonessential cookies, or if they have not yet answered.
   *
   *   true means we can load nonessential cookies
   *   false means they answered to deny nonessential cookies
   *   undefined means the athlete has not yet answered and we should show the banner.
   *
   *   @returns {(boolean|undefined)}
   */
  _savedAnswer: function() {
    var answer;
    var loggedIn = "false" === "true";

    if (loggedIn) {
      answer = "";
    } else {
      var name = '_strava_cbv2=';
      var ca = document.cookie.split(';').filter(function(c) { return c.indexOf(name) !== -1; })[0]
      answer = ca && ca.split('=')[1]
    }

    switch (answer) {
      case "true":
      case true:
        return true;
      case "false":
      case false:
        return false;
      case "":
      default:
        return undefined;
    }
  },
  canLoadNonessentialCookies: function() {
    return this._savedAnswer() &&
      typeof window !== 'undefined' &&
      typeof window.loadNonessentialCookies === 'function';
  },
  shouldShowBanner: function(){
    // The cookie policy page shows the 'accept' and 'deny' buttons, so showing the cookie banner would be redundant.
    if (window.location.pathname.indexOf('/legal/cookie_policy') > -1) {
      return false;
    }

    // Show the banner if the athlete has not yet answered to allow or deny cookies.
    return typeof this._savedAnswer() === 'undefined'
  },
  dismiss: function(){
    document.body.removeChild(document.getElementById('stravaCookieBanner'));
  },
  acceptCookies: function() {
    var loggedIn = "false" === "true";

    if (loggedIn) {
      var csrf = "6CZWrw+4olkTJqHntoqoSl72TcF9gFl8ukzKJC3mmtsWxx+LfCHTkRwrj/Ssb9ns/a3+fGlkoOIDGeY76+TiYw==";
      var pattern = new RegExp(/^(www|dev|loc)/);
      var urlPrefix = window.location.host.match(pattern) ? '' : 'https://www.strava.com';
      var url = urlPrefix + '/frontend/athlete/preferences';
      var xhr = new XMLHttpRequest();
      xhr.open('PUT', url);
      xhr.setRequestHeader('x-csrf-token', csrf);
      xhr.setRequestHeader('Content-Type', 'application/json');
      xhr.withCredentials = true;
      xhr.onreadystatechange = function() {
        if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
          // Request finished. Do processing here.
        }
      };
      xhr.send(JSON.stringify({ allow_nonessential_cookies: true }));
    } else {
      var domain;
      if (/localhost/.test(window.location.hostname)) {
        domain = 'localhost';
      } else if (/dev\./.test(window.location.hostname)) {
        domain = '.dev.strava.com';
      } else {
        domain = '.strava.com';
      }

      var cookieExpiration = new Date();
      cookieExpiration.setFullYear(cookieExpiration.getFullYear() + 1);

      document.cookie =
        '_strava_cbv2=true;' +
        'expires=' + cookieExpiration.toGMTString() + ';' +
        'domain=' + domain + ';' +
        'path=/;';
    }

    document.body.removeChild(document.getElementById('stravaCookieBanner'));

    // After saving the preference, trigger the loadNonessentialCookies event handler
    typeof window !== 'undefined' &&
      typeof window.loadNonessentialCookies === 'function' &&
      window.loadNonessentialCookies();
  },
  render: function(){
    if (this.canLoadNonessentialCookies()) {
      window.loadNonessentialCookies();
      return;
    }

    if (this.shouldShowBanner()) {
      var message = "We use cookies to operate our site, help keep you safe, improve your experience, perform analytics, and serve relevant ads. Click on 'I accept' to accept all cookies. You can learn more, as well as change your cookies preferences, by visiting our \u003ca href='https://www.strava.com/legal/cookie_policy'\u003eCookies Policy.\u003c/a\u003e";
      var accept = "I accept";
      var moreOptions = "More options";
      var html =
        '<div class="cookie-banner-content">' +
          '<span>' + message + '</span>' +
          '<div class="cookie-banner-actions">' +
            '<a href="//strava.com/legal/cookie_policy" class="link-learn-more-cookie-banner">' + moreOptions + '</a>' +
            '<button class="btn-accept-cookie-banner">' + accept + '</button>' +
          '</div>' +
        '</div>';

      var bannerCss =
        'background-color: rgba(18,18,20,0.95);' +
        'border-radius: 8px;' +
        'bottom: 20px;' +
        'font-family: "MaisonNeue", Helvetica, Arial, sans-serif;' +
        'left: 20px;' +
        'padding: 20px 40px 20px 20px;' +
        'position: fixed;' +
        'z-index: 11000;' +
        'box-shadow: 0 12px 24px -12px rgba(18,18,20,0.12);' +
        'max-width: 385px;' +
        '-webkit-font-smoothing: antialiased;';
      var contentCss =
        'color: #cbcbd6;' +
        'font-size: 14px;' +
        'font-weight: 500;' +
        'line-height: 1.5em;' +
        'margin-left: auto;' +
        'margin-right: auto;' +
        'max-width: 1216px;' +
        'justify-content: center;';
      var linkCss =
        'color: #fff;' +
        'font-weight: 700;' +
        'text-decoration: none;';
      var actionsCss =
        'margin-top: 12px;' +
        'display: flex;' +
        'justify-content: space-between;';
      var acceptButtonCss =
        'background: #fff;' +
        'border: 1px solid #fff;' +
        'border-radius: 4px;' +
        'color: #333;' +
        'flex-grow: 1;' +
        'padding: 6px 18px;' +
        'font-family: Helvetica, Arial, sans-serif;';
      var learnMore =
        'border: 1px solid #fff;' +
        'border-radius: 4px;' +
        'color: #fff;' +
        'cursor: pointer;' +
        'display: inline-block;' +
        'flex-grow: 1;' +
        'font-family: Helvetica, Arial, sans-serif;' +
        'margin-right: 12px;' +
        'padding: 6px 18px;' +
        'text-align: center;';

      var cookieBanner = document.createElement('div');
      cookieBanner.setAttribute('id', 'stravaCookieBanner');
      cookieBanner.innerHTML = html;
      cookieBanner.setAttribute('style', bannerCss);
      cookieBanner.getElementsByClassName('cookie-banner-content')[0].setAttribute('style', contentCss);
      cookieBanner.getElementsByTagName('a')[0].setAttribute('style', linkCss);

      var actions = cookieBanner.getElementsByClassName('cookie-banner-actions')[0];
      actions.setAttribute('style', actionsCss);

      var learnMoreLink = cookieBanner.getElementsByClassName('link-learn-more-cookie-banner')[0];
      learnMoreLink.setAttribute('style', learnMore);
      document.body.insertBefore(cookieBanner, document.body.childNodes[0]);
      learnMoreLink.addEventListener('click', this.dismiss);

      var acceptButton = cookieBanner.getElementsByClassName('btn-accept-cookie-banner')[0];
      acceptButton.setAttribute('style', acceptButtonCss);
      document.body.insertBefore(cookieBanner, document.body.childNodes[0]);
      acceptButton.addEventListener('click', this.acceptCookies);
    }
  }
}

