{{ $params := . }}

{{ $alt := $params.alt }}
{{ $breakpoints := $params.breakpoints }}
{{ $class := $params.class }}
{{ $densities := $params.densities }}
{{ $fetchpriority := $params.fetchpriority }}
{{ $height := $params.height }}
{{ $link := $params.link }}
{{ $linked := $params.linked }}
{{ $loading := $params.loading }}
{{ $maxheight := $params.maxheight }}
{{ $maxwidth := $params.maxwidth }}
{{ $page := $params.page }}
{{ $process := $params.process }}
{{ $resource := $params.resource }}
{{ $sizes := $params.sizes }}
{{ $src := $params.src }}
{{ $srcset := $params.srcset }}
{{ $style := $params.style }}
{{ $title := $params.title }}
{{ $width := $params.width }}

{{ $intrinsicheight := "" }}
{{ $intrinsicwidth := "" }}

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

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

{{ if $style }}
    {{ $mapped := slice }}

    {{ range split $style ";" }}
        {{ $s := . | strings.TrimLeft " " | strings.TrimRight " " }}

        {{ if not $s }}
            {{ continue }}
        {{ end }}

        {{ $mapped = $mapped | append $s }}
    {{ end }}

    {{ $style = $mapped }}
{{ else }}
    {{ $style = slice }}
{{ end }}

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

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

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

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

{{ $style = delimit ($style | uniq | sort) "; " }}

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

{{ if $resource }}
    {{ $method := "" }}
    {{ $options := slice }}
    {{ $quality := "" }}
    {{ $size := "" }}
    {{ $smallerresource := $resource }}

    {{ if $process }}
        {{ range split (lower (cond (eq $process "default") "" $process)) " " }}
            {{ if or (eq . "crop") (eq . "fill") (eq . "fit") (eq . "resize") }}
                {{ $method = . }}
            {{ else }}
                {{ with findRE `q\d+` . 1 }}
                    {{ $quality = index . 0 }}
                {{ else }}
                    {{ with findRE `\d+x\d+|\d+x|x\d+` . 1 }}
                        {{ $size = index . 0 }}
                    {{ else }}
                        {{ $options = $options | append . }}
                    {{ end }}
                {{ end }}
            {{ end }}
        {{ end }}

        {{ $options = delimit $options " " }}

        {{ if not $method }}
            {{ $method = "resize" }}
        {{ end }}

        {{ if not $size }}
            {{ $size = print $resource.Width "x" $resource.Height }}
        {{ end }}

        {{ $smalleroptions := print $size " " $quality " " $options }}

        {{ if eq $method "crop" }}
            {{ $smallerresource = $resource.Crop $smalleroptions }}
        {{ else if eq $method "fill" }}
            {{ $smallerresource = $resource.Fill $smalleroptions }}
        {{ else if eq $method "fit" }}
            {{ $smallerresource = $resource.Fit $smalleroptions }}
        {{ else if eq $method "resize" }}
            {{ $smallerresource = $resource.Resize $smalleroptions }}
        {{ end }}
    {{ end }}

    {{ $smallerresource = $smallerresource | fingerprint }}
    {{ $intrinsicheight = $smallerresource.Height }}
    {{ $intrinsicwidth = $smallerresource.Width }}
    {{ $src = $smallerresource.Permalink }}

    {{ if and (not $sizes) (not $srcset) }}
        {{ $partialresource := "" }}

        {{ if and $method (or $breakpoints $densities) (ne $method "resize") }}
            {{ $partialoptions := print $size " q100 " $options }}

            {{ if eq $method "crop" }}
                {{ $partialresource = $resource.Crop $partialoptions }}
            {{ else if eq $method "fill" }}
                {{ $partialresource = $resource.Fill $partialoptions }}
            {{ else if eq $method "fit" }}
                {{ $partialresource = $resource.Fit $partialoptions }}
            {{ end }}
        {{ end }}

        {{ if $densities }}
            {{ $densities = split $densities " " }}
            {{ $srcset = slice }}

            {{ $parseddensities := slice }}

            {{ range $densities }}
                {{ with findRE `^(\d+(\.\d+)?x|\.\d+x)$` . 1 }}
                    {{ $parseddensities = $parseddensities | append (strings.TrimSuffix "x" (index . 0) | float) }}
                {{ else }}
                    {{ errorf "layouts/partials/paige/image.html: invalid pixel density: %q" . }}
                {{ end }}
            {{ end }}

            {{ $parseddensities = $parseddensities | uniq | sort }}

            {{ if lt (len $parseddensities) 2 }}
                {{ errorf "layouts/partials/paige/image.html: must have at least two unique pixel densities" }}
            {{ end }}

            {{ $base := 0 }}
            {{ $maxdensity := index $parseddensities (sub (len $parseddensities) 1) }}

            {{ with $partialresource }}
                {{ $base = div .Width $maxdensity }}
            {{ else }}
                {{ $base = div $smallerresource.Width $maxdensity }}
            {{ end }}

            {{ range $parseddensities }}
                {{ if eq . $maxdensity }}
                    {{ continue }}
                {{ end }}

                {{ $imagewidth := mul $base . | math.Round | int }}
                {{ $resized := "" }}

                {{ with $partialresource }}
                    {{ $resized = .Resize (print $imagewidth "x " $quality " " $options) }}
                {{ else }}
                    {{ $resized = $smallerresource.Resize (print $imagewidth "x q100 " $options) }}
                {{ end }}

                {{ $resized = $resized | fingerprint }}
                {{ $srcset = $srcset | append (printf "%s %gx" $resized.RelPermalink .) }}
            {{ end }}

            {{ $srcset = $srcset | append (printf "%s %gx" $smallerresource.RelPermalink $maxdensity) }}
            {{ $srcset = delimit $srcset ", " | string }}
        {{ else if $breakpoints }}
            {{ $sizes = slice }}
            {{ $srcset = slice }}

            {{ $maxwidth := 0 }}

            {{ range slice (slice 550 576) (slice 696 768) (slice 936 992) (slice 1116 1200) (slice 1296 1400) }}
                {{ $imagewidth := index . 0 }}
                {{ $viewwidth := index . 1 }}

                {{ if gt $imagewidth $smallerresource.Width }}
                    {{ continue }}
                {{ end }}

                {{ $resized := "" }}

                {{ with $partialresource }}
                    {{ $resized = .Resize (print $imagewidth "x " $quality " " $options) }}
                {{ else }}
                    {{ $resized = $smallerresource.Resize (print $imagewidth "x q100 " $options) }}
                {{ end }}

                {{ $resized = $resized | fingerprint }}
                {{ $maxwidth = math.Max $maxwidth $imagewidth }}
                {{ $sizes = $sizes | append (printf "(max-width: %dpx) %dpx" $viewwidth $imagewidth) }}
                {{ $srcset = $srcset | append (printf "%s %dw" $resized.RelPermalink $imagewidth) }}
            {{ end }}

            {{ if $sizes }}
                {{ $sizes = $sizes | append (print $maxwidth "px") }}
                {{ $sizes = delimit $sizes ", " }}
                {{ $srcset = delimit $srcset ", " }}
            {{ end }}
        {{ end }}
    {{ end }}
{{ else }}
    {{ $src = absLangURL $src }}
{{ end }}

{{ $content := partial "paige/img.html" (dict
    "alt" $alt
    "class" $class
    "fetchpriority" $fetchpriority
    "height" $intrinsicheight
    "loading" $loading
    "sizes" $sizes
    "src" $src
    "srcset" $srcset
    "style" $style
    "title" $title
    "width" $intrinsicwidth
) }}

{{ if $linked }}
    {{ if ne $linked "unprocessed" }}
        {{ $method := "" }}
        {{ $options := slice }}
        {{ $size := "" }}

        {{ range split (lower (cond (eq $linked "default") "" $linked)) " " }}
            {{ if or (eq . "crop") (eq . "fill") (eq . "fit") (eq . "resize") }}
                {{ $method = . }}
            {{ else }}
                {{ with findRE `\d+x\d+|\d+x|x\d+` . 1 }}
                    {{ $size = index . 0 }}
                {{ else }}
                    {{ $options = $options | append . }}
                {{ end }}
            {{ end }}
        {{ end }}
        
        {{ $options = delimit $options " " }}

        {{ if not $method }}
            {{ $method = "resize" }}
        {{ end }}

        {{ if not $size }}
            {{ $size = print $resource.Width "x" $resource.Height }}
        {{ end }}

        {{ $options = print $size " " $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 }}
        {{ end }}
    {{ end }}

    {{ $resource = $resource | fingerprint }}
    {{ $content = partial "paige/a.html" (dict "content" $content "href" $resource.RelPermalink "page" $page) }}
{{ else if $link }}
    {{ $content = partial "paige/a.html" (dict "content" $content "href" $link "page" $page) }}
{{ end }}

<span class="paige-image">{{ $content }}</span>
