{{ $params := . }}

{{ $alt := $params.alt }}
{{ $class := $params.class }}
{{ $crossorigin := $params.crossorigin | default "anonymous" }}
{{ $fetchpriority := $params.fetchpriority }}
{{ $fingerprint := $params.fingerprint | default true }}
{{ $height := $params.height }}
{{ $loading := $params.loading | default "lazy" }}
{{ $maxheight := $params.maxheight }}
{{ $maxwidth := $params.maxwidth }}
{{ $method := $params.method }}
{{ $options := $params.options }}
{{ $page := $params.page }}
{{ $referrerpolicy := $params.referrerpolicy | default "no-referrer" }}
{{ $resource := $params.resource }}
{{ $src := $params.src }}
{{ $style := $params.style }}
{{ $title := $params.title }}
{{ $width := $params.width }}

{{ if and (not $src) (not $resource) }}
    {{ errorf "paige/img: no src or resource" }}
{{ end }}

{{ $intrinsicheight := $height }}
{{ $intrinsicwidth := $width }}

{{ if not $resource }}
    {{ $resource = partial "paige/resource.html" (dict "page" $page "url" $src) }}
{{ end }}

{{ with $resource }}
    {{ if and $method $options }}
        {{ if eq $method "crop" }}
            {{ $resource = $resource.Crop $options }}
        {{ else if eq $method "fill" }}
            {{ $resource = $resource.Fill $options }}
        {{ else if eq $method "fit" }}
            {{ $resource = $resource.Fit $options }}
        {{ else if eq $method "resize" }}
            {{ $resource = $resource.Resize $options }}
        {{ else }}
            {{ errorf "paige/img: invalid method: %q" $method }}
        {{ end }}
    {{ end }}

    {{ $intrinsicheight = $resource.Height }}
    {{ $intrinsicwidth = $resource.Width }}

    {{ if $fingerprint }}
        {{ $resource = $resource | fingerprint }}
    {{ end }}

    {{ $src = $resource.RelPermalink }}
{{ else }}
    {{ $src = relLangURL $src }}
{{ end }}

{{ if and (or $height $maxheight) (not (or $maxwidth $width)) }}
    {{ $width = "auto" }}
{{ end }}

{{ if and (or $maxwidth $width) (not (or $height $maxheight)) }}
    {{ $height = "auto" }}
{{ end }}

{{ $styles := slice }}

{{ if $height }}
    {{ $styles = $styles | append (print "height: " $height) }}
{{ end }}

{{ if $maxheight }}
    {{ $styles = $styles | append (print "max-height: " $maxheight) }}
{{ end }}

{{ if $maxwidth }}
    {{ $styles = $styles | append (print "max-width: " $maxwidth) }}
{{ end }}

{{ if $width }}
    {{ $styles = $styles | append (print "width: " $width) }}
{{ end }}

{{ $styles = delimit $styles "; " }}

{{ if $style }}
    {{ $style = print $style "; " $styles }}
{{ else }}
    {{ $style = $styles }}
{{ end }}

<img {{ with $alt }} alt="{{ . }}" {{ end }} {{ with $class }} class="{{ . }}" {{ end }} crossorigin="{{ $crossorigin }}" {{ with $fetchpriority }} fetchpriority="{{ . }}" {{ end }} {{ with $intrinsicheight }} height="{{ . }}" {{ end }} loading="{{ $loading }}" referrerpolicy="{{ $referrerpolicy }}" src="{{ $src }}" {{ with $style }} style="{{ . | safeCSS }}" {{ end }} {{ with $title }} title="{{ . }}" {{ end }} {{ with $intrinsicwidth }} width="{{ . }}" {{ end }}>
