{{ define "book-menu" }}

{{- $first := false -}}
{{- $current_node := .current_node -}}
{{- $order_by := .order_by -}}
{{ $icon := "" }}

{{ with .sect }}
  {{ if .IsSection}}
    {{- $first = eq $current_node.FirstSection . -}}

    {{- safeHTML $current_node.FirstSection.Params.pre_nav -}}

    {{/* Get section icon. */}}
    {{ $pack := or .Params.icon_pack "fas" }}
    {{ $pack_prefix := $pack }}
    {{ if in (slice "fab" "fas" "far" "fal") $pack }}
      {{ $pack_prefix = "fa" }}
    {{ end }}
    {{ with .Params.icon }}
      {{- if eq $pack "emoji" -}}
        {{- . | emojify -}}
      {{- else if eq $pack "custom" -}}
        {{- $svg_icon := resources.Get (printf "images/icon-pack/%s.svg" .) -}}
        {{- if $svg_icon -}}
          {{ $icon = printf "<img src=\"%s\" alt=\"%s\" class=\"svg-icon svg-baseline pr-1\">" $svg_icon.RelPermalink . }}
        {{- end -}}
      {{- else -}}
        {{ $icon = printf "<i class=\"%s %s-%s pr-1\"></i>" $pack $pack_prefix . }}
      {{- end -}}
    {{ end }}

    {{ if $first }}
      <ul class="nav docs-sidenav">
        <li class="{{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }}active{{ end }}"><a href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle | default .Title }}</a></li>
    {{else}}
      <div class="docs-toc-item">
        <a class="docs-toc-link {{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }} active{{ end }}" href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle | default .Title }}</a>
    {{end}}

    {{- $page_count := (add (len .Pages) (len .Sections)) -}}
    {{ if ne $page_count 0 }}
      {{ if not $first }}
        <ul class="nav docs-sidenav">
      {{end}}
      {{- .Scratch.Set "pages" .Pages -}}
      {{- if .Sections -}}
        {{- .Scratch.Set "pages" (.Pages | union .Sections) -}}
      {{- end -}}
      {{- $pages := (.Scratch.Get "pages") -}}
      {{- if eq $order_by "title" -}}
        {{- range $pages.ByTitle -}}
          {{ template "book-menu" dict "sect" . "current_node" $current_node "order_by" $order_by }}
        {{- end -}}
      {{- else if eq $order_by "title_desc" -}}
        {{- range $pages.ByTitle.Reverse -}}
          {{ template "book-menu" dict "sect" . "current_node" $current_node "order_by" $order_by }}
        {{- end -}}
      {{- else -}}
        {{- range $pages.ByWeight -}}
          {{ template "book-menu" dict "sect" . "current_node" $current_node "order_by" $order_by }}
        {{- end -}}
      {{- end}}
      {{ if not $first }}
        </ul>
      {{end}}
    {{end}}

    {{ if not $first }}
      </div>
    {{else}}
      </ul>
    {{end}}

  {{- else -}}
    {{- if not .Params.Hidden -}}
      <li class="{{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }}active{{ end }}"><a href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle | default .Title }}</a></li>
    {{- end -}}
  {{ end -}}
{{- end -}}

{{/* End define. */}}
{{ end -}}
