{{- /* Asciinema */ -}}
{{- $asciinemaBase := "" -}}
{{- $useDefaultCdn := true -}}
{{- with site.Params.asciinema.base -}}
  {{- $asciinemaBase = . -}}
  {{- $useDefaultCdn = false -}}
{{- end -}}
{{- $asciinemaJsAsset := "" -}}
{{- with site.Params.asciinema.js -}}
  {{- $asciinemaJsAsset = . -}}
{{- end -}}
{{- $asciinemaCssAsset := "" -}}
{{- with site.Params.asciinema.css -}}
  {{- $asciinemaCssAsset = . -}}
{{- end -}}
{{- /* If only js/css is set without base, use local asset loading */ -}}
{{- if and $useDefaultCdn (or (ne $asciinemaJsAsset "") (ne $asciinemaCssAsset "")) -}}
  {{- $useDefaultCdn = false -}}
{{- end -}}
{{- /* Set default CDN base if needed */ -}}
{{- if $useDefaultCdn -}}
  {{- $asciinemaBase = "https://cdn.jsdelivr.net/npm/asciinema-player@latest/dist/bundle" -}}
{{- end -}}
{{- $isRemoteBase := or (strings.HasPrefix $asciinemaBase "http://") (strings.HasPrefix $asciinemaBase "https://") -}}
{{- $minSuffix := cond hugo.IsProduction ".min" "" -}}
{{- /* CSS retrieval: get raw CSS from either local asset or remote, then process */ -}}
{{- if $isRemoteBase -}}
  {{- $cssPath := cond (ne $asciinemaCssAsset "") $asciinemaCssAsset "asciinema-player.css" -}}
  {{- $asciinemaCssUrl := printf "%s/%s" $asciinemaBase $cssPath -}}
  {{- with try (resources.GetRemote $asciinemaCssUrl) -}}
    {{- with .Err -}}
      {{- errorf "Could not retrieve Asciinema css file from %s. Reason: %s." $asciinemaCssUrl . -}}
    {{- else with .Value -}}
      {{- with resources.Copy "css/asciinema-player.css" . -}}
        {{- $asciinemaCss := . | fingerprint -}}
        
      {{- end -}}
    {{- end -}}
  {{- end -}}
{{- else if $asciinemaCssAsset -}}
  {{- with resources.Get $asciinemaCssAsset -}}
    {{- $asciinemaCss := . | fingerprint -}}
    
  {{- else -}}
    {{- errorf "Asciinema css asset not found at %q" $asciinemaCssAsset -}}
  {{- end -}}
{{- end -}}
{{- /* JS retrieval: get raw JS from either local asset or remote, then process */ -}}
{{- if $isRemoteBase -}}
  {{- $jsPath := cond (ne $asciinemaJsAsset "") $asciinemaJsAsset (printf "asciinema-player%s.js" $minSuffix) -}}
  {{- $asciinemaJsUrl := printf "%s/%s" $asciinemaBase $jsPath -}}
  {{- with try (resources.GetRemote $asciinemaJsUrl) -}}
    {{- with .Err -}}
      {{- errorf "Could not retrieve Asciinema js file from %s. Reason: %s." $asciinemaJsUrl . -}}
    {{- else with .Value -}}
      {{- with resources.Copy (printf "js/asciinema-player%s.js" $minSuffix) . -}}
        {{- $asciinemaJs := . | fingerprint -}}
        
      {{- end -}}
    {{- end -}}
  {{- end -}}
{{- else if $asciinemaJsAsset -}}
  {{- with resources.Get $asciinemaJsAsset -}}
    {{- $asciinemaJs := . | fingerprint -}}
    
  {{- else -}}
    {{- errorf "Asciinema js asset not found at %q" $asciinemaJsAsset -}}
  {{- end -}}
{{- end -}}