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

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

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

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

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


<div class="mobile-menu-overlay [transition:background-color_1.5s_ease] fixed inset-0 z-10 bg-black/80 dark:bg-black/60 hidden"></div>
<aside class="sidebar-container flex flex-col print:hidden md:top-16 md:shrink-0 md:w-64 md:self-start max-md:[transform:translate3d(0,-100%,0)] {{ $sidebarClass }}">
  <!-- Search bar on small screen -->
  <div class="px-4 pt-4 md:hidden">
    {{ partial "search.html" }}
  </div>
  <div class="hextra-scrollbar overflow-y-auto overflow-x-hidden p-4 grow md:h-[calc(100vh-var(--navbar-height)-var(--menu-height))]">
    <ul class="flex flex-col gap-1 md:hidden">
      <!-- Nav -->
      {{ template "sidebar-main" (dict "context" site.Home "pageURL" $pageURL "page" $context "toc" true) -}}
      {{ template "sidebar-footer" }}
    </ul>

    <!-- Sidebar on large screen -->
    {{- if $disableSidebar -}}
      {{- if $displayPlaceholder }}<div class="max-xl:hidden h-0 w-64 shrink-0"></div>{{ end -}}
      {{ .context.Scratch.Set "enableFooterSwitches" true }}
    {{- else -}}
      <ul class="flex flex-col gap-1 max-md:hidden">
        {{ template "sidebar-main" (dict "context" $navRoot "page" $context  "pageURL" $pageURL) }}
        {{ template "sidebar-footer" }}
      </ul>
    {{ end -}}
  </div>
  {{/* Hide theme switch when sidebar is disabled */}}
  {{ $switchesClass := cond $disableSidebar "md:hidden" "" -}}
  {{ $displayThemeToggle := (site.Params.theme.displayToggle | default true) -}}

  {{ if or site.IsMultiLingual $displayThemeToggle }}
    <div class="{{ $switchesClass }} {{ with site.IsMultiLingual }}justify-end{{ end }} sticky bottom-0 bg-white dark:bg-dark mx-4 py-4 shadow-[0_-12px_16px_#fff] flex items-center gap-2 dark:border-neutral-800 dark:shadow-[0_-12px_16px_#111] contrast-more:border-neutral-400 contrast-more:shadow-none contrast-more:dark:shadow-none 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="flex grow flex-col">{{ partial "theme-toggle" }}</div>
        {{- end -}}
      {{- end -}}
    </div>
  {{- end -}}
</aside>

{{- define "sidebar-main" -}}
  {{ template "sidebar-tree" (dict "context" .context "level" 0 "page" .page "pageURL" .pageURL "toc" (.toc | default false)) }}
{{- end -}}

{{- define "sidebar-tree" -}}
  {{- if ge .level 4 -}}
    {{- return -}}
  {{- end -}}

  {{- $context := .context -}}
  {{- $page := .page }}
  {{- $pageURL := .page.RelPermalink -}}
  {{- $level := .level -}}
  {{- $toc := .toc | default false -}}

  {{- with $items := union .context.RegularPages .context.Sections -}}
    {{- $items = where $items "Params.sidebar.exclude" "!=" true -}}
    {{- if eq $level 0 -}}
      {{- range $items.ByWeight }}
        {{- if .Params.sidebar.separator -}}
          <li class="[word-break:break-word] mt-5 mb-2 px-2 py-1.5 text-sm font-semibold text-gray-900 first:mt-0 dark:text-gray-100">
            <span class="cursor-default">{{ .LinkTitle }}</span>
          </li>
        {{- else -}}
          {{- $active := eq $pageURL .RelPermalink -}}
          {{- $shouldOpen := or (.Params.sidebar.open) (.IsAncestor $page) $active | default true }}
          <li class="{{ if $shouldOpen }}open{{ end }}">
            {{- template "sidebar-item-link" dict "context" . "active" $active "title" .LinkTitle "link" .RelPermalink -}}
            {{- if and $toc $active -}}
              {{- template "sidebar-toc" dict "page" . -}}
            {{- end -}}
            {{- template "sidebar-tree" dict "context" . "page" $page "pageURL" $pageURL "level" (add $level 1) "toc" $toc -}}
          </li>
        {{- end -}}
      {{- end -}}
    {{- else -}}
      <div class="ltr:pr-0 overflow-hidden">
        <ul class='relative flex flex-col gap-1 before:absolute before:inset-y-1 before:w-px before:bg-gray-200 before:content-[""] ltr:ml-3 ltr:pl-3 ltr:before:left-0 rtl:mr-3 rtl:pr-3 rtl:before:right-0 dark:before:bg-neutral-800'>
          {{- range $items.ByWeight }}
            {{- $active := eq $pageURL .RelPermalink -}}
            {{- $shouldOpen := or (.Params.sidebar.open) (.IsAncestor $page) $active | default true }}
            {{- $title := .LinkTitle | default .File.BaseFileName -}}
            <li class="flex flex-col {{ if $shouldOpen }}open{{ end }}">
              {{- template "sidebar-item-link" dict "context" . "active" $active "title" $title "link" .RelPermalink -}}
              {{- if and $toc $active -}}
                {{ template "sidebar-toc" dict "page" . }}
              {{- end }}
              {{ template "sidebar-tree" dict "context" . "page" $page "pageURL" $pageURL "level" (add $level 1) "toc" $toc }}
            </li>
          {{- end -}}
        </ul>
      </div>
    {{- end -}}
  {{- end }}
{{- end -}}

{{- define "sidebar-toc" -}}
  {{ $page := .page }}
  {{ with $page.Fragments.Headings }}
    <ul class='flex flex-col gap-1 relative before:absolute before:inset-y-1 before:w-px before:bg-gray-200 before:content-[""] dark:before:bg-neutral-800 ltr:pl-3 ltr:before:left-0 rtl:pr-3 rtl:before:right-0 ltr:ml-3 rtl:mr-3'>
      {{- range . }}
        {{- with .Headings }}
          {{- range . -}}
            <li>
              <a
                href="#{{ anchorize .ID }}"
                class="flex rounded px-2 py-1.5 text-sm transition-colors [word-break:break-word] cursor-pointer [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] contrast-more:border gap-2 before:opacity-25 before:content-['#'] text-gray-500 hover:bg-gray-100 hover:text-gray-900 dark:text-neutral-400 dark:hover:bg-primary-100/5 dark:hover:text-gray-50 contrast-more:text-gray-900 contrast-more:dark:text-gray-50 contrast-more:border-transparent contrast-more:hover:border-gray-900 contrast-more:dark:hover:border-gray-50"
              >
                {{- .Title -}}
              </a>
            </li>
          {{ end -}}
        {{ end -}}
      {{ end -}}
    </ul>
  {{ end }}
{{- end -}}

{{- define "sidebar-footer" -}}
  {{- range site.Menus.sidebar -}}
    {{- $name := or (T .Identifier) .Name -}}
    {{ if eq .Params.type "separator" }}
      <li class="[word-break:break-word] mt-5 mb-2 px-2 py-1.5 text-sm font-semibold text-gray-900 first:mt-0 dark:text-gray-100">
        <span class="cursor-default">{{ $name }}</span>
      </li>
    {{ else }}
      <li>{{ template "sidebar-item-link" dict "active" false "title" $name "link" (.URL | relLangURL) }}</li>
    {{ end }}
  {{- end -}}
{{- end -}}

{{- define "sidebar-item-link" -}}
  {{- $external := strings.HasPrefix .link "http" -}}
  {{- $open := .open | default true -}}
  <a
    class="flex items-center justify-between gap-2 cursor-pointer rounded px-2 py-1.5 text-sm transition-colors [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] [word-break:break-word]
    {{- if .active }}
      sidebar-active-item bg-primary-100 font-semibold text-primary-800 contrast-more:border contrast-more:border-primary-500 dark:bg-primary-400/10 dark:text-primary-600 contrast-more:dark:border-primary-500
    {{- else }}
      text-gray-500 hover:bg-gray-100 hover:text-gray-900 contrast-more:border contrast-more:border-transparent contrast-more:text-gray-900 contrast-more:hover:border-gray-900 dark:text-neutral-400 dark:hover:bg-primary-100/5 dark:hover:text-gray-50 contrast-more:dark:text-gray-50 contrast-more:dark:hover:border-gray-50
    {{- end -}}"
    href="{{ .link }}"
    {{ if $external }}target="_blank" rel="noreferer"{{ end }}
  >
    {{- .title -}}
    {{- with .context }}
      {{- if or .RegularPages .Sections }}
        <span class="hextra-sidebar-collapsible-button">
          {{- template "sidebar-collapsible-button" -}}
        </span>
      {{- end }}
    {{ end -}}
  </a>
{{- end -}}

{{- define "sidebar-collapsible-button" -}}
  <svg fill="none" viewBox="0 0 24 24" stroke="currentColor" class="h-[18px] min-w-[18px] rounded-sm p-0.5 hover:bg-gray-800/5 dark:hover:bg-gray-100/5"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" class="origin-center transition-transform rtl:-rotate-180"></path></svg>
{{- end -}}
