{{/* Hugo Blox: Buttons */}}
{{/* Documentation: https://hugoblox.com/blocks/ */}}
{{/* License: https://github.com/HugoBlox/kit/blob/main/LICENSE.md */}}

{{/* Initialise */}}
{{ $page := .wcPage }}
{{ $block := .wcBlock }}
{{ $content := $block.content }}
{{ if not (reflect.IsMap $content) }}{{ $content = dict }}{{ end }}

<div class="flex items-center flex-col mx-auto w-full justify-center mt-4 px-8 pb-5">

{{ $buttons_raw := index $content "buttons" }}
{{ $buttons := slice }}
{{ if reflect.IsSlice $buttons_raw }}
  {{ $buttons = $buttons_raw }}
{{ else if and $buttons_raw (reflect.IsMap $buttons_raw) }}
  {{ $buttons = slice $buttons_raw }}
{{ else if eq (printf "%T" $buttons_raw) "string" }}
  {{ $buttons = slice (dict "url" $buttons_raw) }}
{{ end }}

{{ range $index, $item := $buttons }}
  {{ $text := "" }}
  {{ $url := "" }}
  {{ $icon := "" }}
  {{ $new_tab := false }}
  {{ if reflect.IsMap $item }}
    {{ $text_raw := index $item "text" | default (index $item "label") | default (index $item "name") }}
    {{ if ne $text_raw nil }}{{ $text = strings.TrimSpace (printf "%v" $text_raw) }}{{ end }}
    {{ $url_raw := index $item "url" | default (index $item "link") }}
    {{ if eq (printf "%T" $url_raw) "string" }}{{ $url = strings.TrimSpace $url_raw }}{{ end }}
    {{ $icon_raw := index $item "icon" }}
    {{ if eq (printf "%T" $icon_raw) "string" }}{{ $icon = $icon_raw }}{{ end }}
    {{ $new_tab = partial "functions/coerce_bool" (dict "value" (index $item "new_tab") "default" false) }}
  {{ else if eq (printf "%T" $item) "string" }}
    {{ $url = strings.TrimSpace $item }}
  {{ end }}
  {{ if and (not $text) $url }}
    {{ $text = $url }}
  {{ end }}
  {{ if $url }}
    {{ $link := $url }}
    {{ $target := "" }}
    {{ $scheme := (urls.Parse $link).Scheme }}
    {{ if not $scheme }}
      {{ if not (hasPrefix $link "#") }}
        {{ $link = $link | relLangURL }}
      {{ end }}
      {{ if eq (path.Ext $link) ".pdf" }}
        {{ $new_tab = true }}
      {{ end }}
    {{ else if in (slice "http" "https") $scheme }}
      {{ $new_tab = true }}
    {{ end }}
    {{ if $new_tab }}
      {{ $target = "target=\"_blank\" rel=\"noopener noreferrer\"" }}
    {{ end }}
    <a href="{{ $link | safeURL }}" {{ $target | safeHTMLAttr }} class="flex items-center p-1 w-full rounded-md hover:scale-105 transition-all bg-gray-100 mb-3 max-w-3xl">
      <div class="flex text-center w-full">
        <div class="w-10 h-10 text-gray-700">
          {{ with $icon }}
            {{ partial "functions/get_icon" (dict "name" . "attributes" "height=\"40\" width=\"40\" class=\"rounded-sm\"") }}
          {{ end }}
        </div>
        <div class="flex justify-center items-center font-semibold w-full text-gray-700 -ml-10">
          {{ $text | markdownify | emojify }}
        </div>
      </div>
    </a>
  {{ end }}
{{ end }}

</div>
