{{- $next := "" }}
{{- if eq .Page.Kind "term" }}
  {{- /* go to next term page */}}
  {{- $taxonomy_page := .Site.GetPage .Data.Plural }}
  {{- $pages := partialCached "partials/_relearn/pagesTaxonomy.gotmpl" $taxonomy_page $taxonomy_page.Path }}
  {{- $next = partial "inline/next-term" (dict "collection" $pages "item" .) }}
{{- else if eq .Page.Kind "taxonomy" }}
  {{- /* go to first term page */}}
  {{- $pages := partialCached "partials/_relearn/pagesTaxonomy.gotmpl" . .Path }}
  {{- with index $pages 0 }}
    {{- $next = .Page }}
  {{- end }}
{{- else }}
  {{- $next = partial "inline/next-page" . }}
{{- end }}
{{- return $next }}

{{- define "partials/inline/next-term" }}
  {{- $collection := .collection }}
  {{- $item := .item }}
  {{- $found := false }}
  {{- $result := "" }}
  {{- range $collection }}
    {{- if $found }}
      {{- $result = .Page.Page }}
      {{- break }}
    {{- else if eq $item .Page.Page }}
      {{- $found = true }}
    {{- end }}
  {{- end }}
  {{- return $result }}
{{- end }}

{{- define "partials/inline/next-page" }}
  {{- $nextVisible := "" }}
  {{- $currentNode := . }}
  {{- $pages := partialCached "_relearn/pages.gotmpl" (dict "page" $currentNode) $currentNode.Path }}
  {{- range $pages }}
    {{- $nextVisible = partial "inline/leftmost_descendant" . }}
    {{- if $nextVisible }}
      {{- break }}
    {{- end }}
  {{- end }}
  {{- if not $nextVisible }}
    {{- $nextVisible = partial "inline/next_sibling_or_ancestor" $currentNode }}
  {{- end }}
  {{- return $nextVisible }}
{{- end }}

{{- define "partials/inline/next_sibling_or_ancestor" }}
  {{- $currentNode := . }}
  {{- $nextSibling := "" }}
  {{- with partial "inline/parent" $currentNode }}
    {{- $parent := . }}
    {{- $pages := partialCached "_relearn/pages.gotmpl" (dict "page" $parent) $parent.Path }}
    {{- $nextVisible := partial "inline/next_sibling" (dict "siblings" $pages "currentNode" $currentNode) }}
    {{- range $pages }}
      {{- if not $nextVisible }}
        {{- break }}
      {{- end }}
      {{- $nextSibling = partial "inline/leftmost_descendant" $nextVisible }}
      {{- if $nextSibling }}
        {{- break }}
      {{- else }}
        {{- $nextVisible = partial "inline/next_sibling" (dict "siblings" $pages "currentNode" $nextVisible) }}
      {{- end }}
    {{- end }}
    {{- if not $nextSibling }}
      {{- if $nextVisible }}
        {{- $nextSibling = partial "inline/leftmost_descendant" $nextVisible }}
      {{- end }}
      {{- if not $nextSibling }}
        {{- $nextSibling = partial "inline/next_sibling_or_ancestor" $parent }}
      {{- end }}
    {{- end }}
  {{- end }}
  {{- return $nextSibling }}
{{- end }}

{{- define "partials/inline/next_sibling" }}
  {{- $siblings := .siblings }}
  {{- $currentNode := .currentNode }}
  {{- $nextSibling := "" }}
  {{- $currentFound := false }}
  {{- range $siblings }}
    {{- if eq . $currentNode }}
      {{- $currentFound = true }}
    {{- else if and $currentFound (not (or (eq .Title "") .Params.hidden .Params.menuUrl .Params.menuPageRef)) }}
      {{- $nextSibling = . }}
      {{- break }}
    {{- end }}
  {{- end }}
  {{- return $nextSibling }}
{{- end }}

{{- define "partials/inline/leftmost_descendant" }}
  {{- $leftmostVisible := "" }}
  {{- if not (or (eq .Title "") .Params.hidden .Params.menuUrl .Params.menuPageRef) }}
    {{- if .RelPermalink }}
      {{- $leftmostVisible = . }}
    {{- else }}
      {{- $pages := partialCached "_relearn/pages.gotmpl" (dict "page" .) .Path }}
      {{- range $pages }}
        {{- with partial "inline/leftmost_descendant" . }}
          {{- $leftmostVisible = . }}
          {{- break }}
        {{- end }}
      {{- end }}
    {{- end }}
  {{- end }}
  {{- return $leftmostVisible }}
{{- end }}

{{- define "partials/inline/parent" }}
  {{- $nonheadless := "" }}
  {{- $page := . }}
  {{- $parent := .Parent }}
  {{- if not (partial "_relearn/pageIsSpecial.gotmpl" $page) }}
    {{- with $parent }}
      {{- $pages := partialCached "_relearn/pages.gotmpl" (dict "page" .) .Path }}
      {{- range $pages }}
        {{- if eq . $page }}
          {{- $nonheadless = $parent }}
          {{- break }}
        {{- end }}
      {{- end }}
    {{- else }}
      {{- $nonheadless = $parent }}
    {{- end }}
  {{- end }}
  {{- return $nonheadless }}
{{- end }}