{{- $src := .Get "src" -}}
{{- $id := .Get "id" -}}
{{- $title := .Get "title" | default "Video" -}}
{{- $class := .Get "class" | default "" -}}

{{- if $id -}}
  <div class="my-6{{- with $class }} {{ . }}{{- end -}}">
    <iframe
      class="w-full aspect-video"
      src="https://www.youtube.com/embed/{{ $id }}"
      title="{{ $title }}"
      allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
      allowfullscreen
    ></iframe>
  </div>
{{- else if $src -}}
  {{- $ext := $src | path.Ext -}}
  {{- $filetype := slicestr $ext 1 -}}
  {{- $videoURL := $src -}}
  {{- $resource := .Page.Resources.GetMatch $src -}}
  {{- if $resource -}}{{- $videoURL = $resource.RelPermalink -}}{{- end -}}
  <video
    {{- with $class }} class="{{ . }}"{{ end }}
    controls
    preload="auto"
    playsinline
    {{- with .Get "width" }} width="{{ . }}"{{ end }}
    {{- with .Get "height" }} height="{{ . }}"{{ end }}
    {{- if eq (.Get "autoplay") "true" }} autoplay{{ end }}
    {{- if eq (.Get "loop") "true" }} loop{{ end }}
    {{- if eq (.Get "muted") "true" }} muted{{ end }}
  >
    <source src="{{ $videoURL }}" type="video/{{ $filetype }}">
  </video>
{{- else -}}
  {{- errorf "video shortcode: either 'src' or 'id' parameter is required" -}}
{{- end -}}
