{{/* tag-cloud.html
  Reusable tag cloud partial for homepage, sidebar, and 404 pages.

  Params (dict):
    page         Page    required — calling page context (provides .Site.Taxonomies.tags, .Lang)
    showCount    bool    optional — show post count per tag (default true)
    heading      string  optional — heading text override (default: i18n "tagCloud")
    headingLevel string  optional — h2|h3|p for non-widget mode (default "h2")
    wrapInWidget bool    optional — wrap in .sidebar-widget for sidebar placement (default false)
    maxTags      int     optional — max tags to show, 0 = all (default 0)
*/}}

{{- $page         := .page -}}
{{- $showCount    := .showCount    | default true -}}
{{- $heading      := .heading      | default (i18n "tagCloud") -}}
{{- $headingLevel := .headingLevel -}}
{{- $wrapInWidget := .wrapInWidget | default false -}}
{{- $maxTags      := .maxTags      | default 0 -}}

{{- $tags := $page.Site.Taxonomies.tags -}}

{{/* Early exit if no tags */}}
{{- if $tags -}}

{{/* Compute max count for continuous scaling */}}
{{- $maxCount := 0 -}}
{{- range $tags -}}
  {{- if gt .Count $maxCount -}}{{- $maxCount = .Count -}}{{- end -}}
{{- end -}}

{{/* Ordered tag list (descending by count) */}}
{{- $orderedTags := $tags.ByCount -}}
{{- if gt $maxTags 0 -}}
  {{- $orderedTags = first $maxTags $orderedTags -}}
{{- end -}}

{{/* Render based on placement mode */}}
{{- if $wrapInWidget -}}
<div class="sidebar-widget">
  <p class="sidebar-widget-label"># {{ i18n "topTags" }}</p>
  <nav aria-label="{{ i18n "exploreTopics" }}">
    <div class="tag-cloud" data-tag-cloud>
{{- else -}}
<section {{- if $headingLevel }} aria-labelledby="tag-cloud-heading"{{ end }}>
  {{- if $headingLevel -}}
  {{- if eq $headingLevel "h2" -}}
  <h2 id="tag-cloud-heading" class="text-lg font-semibold text-accent mb-4">{{ $heading }}</h2>
  {{- else if eq $headingLevel "h3" -}}
  <h3 id="tag-cloud-heading" class="text-lg font-semibold text-accent mb-4">{{ $heading }}</h3>
  {{- else -}}
  <p id="tag-cloud-heading" class="text-lg font-semibold text-accent mb-4">{{ $heading }}</p>
  {{- end -}}
  {{- end -}}
  <nav aria-label="{{ i18n "exploreTopics" }}">
    <div class="tag-cloud" data-tag-cloud>
{{- end -}}

      {{- range $orderedTags -}}
        {{- $count := .Count -}}
        {{- $ratio := (div (float $count) (float $maxCount)) -}}
        {{- $size := (add 0.6 (mul $ratio 1.2)) -}}
        {{- $opacity := (add 0.7 (mul $ratio 0.3)) -}}
        <a
          href="{{ .Page.RelPermalink }}"
          class="tag-cloud-link"
          data-weight="{{ printf "%.4f" $ratio }}"
          {{- if ge $ratio 0.5 }}
          style="font-size: {{ $size }}rem; color: var(--accent); opacity: {{ $opacity }};"
          {{- else }}
          style="font-size: {{ $size }}rem; color: var(--text-dim); opacity: {{ $opacity }};"
          {{- end }}
          aria-label="{{ .Name }}{{- if $showCount }} ({{ i18n "postCount" $count }}){{- end -}}"
        >
          {{- .Name -}}
          {{- if $showCount -}}
          <span class="tag-cloud-count" aria-hidden="true">{{ $count }}</span>
          {{- end -}}
        </a>
      {{- end -}}

    </div>
  </nav>

{{- if $wrapInWidget -}}
</div>
{{- else -}}
</section>
{{- end -}}

{{- end -}}{{/* end if $tags */}}
