{{- warnf "The \"twitter\", \"tweet\", and \"twitter_simple\" shortcodes were deprecated in v0.142.0 and will be removed in a future release. Please use the \"x\" shortcode instead." }}
{{- $pc := site.Config.Privacy.Twitter -}}
{{- if not $pc.Disable -}}
  {{- if $pc.Simple -}}
    {{- template "_shortcodes/twitter_simple.html" . -}}
  {{- else -}}
    {{- $id := or (.Get "id") "" -}}
    {{- $user := or (.Get "user") "" -}}
    {{- if and $id $user -}}
      {{- template "render-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT "ctx" .) -}}
    {{- else -}}
      {{- errorf "The %q shortcode requires two named parameters: user and id. See %s" .Name .Position -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

{{- define "render-tweet" -}}
  {{- $url := printf "https://twitter.com/%v/status/%v" .user .id -}}
  {{- $query := querify "url" $url "dnt" .dnt -}}
  {{- $request := printf "https://publish.twitter.com/oembed?%s" $query -}}
  {{- with try (resources.GetRemote $request) -}}
    {{- with .Err -}}
      {{- warnidf "shortcode-twitter-getremote" "The %q shortcode was unable to retrieve the remote data: %s. See %s" $.ctx.Name . $.ctx.Position -}}
    {{- else with .Value -}}
      {{- (. | transform.Unmarshal).html | safeHTML -}}
    {{- else -}}
      {{- warnidf "shortcode-twitter-getremote" "The %q shortcode was unable to retrieve the remote data. See %s" $.ctx.Name $.ctx.Position -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
