{% styles default %}
{% styles colors %}
{% styles custom %}

<script type="text/javascript" async src="https://play.vidyard.com/embed/v4.js"></script>

<!-- fontawesome inclusion -->
<script src="https://kit.fontawesome.com/41a3aee3ad.js" crossorigin="anonymous"></script>

<!-- clamping text to given number of lines -->
<script src="https://s3.amazonaws.com/static.northpass.com/JS+scripts/clamp.js" type="module"></script>

<!-- JavaScript Bundle with Popper -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

{% include "cookie_banner" %}

<!-- language scripts -->
<script>
  // changing language
  function changeLanguage(newLanguage, sendRequest=true) {
    document.querySelector(".languages-dropdown").classList.remove('show');
    if (!window.current_language || window.current_language !== newLanguage) {
      window.current_language = newLanguage
    } else {
      console.log('language already selected')
      return null
    }

    document.querySelectorAll('.current-lang').forEach(activeElement => {
      activeElement.classList.remove('current-lang');
    });
    document.querySelectorAll(`[data-lang="${newLanguage}"]`).forEach(newLanguageElement => {
      newLanguageElement.classList.add('current-lang');
    });
    {% if current_person.signed_in? and current_school.properties.sandbox == false %}
      if (sendRequest) {
        let xhr = new XMLHttpRequest();
        {% if current_school.sso_active? %}
          var url = "https://www.workato.com/webhooks/rest/bd1a1eb7-7e79-4208-a1db-8e9c7440bcc9/pipedrive-sso-change-user-language";
        {% else %}
          var url = "https://www.workato.com/webhooks/rest/bd1a1eb7-7e79-4208-a1db-8e9c7440bcc9/pipedrive-open-change-user-language";
        {% endif %}
        xhr.open("POST", url, true);
        xhr.send(JSON.stringify({
          user_id: '{{ current_person.id }}',
          language: newLanguage
        }));
      }
    {% endif %}

    window.localStorage.setItem('academy-language', newLanguage);
    document.querySelector('body').className = `lang-${newLanguage}`;

  }

  // setup body class based on property or cookies
  function setupInitialLanguage() {
    let userStorageLanguage = window.localStorage.getItem('academy-language');
    {% if current_person.signed_in? and current_person.properties.user_language contains 'missing property' %}
      {% assign property_missing = true %}
    {% else %}
      {% assign property_missing = false %}
    {% endif %}
    {% if current_person.signed_in? and current_person.properties.user_language != 'NULL' and property_missing == false %}
      var selectedLanguage = "{{ current_person.properties.user_language }}";
    {% else %}
      var selectedLanguage = userStorageLanguage || "en";
    {% endif %}
    if (['en', 'de', 'es', 'fr', 'br'].includes(selectedLanguage)) {
      changeLanguage(selectedLanguage, false);
    } else {
      changeLanguage('en', false);
      console.log('wrong language initialized', selectedLanguage);
      window.current_language = 'en';
    }
  

    const pathParts = window.location.pathname.split('/').slice(1); 

    if (
      (pathParts.length === 1 && pathParts[0] === 'app') || 
      (pathParts.length === 2 && (pathParts[0] === 'app' && (pathParts[1] === 'video-tutorials' || pathParts[1] === 'catalog'))) 
    ) {
      selectedLanguage = 'en';
    } else if (
      window.location.pathname.includes('/de-homepage') ||
      window.location.pathname.includes('/de-video-tutorials') ||
      window.location.pathname.includes('/de-catalog')
    ) {
      selectedLanguage = 'de';
    } else if (
      window.location.pathname.includes('/es-homepage') ||
      window.location.pathname.includes('/es-video-tutorials') ||
      window.location.pathname.includes('/es-catalog')
    ) {
      selectedLanguage = 'es';
    } else if (
      window.location.pathname.includes('/fr-homepage') ||
      window.location.pathname.includes('/fr-video-tutorials') ||
      window.location.pathname.includes('/fr-catalog')
    ) {
      selectedLanguage = 'fr';
    } else if (
      window.location.pathname.includes('/br-homepage') ||
      window.location.pathname.includes('/br-video-tutorials') ||
      window.location.pathname.includes('/br-catalog')
    ) {
      selectedLanguage = 'br';
    } else {
      selectedLanguage =  'en';
    }


    changeLanguage(selectedLanguage, false);
    
  }
  
  addEventListener('DOMContentLoaded', () => {
    setupInitialLanguage();
  });

  function redirectToLanguageHomepage(language) {
    let homepageUrl = '';
    
    switch (window.current_language) {
      case 'de':
        homepageUrl = '/app/de-homepage';
        break;
      case 'es':
        homepageUrl = '/app/es-homepage';
        break;
      case 'fr':
        homepageUrl = '/app/fr-homepage';
        break;
      case 'br':
        homepageUrl = '/app/br-homepage';
        break;
      default:
        homepageUrl = '/app';
        break;
    }
    
    window.location.href = homepageUrl;
  }

  document.addEventListener('DOMContentLoaded', function() {
    const academyLanguage = window.localStorage.getItem('academy-language');
    const url = window.location.href;
    const lastSegment = url.substring(url.lastIndexOf('/') + 1);
    console.log(academyLanguage);
    function redirectToRightPage() {
      let catalogUrl = '';
      let videoUrl = '';
      let homepageUrl = '';
  
      if (window.location.href.includes('/app/catalog')) {
        switch (academyLanguage) {
          case 'de':
            catalogUrl = '/app/de-catalog';
            break;
          case 'es':
            catalogUrl = '/app/es-catalog';
            break;
          case 'fr':
            catalogUrl = '/app/fr-catalog';
            break;
          case 'br':
            catalogUrl = '/app/br-catalog';
            break;
          case 'en':
            catalogUrl = '/app/catalog';
            return;
        }
        window.location.href = catalogUrl; 
      } 
      else if (window.location.href.includes('/app/video-tutorials')) {
        switch (academyLanguage) {
          case 'de':
            videoUrl = '/app/de-video-tutorials';
            break;
          case 'es':
            videoUrl = '/app/es-video-tutorials';
            break;
          case 'fr':
            videoUrl = '/app/fr-video-tutorials';
            break;
          case 'br':
            videoUrl = '/app/br-video-tutorials';
            break;
          case 'en':
            videoUrl = '/app/catalog';
            return;
        }
        window.location.href = videoUrl;
      }
      else if (lastSegment === 'app') {
        switch (academyLanguage) {
          case 'de':
            homepageUrl = '/app/de-homepage';
            break;
          case 'es':
            homepageUrl = '/app/es-homepage';
            break;
          case 'fr':
            homepageUrl = '/app/fr-homepage';
            break;
          case 'br':
            homepageUrl = '/app/br-homepage';
            break;
          case 'en':
            homepageUrl = '/app';
            return;
        }
        window.location.href = homepageUrl;
      }
  
  
    }
  
    redirectToRightPage();
  });

  const academyLanguage = window.localStorage.getItem('academy-language');
  console.log(academyLanguage);
</script>

<!--Function to display proper courses based on language value-->
<script>
  function filterCoursesByLanguage() {
    var selectedLanguage = localStorage.getItem('academy-language');
    var courses = document.querySelectorAll('.homepage-card-wrapper, .course-card');
  
    if (!courses.length) {
      return; // Nie ma żadnych kursów na stronie
    }
  
    for (var i = 0; i < courses.length; i++) {
      var course = courses[i];
      var courseLanguage = course.getAttribute('language');
  
      if (selectedLanguage && courseLanguage !== selectedLanguage) {
        course.style.display = 'none'; // Ukryj kursy o nieodpowiednim języku
      } else {
        course.style.display = 'block'; // Pokaż kursy o wybranym języku
      }
    }
  }
  
 // Wywołanie funkcji po załadowaniu strony
window.addEventListener('DOMContentLoaded', filterCoursesByLanguage);

// Wywołanie funkcji po zmianie wartości w local storage
window.addEventListener('storage', filterCoursesByLanguage);

  
</script>

<!-- on SSO school redirect new users to setup their name -->
{% if current_person.signed_in? and current_school.sso_active? %}
  {% unless current_person.first_name %}
    <script>
      if ("{{ current_person.email }}".includes('+preview-') === false &&
        window.location.pathname !== "/app/profile-settings" &&
        !window.sessionStorage.getItem('nameSetup'))
      {
        window.location.replace('/app/profile-settings')
      }
      
    </script>
  {% endunless %}
{% endif %}

<!-- clear last video filter when leaving video tutorials pages -->
<script>
  if (window.location.pathname !== '/app/video-tutorials' &&
    !window.location.pathname.includes('/app/courses/'))
  {
    window.sessionStorage.removeItem('last-selected-video-filter')
  }
</script>

{% include 'seo_logo' %}

{% if current_school.sso_active? and current_person.signed_in? == false %}
  {% include 'sso_login_widget' %}
{% endif %}

{% if current_school.sso_active? %}
  {% if current_person.signed_in? %}
    <script>
      if (window.location.pathname === '/app' && window.sessionStorage.sso_last_page) {
        let new_url = window.sessionStorage.sso_last_page;
        window.sessionStorage.sso_last_page = ''
        window.location.replace(new_url);
      }
    </script>
  {% else %}
    <script>
      window.sessionStorage.sso_last_page = window.location.pathname;
    </script>
  {% endif %}
{% endif %}
