{{- $pc := site.Config.Privacy.Vimeo -}}
{{- if not $pc.Disable -}}
  {{- $ctx := dict "ctx" . }}
  {{- if .IsNamedParams -}}
    {{- with .Get "id" -}}
      {{- $ctx = merge $ctx (dict "id" . "class" ($.Get "class")) -}}
      {{- template "render-vimeo" $ctx -}}
    {{- else -}}
      {{- errorf "The %q shortocde requires a single named parameter, the ID of the Vimeo video. See %s" .Name .Position -}}
    {{- end -}}
  {{- else -}}
    {{- with .Get 0 -}}
      {{- $ctx = merge $ctx (dict "id" . "class" ($.Get 1)) -}}
      {{- template "render-vimeo" $ctx -}}
    {{- else -}}
      {{- errorf "The %q shortocde requires a single positional parameter, the ID of the Vimeo video. See %s" .Name .Position -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

{{- define "render-vimeo" -}}
  {{- $dnt := cond site.Config.Privacy.Vimeo.EnableDNT 1 0 -}}
  {{- $url := urls.JoinPath "https://vimeo.com" .id -}}
  {{- $query := querify "url" $url "dnt" $dnt -}}
  {{- $request := printf "https://vimeo.com/api/oembed.json?%s" $query -}}
  {{- with try (resources.GetRemote $request) -}}
    {{- with .Err -}}
      {{- errorf "%s" . -}}
    {{- else with .Value -}}
      {{- with . | transform.Unmarshal -}}
        {{- $class := printf "%s %s" "s_video_simple" "__h_video" -}}
        {{- with $.class -}}
          {{- $class = printf "%s %s" "s_video_simple" . -}}
        {{- else -}}
          {{ template "__h_simple_css" $.ctx.Page }}
        {{- end -}}
        {{- $thumbnail := .thumbnail_url -}}
        {{- $original := $thumbnail | replaceRE "(_.*\\.)" "." -}}
        <div class="{{ $class }}">
          <a href="{{ .provider_url }}{{ .video_id }}" rel="noopener" target="_blank">
            <img src="{{ $thumbnail }}" srcset="{{ $thumbnail }} 1x, {{ $original }} 2x" alt="{{ .title }}">
            <div class="play">
              {{ template "__h_simple_icon_play" $.ctx.Page }}
            </div>
          </a>
        </div>
      {{- end -}}
    {{- else -}}
      {{- warnidf "shortcode-vimeo-simple" "The %q shortcode was unable to retrieve the remote data. See %s" $.ctx.Name $.ctx.Position -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
