{{/* Function to return a linked social icon as a map from an iteration of an author's `social` data. */}}

{{ $scr := newScratch }}

{{/* Get icon name. */}}
{{ $scr.SetInMap "social_link" "icon" .icon }}

{{/* Get icon pack (default to Font Awesome's Solid pack). */}}
{{ $pack := or .icon_pack "fas" }}
{{ $scr.SetInMap "social_link" "icon_pack" $pack }}

{{/* Derive Font Awesome class name prefix. */}}
{{ $pack_prefix := $pack }}
{{ if in (slice "fab" "fas" "far" "fal") $pack }}
  {{ $pack_prefix = "fa" }}
{{ end }}
{{ $scr.SetInMap "social_link" "pack_prefix" $pack_prefix }}

{{/* Get tooltip label (default to none). */}}
{{ $scr.SetInMap "social_link" "tooltip" (.label | default "") }}

{{/* Get screen reader label (default to icon name). */}}
{{ $scr.SetInMap "social_link" "aria_label" (.label | default .icon) }}

{{/* Get external link or relative internal link. */}}
{{ $link := .link }}
{{ $target := "" }}
{{ $scheme := (urls.Parse $link).Scheme }}
{{ if not $scheme }}
  {{ $link = .link | relLangURL }}
{{ else if in (slice "http" "https") $scheme }}
  {{ $target = "target=\"_blank\" rel=\"noopener\"" }}
{{ end }}
{{ $scr.SetInMap "social_link" "link" $link }}
{{ $scr.SetInMap "social_link" "target" $target }}

{{ return $scr.Get "social_link" }}