{{ $current_page := . }}
{{ $highlight_active_link := site.Params.main_menu.highlight_active_link | default true }}
{{ $show_current_language := site.Params.main_menu.show_language | default false }}

{{/* Get site logo. */}}
{{ $show_logo := site.Params.main_menu.show_logo | default true }}
{{ $has_logo := fileExists "assets/images/logo.png" | or (fileExists "assets/images/logo.svg") }}
{{ $logo := "" }}
{{ if $has_logo }}
  {{ $logo = (partial "functions/get_logo" (dict "constraint" "max_height" "size" 70)) }}
{{ end }}

<nav class="navbar navbar-expand-lg navbar-light compensate-for-scrollbar" id="navbar-main">
  <div class="container">

    {{if $show_logo}}
    <div class="d-none d-lg-inline-flex">
      <a class="navbar-brand" href="{{ "/" | relLangURL }}">
        {{- if $has_logo -}}
          <img src="{{ $logo.RelPermalink }}" alt="{{ site.Title }}">
        {{- else -}}
          {{- site.Title -}}
        {{- end -}}
      </a>
    </div>
    {{end}}

    {{ if site.Menus.main }}
    <button type="button" class="navbar-toggler" data-toggle="collapse"
            data-target="#navbar-content" aria-controls="navbar" aria-expanded="false" aria-label="{{ i18n "toggle_navigation" }}">
    <span><i class="fas fa-bars"></i></span>
    </button>
    {{ end }}

    {{if $show_logo}}
    <div class="navbar-brand-mobile-wrapper d-inline-flex d-lg-none">
      <a class="navbar-brand" href="{{ "/" | relLangURL }}">
      {{- if $has_logo -}}
        <img src="{{ $logo.RelPermalink }}" alt="{{ site.Title }}">
      {{- else -}}
        {{- site.Title -}}
      {{- end -}}
      </a>
    </div>
    {{end}}

    <!-- Collect the nav links, forms, and other content for toggling -->
    {{ $align_menu := site.Params.main_menu.align | default "l" }}
    <div class="navbar-collapse main-menu-item collapse {{ if eq $align_menu "c" }}justify-content-center{{ else if eq $align_menu "r" }}justify-content-end{{else}}justify-content-start{{ end }}" id="navbar-content">

      <!-- Left Nav Bar -->
      <ul class="navbar-nav d-md-inline-flex">
        {{ range site.Menus.main }}

        {{ if .HasChildren }}
        <li class="nav-item dropdown">
          <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true">
            {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
            <span class="caret"></span>
          </a>
          <div class="dropdown-menu">
            {{ range .Children }}
              <a class="dropdown-item" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}>
                {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
              </a>
            {{ end }}
          </div>
        </li>

        {{ else }}

        {{/* Set target for link. */}}
        {{ $.Scratch.Set "target" "" }}
        {{ if gt (len .URL) 4 }}
          {{ if eq "http" (slicestr .URL 0 4) }}
            {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
          {{ end }}
        {{ end }}

        {{/* Get active page. */}}
        {{ $is_link_in_current_path := in $current_page.RelPermalink .URL }}
        {{ $is_widget_page := or $current_page.IsHome (eq $current_page.Type "widget_page") }}
        {{ $hash := findRE "#(.+)" .URL }}
        {{ $is_same_page := $is_link_in_current_path }}
        {{ if gt (len $hash) 0 }}
          {{ $hash = index $hash 0 }}
          {{ $hash_removed := replace .URL $hash "" }}
          {{ if eq (len $hash_removed) 0 }}
            {{ $hash_removed = "/" }}{{/* Add robustness for `/#SECTION` or `#SECTION` in `menus.toml`. */}}
          {{ end }}
          {{ $is_same_page = eq (path.Dir $current_page.RelPermalink) (path.Dir ($hash_removed|relLangURL)) }}
        {{ end }}

        <li class="nav-item">
          <a class="nav-link {{if and $highlight_active_link $is_link_in_current_path }} active{{end}}" href="{{.URL | relLangURL}}"{{ if and $is_widget_page $is_same_page }} data-target="{{$hash}}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
            {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
          </a>
        </li>

        {{ end }}
        {{ end }}

      {{ if site.Menus.main_right | and (eq $align_menu "l") }}
      </ul>
      <ul class="navbar-nav ml-md-auto">
      {{ end }}

        {{ range site.Menus.main_right }}

        {{/* Set target for link. */}}
        {{ $.Scratch.Set "target" "" }}
        {{ if gt (len .URL) 4 }}
        {{ if eq "http" (slicestr .URL 0 4) }}
        {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
        {{ end }}
        {{ end }}

        <li class="nav-item">
          <a class="nav-link" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
            {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
          </a>
        </li>

        {{ end }}
      </ul>
    </div><!-- /.navbar-collapse -->

    <ul class="nav-icons navbar-nav flex-row ml-auto d-flex pl-md-2">
      {{ if site.Params.search.engine }}
      <li class="nav-item">
        <a class="nav-link js-search" href="#" aria-label="{{ i18n "search" }}"><i class="fas fa-search" aria-hidden="true"></i></a>
      </li>
      {{ end }}

      {{ if site.Params.day_night }}
      <li class="nav-item dropdown theme-dropdown">
        <a href="#" class="nav-link js-theme-selector" data-toggle="dropdown" aria-haspopup="true">
          <i class="fas fa-palette" aria-hidden="true"></i>
        </a>
        <div class="dropdown-menu">
          <a href="#" class="dropdown-item js-set-theme-light">
            <span>{{ i18n "theme_light" | default "Light" }}</span>
          </a>
          <a href="#" class="dropdown-item js-set-theme-dark">
            <span>{{ i18n "theme_dark" | default "Dark" }}</span>
          </a>
          <a href="#" class="dropdown-item js-set-theme-auto">
            <span>{{ i18n "theme_auto" | default "Automatic" }}</span>
          </a>
        </div>
      </li>
      {{ end }}

      {{ if .IsTranslated }}
      <li class="nav-item dropdown i18n-dropdown">
        <a href="#" class="nav-link {{ if $show_current_language }}dropdown-toggle{{end}}" data-toggle="dropdown" aria-haspopup="true">
          <i class="fas fa-globe mr-1" aria-hidden="true"></i>
          {{- if $show_current_language -}}
            <span class="d-none d-lg-inline">{{ index site.Data.i18n.languages .Lang }}</span>
          {{- end -}}
        </a>
        <div class="dropdown-menu">
          <div class="dropdown-item dropdown-item-active">
            <span>{{ index site.Data.i18n.languages .Lang }}</span>
          </div>
          {{ range .Translations }}
          <a class="dropdown-item" href="{{ .Permalink }}"{{ if $.IsHome }} data-target="{{ .RelPermalink }}"{{ end }}>
            <span>{{ index site.Data.i18n.languages .Lang }}</span>
          </a>
          {{ end }}
        </div>
      </li>
      {{ end }}

    </ul>

  </div><!-- /.container -->
</nav>
