{{- $context := . -}}
{{- $disabled := $context.Params.disable_share | default "false" -}}
{{- $config := site.Params.ananke.social -}}
{{- with $config.share.disable_share -}}
  {{- $disabled := . -}}
{{- end -}}

{{- if eq "false" $disabled -}}

  {{- $title := $context.Title -}}
  {{- $url := printf "%s" $context.Permalink | absLangURL -}}

  {{- $config := site.Params.ananke.social -}}
  {{- $networks := $config.share.networks -}}
  {{- $hasLabel := $config.share.sharetext | default "true" -}}
  {{- $hasIcon := $config.share.icons | default "true" -}}
  {{- $setups := (collections.Where $config.networks  "slug" "in" $networks) -}}

  {{/* This here is an ugly workaround for GoHugo's missing sortByArray feature.
    Let's cache it so it does not take away too much time.
    PS: It's also a couple of years old, so maybe there is a better solution by now. */}}
  {{- $setups = partials.IncludeCached "func/sortNetworks.html" (dict "networks" $networks "setups" $setups) "social-follow" -}}

  <div id="sharing" class="mt3 ananke-socials">
    {{- range $setups -}}
      {{- $setup := . -}}
      {{/* @todo i18n $sharetext := lang.Translate (fmt.Printf "share_on_%s" $setup.slug) */}}
      {{- $label := "" -}}
      {{ with $config.share.sharetext -}}
        {{- $label = printf "Share on %s" $setup.label -}}
      {{- end -}}
      {{ if eq .share "false" | default "true" -}}
        {{- continue -}}
        {{/* @todo notification into CLI that a network is configured but not supported */}}
      {{- end -}}
      {{- $options := (dict "context" page "setup" $setup) }}
      {{- $href := partialCached "func/getShareLink.html" $options $options -}}
      <a href="{{ $href }}"
        class="ananke-social-link {{ $setup.slug }} no-underline"
        title="{{ $label }}" aria-label="{{ $label }}"
        target="_blank" rel="nofollow noopener noreferrer">
        {{ with $config.share.icons -}}
          {{/* @todo add the following lines to a partial, reused in social/follow.html */}}
          {{- with $setup.icon -}}
            {{- $icon := resources.Get (printf "ananke/socials/%s.svg" .) -}}
            {{- with $icon -}}
              <span class="icon">
                {{ .Content | safeHTML }}
                {{/* @todo indicator for missing or misconfigured icon */}}
              </span>
            {{- end -}}
          {{- else -}}
            {{- $label -}}
          {{- end -}}
        {{- else -}}
          {{- $label -}}
        {{- end -}}
      </a>
    {{- end -}}
  </div>

{{- end -}} {{/* if eq "true" $disabled */}}

{{ define "partials/func/getShareLink.html" }}
  {{- $context := .context -}}
  {{- $setup := .setup -}}
  {{- $separator := "&" -}}
  {{- $title := $context.Title | transform.HTMLEscape -}}
  {{- $description := $context.Summary | transform.HTMLEscape -}}
  {{- $permalink := $context.Permalink | transform.HTMLEscape -}}
  {{- with $setup.separator -}}
    {{- $separator = . -}}
  {{- end -}}
  {{- $link := fmt.Printf "%s%s" $setup.link "?" -}}
  {{- range $key, $value := $setup.particles -}}
    {{- if compare.Eq $key "params" -}}
      {{- $link = fmt.Printf "%s%s%s" $permalink $separator $value -}}
    {{- else -}}
      {{- if compare.Eq $value "description" -}}
        {{- $link = fmt.Printf "%s%s%s" $link $separator (collections.Querify $key $description) -}}
      {{- else if compare.Eq $value "title" -}}
        {{- $link = fmt.Printf "%s%s%s" $link $separator (collections.Querify $key $title) -}}
      {{- else if compare.Eq $value "permalink" -}}
        {{- $link = fmt.Printf "%s%s%s" $link $separator (collections.Querify $key $permalink) -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}
  {{- return $link -}}
{{ end }}