{{- $ctx := "" -}}
{{- $isCoverCaptionEnabled := default nil -}}
{{- if (reflect.IsMap . ) -}}
  {{- /* Assign Custom Context */ -}}
  {{- $ctx = .ctx -}}
  {{- /* Assign value of .isCoverCaptionEnabled */ -}}
  {{- $isCoverCaptionEnabled = cond (not .isCoverCaptionEnabled) nil .isCoverCaptionEnabled -}}
{{- else -}}
  {{- /* Passing dot context, default behaviour */ -}}
  {{- $ctx = . -}}
  {{- /* Default isCoverCaptionEnabled for Dot Context */ -}}
  {{- $isCoverCaptionEnabled = false -}}
{{- end -}}


{{- $cover := false -}}
{{- $autoCover := default $.Site.Params.autoCover false }}

{{- if index $ctx.Params "cover" -}}
  {{- if .Resources.GetMatch $ctx.Params.Cover }}
    {{- $cover = (.Resources.GetMatch $ctx.Params.Cover).RelPermalink -}}
  {{- else -}}
    {{- $cover = absURL $ctx.Params.Cover -}}
  {{- end -}}
{{- else if $.Site.Params.AutoCover -}}
  {{- if (not $ctx.Params.Cover) -}}
    {{- if .Resources.GetMatch "cover.*" -}}
      {{- $cover = (.Resources.GetMatch "cover.*").RelPermalink -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

{{- if $cover -}}
  {{ if and $ctx.Params.coverCaption $isCoverCaptionEnabled }}
    <figure> 
      <!-- Cover image found -->
      <img src="{{ $cover }}"
      class="post-cover"
      alt="{{ .Title | plainify | default " " }}"
      title="{{ $ctx.Params.CoverCredit |plainify|default "Cover Image" }}" />
      
      <figcaption class="right">{{ $ctx.Params.coverCaption | safeHTML }}</figcaption>
    </figure>
  {{ else }}
    <!-- Cover image found -->
    <img src="{{ $cover }}"
      class="post-cover"
      alt="{{ .Title | plainify | default " " }}"
      title="{{ $ctx.Params.CoverCredit |plainify|default "Cover Image" }}" />
  {{ end }}
{{- end }}