{{- $context := .context -}}

{{- $disableSidebar := .disableSidebar | default false -}}
{{- $displayPlaceholder := .displayPlaceholder | default false -}}

{{- $sidebarClass := cond $disableSidebar (cond $displayPlaceholder "md:hx-hidden xl:hx-block" "md:hx-hidden") "md:hx-sticky" -}}

{{- $navRoot := cond (eq site.Home.Type "docs") site.Home $context.FirstSection -}}
{{- $pageURL := $context.RelPermalink -}}

{{- $data := slice -}}
{{- $dataMobile := slice -}}

{{- if (eq site.Params.page.sidebar.source "data") -}}
  {{ $data = partialCached "components/sidebar/get-section-data" $context $context.Section }}
  {{- $dataMobile = $data -}}
{{- else -}}
  {{- $data = (partialCached "components/sidebar/generate-section-data" $navRoot $navRoot) | unmarshal -}}
  {{- $dataMobile = (partialCached "components/sidebar/generate-section-data" site.Home site.Home) | unmarshal -}}
{{- end -}}

{{- $shouldCache := site.Params.page.sidebar.cache | default false -}}

{{/* EXPERIMENTAL */}}
{{- if .context.Params.sidebar.hide -}}
  {{- $disableSidebar = true -}}
  {{- $displayPlaceholder = true -}}
{{- end -}}

<div class="mobile-menu-overlay [transition:background-color_1.5s_ease] hx-fixed hx-inset-0 hx-z-10 hx-bg-black/80 dark:hx-bg-black/60 hx-hidden"></div>

<aside class="hextra-sidebar-container hx-flex hx-flex-col print:hx-hidden md:hx-top-16 md:hx-shrink-0 md:hx-w-64 md:hx-self-start max-md:[transform:translate3d(0,-100%,0)] {{ $sidebarClass }}">
  {{/* Search bar on small screen */}}
  {{- partialCached "components/sidebar/mobile-search" . -}}


  <div class="hextra-scrollbar hx-overflow-y-auto hx-overflow-x-hidden hx-p-4 hx-grow md:hx-h-[calc(100vh-var(--navbar-height)-var(--menu-height))]">
    {{/* Mobile Navigation */}}
    <ul class="hx-flex hx-flex-col hx-gap-1 md:hx-hidden">
      {{- with $dataMobile -}}
        {{- partial "components/sidebar/render-data" (dict "data" . "page" $context) -}}
      {{- end -}}
    </ul>

    {{/* Sidebar on large screen */}}
    {{- if $disableSidebar -}}
      {{- if $displayPlaceholder }}<div class="max-xl:hx-hidden hx-h-0 hx-w-64 hx-shrink-0"></div>{{ end -}}
      {{ .context.Scratch.Set "enableFooterSwitches" true }}
    {{- else -}}
      <ul class="hx-flex hx-flex-col hx-gap-1 max-md:hx-hidden">
        {{- with $data -}}
          {{- if $shouldCache -}}
            {{- partialCached "components/sidebar/render-data" (dict "data" . "page" $context "cached" $shouldCache) $navRoot -}}
          {{- else -}}
            {{- partial "components/sidebar/render-data" (dict "data" . "page" $context "cached" $shouldCache) -}}
          {{- end -}}
        {{- end -}}
        {{- partialCached "components/sidebar/bottom" $context site.Home -}}
      </ul>
    {{ end -}}
  </div>
  {{- partial "components/sidebar/switches" (dict "context" $context "disableSidebar" $disableSidebar) -}}
</aside>

{{- define "partials/components/sidebar/mobile-search" -}}
  <div class="hx-px-4 hx-pt-4 md:hx-hidden">
    {{- partialCached "search.html" . -}}
  </div>
{{- end -}}

{{- define "partials/components/sidebar/switches" -}}
  {{- $context := .context -}}
  {{- $disableSidebar := .disableSidebar -}}
  {{/* Hide theme switch when sidebar is disabled */}}
  {{ $switchesClass := cond $disableSidebar "md:hx-hidden" "" -}}
  {{ $displayThemeToggle := (site.Params.theme.displayToggle | default true) -}}

  {{ if or site.IsMultiLingual $displayThemeToggle }}
    <div
      class="{{ $switchesClass }} {{ with site.IsMultiLingual }}
        hx-justify-end
      {{ end }} hx-sticky hx-bottom-0 hx-bg-white dark:hx-bg-dark hx-mx-4 hx-py-4 hx-shadow-[0_-12px_16px_#fff] hx-flex hx-items-center hx-gap-2 dark:hx-border-neutral-800 dark:hx-shadow-[0_-12px_16px_#111] contrast-more:hx-border-neutral-400 contrast-more:hx-shadow-none contrast-more:dark:hx-shadow-none hx-border-t"
      data-toggle-animation="show"
    >
      {{- with site.IsMultiLingual -}}
        {{- partial "language-switch" (dict "context" $context "grow" true) -}}
        {{- with $displayThemeToggle }}{{ partial "theme-toggle" (dict "hideLabel" true) }}{{ end -}}
      {{- else -}}
        {{- with $displayThemeToggle -}}
          <div class="hx-flex hx-grow hx-flex-col">{{ partial "theme-toggle" }}</div>
        {{- end -}}
      {{- end -}}
    </div>
  {{- end -}}
{{- end -}}
