<header class="md-header" data-md-component="header">
  <nav class="md-header-nav md-grid" aria-label="{{ lang.t('header.title') }}">
    <a href="{{ config.site_url | default(nav.homepage.url, true) | url }}" title="{{ config.site_name }}" class="md-header-nav__button md-logo" aria-label="{{ config.site_name }}">
      {% include "partials/logo.html" %}
    </a>
    <label class="md-header-nav__button md-icon" for="__drawer">
      {% include ".icons/material/menu" ~ ".svg" %}
    </label>
    <div class="md-header-nav__title" data-md-component="header-title">
      {% if config.site_name == page.title %}
        <div class="md-header-nav__ellipsis md-ellipsis">
          {{ config.site_name }}
        </div>
      {% else %}
        <div class="md-header-nav__ellipsis">
          <span class="md-header-nav__topic md-ellipsis">
            {{ config.site_name }}
          </span>
          <span class="md-header-nav__topic md-ellipsis">
            {% if page and page.meta and page.meta.title %}
              {{ page.meta.title }}
            {% else %}
              {{ page.title }}
            {% endif %}
          </span>
        </div>
      {% endif %}
    </div>
    {% if "search" in config["plugins"] %}
      <label class="md-header-nav__button md-icon" for="__search">
        {% include ".icons/material/magnify.svg" %}
      </label>
      {% include "partials/search.html" %}
    {% endif %}
    {% if config.repo_url %}
      <div class="md-header-nav__source">
        {% include "partials/source.html" %}
      </div>
    {% endif %}
    <button id="dark-mode-button"></button>
    <script>
      function detectColorScheme(){
          var theme="default";
          document.getElementById("dark-mode-button").style.backgroundImage = "url('https://raw.githubusercontent.com/weaveworks/eksctl/abc01e84cbedb821672e06fc5e27230d309b545c/userdocs/src/img/moon-dark.svg')"

          if(localStorage.getItem("theme")){
              if(localStorage.getItem("theme") == "slate"){
                  var theme = "slate";
              }
          }
          else if(window.matchMedia("(prefers-color-scheme: dark)").matches) {
              var theme = "slate";
          }
          if (theme=="slate") {
              document.body.setAttribute("data-md-color-scheme", "slate");
              document.getElementById("dark-mode-button").style.backgroundImage = "url('https://raw.githubusercontent.com/weaveworks/eksctl/abc01e84cbedb821672e06fc5e27230d309b545c/userdocs/src/img/sun-dark.svg')"
          }
      }
      detectColorScheme();

      const toggleSwitch = document.querySelector("#dark-mode-button");
      function switchTheme() {
          if (document.getElementById("dark-mode-button").style.backgroundImage == 'url("https://raw.githubusercontent.com/weaveworks/eksctl/abc01e84cbedb821672e06fc5e27230d309b545c/userdocs/src/img/moon-dark.svg")') {
            console.log('switch to dark', document.getElementById("dark-mode-button").style.backgroundImage)

              localStorage.setItem('theme', 'slate');
              document.body.setAttribute('data-md-color-scheme', 'slate');
              document.getElementById("dark-mode-button").style.backgroundImage = "url('https://raw.githubusercontent.com/weaveworks/eksctl/abc01e84cbedb821672e06fc5e27230d309b545c/userdocs/src/img/sun-dark.svg')"
          } else {
            console.log('switch to light', document.getElementById("dark-mode-button").style.backgroundImage)
              localStorage.setItem('theme', 'default');
              document.body.setAttribute('data-md-color-scheme', 'default');
              document.getElementById("dark-mode-button").style.backgroundImage = "url('https://raw.githubusercontent.com/weaveworks/eksctl/abc01e84cbedb821672e06fc5e27230d309b545c/userdocs/src/img/moon-dark.svg')"
          }
      }
      toggleSwitch.addEventListener('click', switchTheme);
    </script>
  </nav>
</header>
