{{ $page := . }}

{{ $basepath := path.Clean (urls.Parse (relLangURL "")).Path }}
{{ $links := false }}
{{ $pills := false }}
{{ $tabs := false }}
{{ $underline := false }}

{{ if eq $basepath "/" }}
    {{ $basepath = "" }}
{{ end }}

{{ $pagepath := strings.TrimPrefix $basepath $page.RelPermalink }}

{{ with $page.Param "paige.menu_style" }}
    {{ if eq . "pills" }}
        {{ $pills = true }}
    {{ else if eq . "tabs" }}
        {{ $tabs = true }}
    {{ else if eq . "underline" }}
        {{ $underline = true }}
    {{ else }}
        {{ $links = true }}
    {{ end }}
{{ else }}
    {{ $links = true }}
{{ end }}

{{ with site.Menus.main }}
    <nav>
        <ul class="align-items-center justify-content-center nav {{ if $pills }} nav-pills {{ else if $tabs }} nav-tabs {{ else if $underline }} nav-underline {{ end }}">
            {{ range . }}
                {{ $menupath := strings.TrimPrefix $basepath .URL }}
                {{ $home := and (eq $menupath "/") (eq $pagepath "/") }}
                {{ $menuprefix := and (ne $menupath "/") (hasPrefix $pagepath $menupath) }}
                {{ $sectionprefix := and (ne $menupath "/") (hasPrefix $menupath (printf "/%s/" $page.Section)) }}
                {{ $active := or $home $menuprefix $sectionprefix }}

                <li class="nav-item {{ if .Children }} dropdown {{ end }}">
                    <a {{ if $active }} aria-current="page" {{ end }} {{ if .Children }} aria-expanded="false" {{ end }} class="{{ if $active }} active {{ if $links }} text-body-emphasis {{ end }} {{ end }} {{ if .Params.paige.disabled }} disabled {{ end }} nav-link {{ if .Children }} dropdown-toggle {{ end }} {{ if $links }} text-decoration-underline {{ end }}" {{ if .Children }} data-bs-toggle="dropdown" {{ end }} href="{{ if .Children -}} # {{- else -}} {{ .URL | safeURL }} {{- end }}" {{ if .Children }} role="button" {{ end }} {{ if not .Children }} {{ with partial "paige/target.html" (dict "page" $page "url" .URL) }} target="{{ . }}" {{ end }} {{ end }}>{{ .Name }}</a>

                    {{ with .Children }}
                        <ul class="dropdown-menu">
                            {{ range . }}
                                <li>
                                    <a class="{{ if .Params.paige.disabled }} disabled {{ end }} dropdown-item" href="{{ .URL | safeURL }}" {{ with partial "paige/target.html" (dict "page" $page "url" .URL) }} target="{{ . }}" {{ end }}>{{ .Name }}</a>
                                </li>

                                {{ if .Params.paige.divider_below }}
                                    <li>
                                        <hr class="dropdown-divider">
                                    </li>
                                {{ end }}
                            {{ end }}
                        </ul>
                    {{ end }}
                </li>
            {{ end }}
        </ul>
    </nav>
{{ end }}
