---
import MobileMenuToggle from './MobileMenuToggle.astro'

const routeHasSidebar = Astro.locals.starlightRoute.hasSidebar
const isApiPage = Astro.url.pathname.includes('/tools/api')
const hasSidebar = routeHasSidebar && !isApiPage
---

<div class={`page sl-flex ${!hasSidebar ? 'no-sidebar' : ''}`}>
  <div class="header-wrapper"><slot name="header" /></div>
  <div class="grid-template">
    {
      hasSidebar && (
        <nav
          class="sidebar"
          aria-label={Astro.locals.t('sidebarNav.accessibleLabel' as any)}
        >
          <MobileMenuToggle {...Astro.props} />
          <div id="starlight__sidebar" class="sidebar-pane">
            <div class="sidebar-content sl-flex">
              <slot name="sidebar" />
            </div>
          </div>
        </nav>
      )
    }
    <div class="main-frame"><slot /></div>
  </div>
</div>

<script is:inline>
  ;(() => {
    try {
      if (!matchMedia('(min-width: 50em)').matches) return
      const scroller = document.getElementById('starlight__sidebar')

      const state = JSON.parse(
        sessionStorage.getItem('sl-sidebar-state') || '0'
      )
      if (!scroller || !state) return

      scroller.scrollTop = state.scroll
    } catch {}
  })()
</script>

<style>
  .grid-template {
    position: relative;
    display: grid;
    grid-template-columns: 1fr 4fr;
    column-gap: 48px;
    @media (max-width: 1024px) {
      grid-template-columns: 100%;
    }
  }

  :global(.no-sidebar) .grid-template {
    grid-template-columns: 100%;
  }

  .header-wrapper {
    position: sticky;
    z-index: 4;
    inset-inline-start: 0;
    inset-block-start: 0;
    width: 100%;
    background-color: var(--bg-color);
  }

  :global([data-has-sidebar]) .header {
    padding-inline-end: var(--sl-nav-pad-x);
    @media (max-width: 1024px) {
      padding-inline-end: calc(
        var(--sl-nav-gap) + var(--sl-nav-pad-x) + var(--sl-menu-button-size)
      );
    }
  }

  .sidebar-pane {
    --sl-sidebar-visibility: visible;
    visibility: var(--sl-sidebar-visibility, hidden);
    position: sticky;
    max-height: 100vh;
    z-index: 3;
    inset-block: 0;
    width: 100%;
    background-color: var(--bg-color);
    overflow-y: auto;
    overscroll-behavior: contain;
    max-height: calc(100vh - 75px);
    position: sticky;
    top: 75px;
    mask-image: linear-gradient(
      to bottom,
      transparent,
      black 30px,
      black calc(100% - 30px),
      transparent 100%
    );

    @media (max-width: 1024px) {
      display: none;
      --sl-sidebar-visibility: hidden;
      z-index: 5;
      padding-top: 55px;
      position: fixed;
      right: 0;
      width: 75vw;
      border-left: 1px solid var(--border-extra);
      padding-left: 20px;
      padding-right: 20px;
      top: 0;
      max-height: unset;
    }
  }

  :global([aria-expanded='true']) ~ .sidebar-pane {
    --sl-sidebar-visibility: visible;
    display: block;
  }

  .sidebar-content {
    min-height: max-content;
    flex-direction: column;
    gap: 1rem;
    &:after {
      content: '';
      padding-bottom: 1px;
    }
    @media (max-width: 1024px) {
      &:after {
        content: unset;
      }
    }
  }

  .main-frame {
    @media (max-width: 1024px) {
      padding-top: calc(48px + var(--sl-mobile-toc-height));
    }
  }
</style>
