<script src="/css/highlight/highlight.min.js"></script>
<script>
  document.addEventListener('DOMContentLoaded', () => {
    document.querySelectorAll('pre code').forEach(el => { el.textContent = el.textContent.trimEnd(); });
    hljs.highlightAll();
    var kbd = document.querySelector('.search-button__shortcut');
    if (kbd) kbd.textContent = navigator.platform.indexOf('Mac') > -1 ? '⌘K' : 'Ctrl K';
  });
</script>
<script>
  (function() {
    var themeKey = window.__doculaThemeKey;
    function getMode() {
      return localStorage.getItem(themeKey) || {{#if themeMode}}'{{themeMode}}'{{else}}'system'{{/if}};
    }

    function resolveTheme(mode) {
      if (mode === 'system') {
        return window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
      }
      return mode;
    }

    var cachedEls = null;
    function getThemeEls() {
      if (!cachedEls) {
        cachedEls = {
          systemIcon: document.querySelector('.theme-button__icon--system'),
          sunIcon: document.querySelector('.theme-button__icon--sun'),
          moonIcon: document.querySelector('.theme-button__icon--moon'),
          toggle: document.getElementById('theme-toggle')
        };
      }
      return cachedEls;
    }

    function applyTheme(mode) {
      var resolved = resolveTheme(mode);
      if (resolved === 'light') {
        document.documentElement.setAttribute('data-theme', 'light');
      } else {
        document.documentElement.removeAttribute('data-theme');
      }

      var els = getThemeEls();
      if (els.systemIcon && els.sunIcon && els.moonIcon) {
        els.systemIcon.classList.add('theme-button__icon--hidden');
        els.sunIcon.classList.add('theme-button__icon--hidden');
        els.moonIcon.classList.add('theme-button__icon--hidden');
        if (mode === 'system') {
          els.systemIcon.classList.remove('theme-button__icon--hidden');
        } else if (mode === 'light') {
          els.sunIcon.classList.remove('theme-button__icon--hidden');
        } else {
          els.moonIcon.classList.remove('theme-button__icon--hidden');
        }
      }

      if (els.toggle) {
        var labels = { system: 'Theme: system (following system)', light: 'Theme: light', dark: 'Theme: dark' };
        els.toggle.setAttribute('aria-label', labels[mode] || 'Toggle theme');
      }
    }

    function nextMode(current) {
      var cycle = { system: 'light', light: 'dark', dark: 'system' };
      return cycle[current] || 'system';
    }

    var currentMode = getMode();

    document.addEventListener('DOMContentLoaded', function() {
      applyTheme(currentMode);

      var els = getThemeEls();
      if (els.toggle) {
        els.toggle.addEventListener('click', function() {
          currentMode = nextMode(currentMode);
          localStorage.setItem(themeKey, currentMode);
          applyTheme(currentMode);
        });
      }
    });

    window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', function() {
      if (getMode() === 'system') {
        applyTheme('system');
      }
    });
  })();
</script>
{{#if cookieAuth}}
<div id="cookie-auth-config" hidden data-cookie-name="{{#if cookieAuth.cookieName}}{{cookieAuth.cookieName}}{{else}}token{{/if}}"{{#if cookieAuth.logoutUrl}} data-logout-url="{{cookieAuth.logoutUrl}}"{{/if}}></div>
<script>
  (function() {
    var configEl = document.getElementById('cookie-auth-config');
    if (!configEl) return;
    var cookieName = configEl.getAttribute('data-cookie-name');
    var logoutUrl = configEl.getAttribute('data-logout-url');
    function isSafeUrl(url) {
      if (!url) return false;
      try {
        var parsed = new URL(url, window.location.origin);
        return parsed.origin === window.location.origin;
      } catch (e) {
        return false;
      }
    }
    function getCookieValue() {
      var match = document.cookie.split(';').find(function(c) {
        return c.trim().startsWith(cookieName + '=');
      });
      return match ? match.trim().substring(cookieName.length + 1) : null;
    }
    function getDisplayName(token) {
      try {
        var payload = token.split('.')[1];
        if (!payload) return null;
        var json = atob(payload.replace(/-/g, '+').replace(/_/g, '/'));
        var claims = JSON.parse(json);
        return claims.name || claims.preferred_username || claims.email || null;
      } catch (e) {
        return null;
      }
    }
    function updateAuthUI() {
      var token = getCookieValue();
      var loggedIn = !!token;
      var displayName = loggedIn ? getDisplayName(token) : null;
      var els = [
        { login: document.getElementById('cookie-auth-login'), logout: document.getElementById('cookie-auth-logout'), user: document.getElementById('cookie-auth-user') },
        { login: document.getElementById('cookie-auth-login-mobile'), logout: document.getElementById('cookie-auth-logout-mobile'), user: document.getElementById('cookie-auth-user-mobile') }
      ];
      els.forEach(function(pair) {
        if (pair.login) pair.login.style.display = loggedIn ? 'none' : '';
        if (pair.logout) pair.logout.style.display = loggedIn ? '' : 'none';
        if (pair.user) {
          pair.user.textContent = displayName || '';
          pair.user.style.display = (loggedIn && displayName) ? '' : 'none';
        }
      });
    }
    document.addEventListener('DOMContentLoaded', function() {
      updateAuthUI();
      var logoutEls = [
        document.getElementById('cookie-auth-logout'),
        document.getElementById('cookie-auth-logout-mobile')
      ];
      logoutEls.forEach(function(el) {
        if (el) {
          el.addEventListener('click', function() {
            if (isSafeUrl(logoutUrl)) {
              window.location.href = logoutUrl;
            } else {
              document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
              window.location.reload();
            }
          });
        }
      });
    });
  })();
</script>
{{/if}}
<script defer>
  document.addEventListener('DOMContentLoaded', () => {
    const menuToggle = document.getElementById('mobile-menu-toggle');
    const mobileSidebar = document.getElementById('mobile-sidebar');
    const backdrop = document.getElementById('sidebar-backdrop');
    if (menuToggle && mobileSidebar) {
      menuToggle.addEventListener('click', () => {
        mobileSidebar.classList.toggle('mobile-sidebar--open');
        if (backdrop) backdrop.classList.toggle('sidebar-backdrop--visible');
      });
      if (backdrop) {
        backdrop.addEventListener('click', () => {
          mobileSidebar.classList.remove('mobile-sidebar--open');
          backdrop.classList.remove('sidebar-backdrop--visible');
        });
      }
    }

    // Page dropdown toggle (docs pages)
    const dropdownToggle = document.getElementById('page-dropdown-toggle');
    const dropdownList = document.getElementById('page-dropdown-list');
    if (dropdownToggle && dropdownList) {
      dropdownToggle.addEventListener('click', () => {
        dropdownList.classList.toggle('page-dropdown__list--open');
      });
    }

    // Active sidebar link highlighting
    const sidebarLinks = document.querySelectorAll('.nav-sidebar__item');
    const currentPath = window.location.pathname;
    sidebarLinks.forEach((link) => {
      if (link.getAttribute('href') === currentPath || link.getAttribute('href') === currentPath.replace(/\/$/, '')) {
        link.classList.add('nav-sidebar__item--active');
      }
    });

    // Active header nav link highlighting
    const navLinks = document.querySelectorAll('.header-bottom__item');
    navLinks.forEach((link) => {
      const href = link.getAttribute('href');
      if (currentPath.startsWith(href)) {
        link.classList.add('header-bottom__item--active');
      }
    });

    // Active mobile nav link highlighting
    const mobileNavLinks = document.querySelectorAll('.mobile-nav__item');
    mobileNavLinks.forEach((link) => {
      const href = link.getAttribute('href');
      if (currentPath.startsWith(href)) {
        link.classList.add('mobile-nav__item--active');
      }
    });

    // TOC extraction: move the inline TOC into the right sidebar and hide the original
    const tocHeading = document.getElementById('table-of-contents');
    if (tocHeading) {
      const tocList = tocHeading.nextElementSibling;
      const tocTarget = document.getElementById('toc-list');
      if (tocList && tocList.tagName === 'UL' && tocTarget) {
        tocTarget.innerHTML = tocList.innerHTML;
        tocHeading.style.display = 'none';
        tocList.style.display = 'none';
      }
    }

    // Hide the TOC aside if it has no entries
    const tocSidebar = document.getElementById('toc-list');
    if (tocSidebar && tocSidebar.children.length === 0) {
      const aside = tocSidebar.closest('.content-aside');
      if (aside) { aside.style.display = 'none'; }
    }
  });
</script>
