{{/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

Custom breadcrumb partial for Apache Polaris.
For documentation pages (under /in-dev/unreleased/ or /releases/), 
replaces the first breadcrumb with "Documentation" linking to /docs/.
*/}}
{{ $isSingle := true -}}
{{ with .Parent -}}
  {{ $isSingle = .IsHome -}}
{{ end -}}

{{/* Check if this is a documentation page */}}
{{ $isDocsPage := or (hasPrefix .RelPermalink "/in-dev/unreleased") (hasPrefix .RelPermalink "/releases/") -}}
{{ $docsPage := .Site.GetPage "/docs" -}}

<nav aria-label="breadcrumb" class="td-breadcrumbs
    {{- if $isSingle }} td-breadcrumbs__single {{- end }}">
  <ol class="breadcrumb">
    {{- if and $isDocsPage $docsPage }}
      {{/* For documentation pages, start with "Documentation" breadcrumb */}}
      <li class="breadcrumb-item">
        <a href="{{ $docsPage.RelPermalink }}">{{ $docsPage.LinkTitle }}</a>
      </li>
      {{/* Then render the rest of the breadcrumb chain, skipping /in-dev/ and /releases/ */}}
      {{- template "docs-breadcrumbnav" (dict "p1" . "p2" . "site" .Site) }}
    {{- else }}
      {{/* For non-documentation pages, use standard breadcrumb */}}
      {{- template "breadcrumbnav" (dict "p1" . "p2" .) }}
    {{- end }}
  </ol>
</nav>

{{/* Standard breadcrumb navigation (for non-docs pages) */}}
{{- define "breadcrumbnav" -}}
  {{ if .p1.Parent -}}
    {{ if not .p1.Parent.IsHome -}}
      {{ template "breadcrumbnav" (dict "p1" .p1.Parent "p2" .p2 )  -}}
    {{ end -}}
  {{ else if not .p1.IsHome -}}
    {{ template "breadcrumbnav" (dict "p1" .p1.Site.Home "p2" .p2 )  -}}
  {{ end -}}
  {{ $isActive :=  eq .p1 .p2 }}
  <li class="breadcrumb-item{{ if $isActive }} active{{ end }}"
      {{- if $isActive }} aria-current="page"{{ end }}>
    {{ if $isActive -}}
      {{ .p1.LinkTitle -}}
    {{ else -}}
      <a href="{{ .p1.RelPermalink }}">{{ .p1.LinkTitle }}</a>
    {{- end -}}
  </li>
{{- end -}}

{{/* Documentation breadcrumb navigation - skips /in-dev/ and /releases/ parent pages */}}
{{- define "docs-breadcrumbnav" -}}
  {{/* Stop recursion at /in-dev/ or /releases/ (we already added Documentation) */}}
  {{ $skipParent := or (eq .p1.RelPermalink "/in-dev/") (eq .p1.RelPermalink "/releases/") -}}
  {{ if and .p1.Parent (not $skipParent) -}}
    {{ $parentSkip := or (eq .p1.Parent.RelPermalink "/in-dev/") (eq .p1.Parent.RelPermalink "/releases/") -}}
    {{ if and (not .p1.Parent.IsHome) (not $parentSkip) -}}
      {{ template "docs-breadcrumbnav" (dict "p1" .p1.Parent "p2" .p2 "site" .site)  -}}
    {{ end -}}
  {{ end -}}
  {{/* Don't render the /in-dev/ or /releases/ page itself */}}
  {{ if not $skipParent -}}
    {{ $isActive := eq .p1 .p2 }}
    <li class="breadcrumb-item{{ if $isActive }} active{{ end }}"
        {{- if $isActive }} aria-current="page"{{ end }}>
      {{ if $isActive -}}
        {{ .p1.LinkTitle -}}
      {{ else -}}
        <a href="{{ .p1.RelPermalink }}">{{ .p1.LinkTitle }}</a>
      {{- end -}}
    </li>
  {{- end -}}
{{- end -}}

