{{ $integrations := (index . "integrations") }}

{{ $scratch := newScratch }}
{{ $scratch.Set "integrations-ranked" (slice) }}
{{ range $integration := $integrations }}
  {{ $params := $integration.Params }}

  {{ $scratch.Set "tutorials" 0 }}
  {{ if isset $params "tutorials" }}
  {{ $hosts := slice }}
  {{ range $elem := index $params "tutorials" }}
  {{ $url := urls.Parse $elem }}
  {{ $hosts = $hosts | append $url.Host }}
  {{ end }}
  {{ $scratch.Set "tutorials" (len ($hosts | uniq)) }}
  {{ end }}

  {{ $scratch.Set "videos" 0 }}
  {{ if isset $params "videos" }}
  {{ $scratch.Set "videos" (len (index $params "videos")) }}
  {{ end }}

  {{ $scratch.Set "blogs" 0 }}
  {{ if isset $params "blogs" }}
  {{ $hosts := slice }}
  {{ range $elem := index $params "blogs" }}
  {{ $url := urls.Parse $elem }}
  {{ $hosts = $hosts | append $url.Host }}
  {{ end }}
  {{ $scratch.Set "blogs" (len ($hosts | uniq)) }}
  {{ end }}

  {{ $scratch.Set "code" 0 }}
  {{ if isset $params "code" }}
  {{ $scratch.Set "code" (len (index $params "code")) }}
  {{ end }}

  {{ $rank := (add ($scratch.Get "tutorials") (add ($scratch.Get "videos") (add ($scratch.Get "blogs") ($scratch.Get "code")))) }}
  {{ $scratch.Add "integrations-ranked" (slice (dict "id" $integration.RelPermalink "rank" $rank)) }}
{{ end }}

{{ $scratch.Set "integrations" (slice) }}
{{ range $int := sort ($scratch.Get "integrations-ranked") "rank" "desc" }}
{{ $scratch.Add "integrations" $int.id }}
{{ end }}

{{ return ($scratch.Get "integrations") }}
