{{ $page := . }}

{{ $auto := $page.Param "paige.pages.disable_auto_schema" | not }}
{{ $pageBase := $page.Param "paige.pages.base_schema" }}
{{ $siteBase := $page.Param "paige.site.base_schema" }}

{{ range $page.Param "paige.site.schemas" }}
    <script type="application/ld+json">{{ merge $siteBase . | jsonify | safeJS }}</script>
{{ end }}

{{ range $page.Param "paige.pages.schemas" }}
    <script type="application/ld+json">{{ merge $pageBase . | jsonify | safeJS }}</script>
{{ end }}

{{ if $auto }}
    {{ $audio := $page.Params.audio | default slice }}
    {{ $authors := partial "paige/func-authors.html" $page }}
    {{ $images := $page.Params.images | default slice }}
    {{ $keywords := sort (union (union $page.Keywords $page.Params.tags) $page.Params.categories) }}
    {{ $license := $page.Param "paige.site.license" | markdownify | plainify }}
    {{ $schema := newScratch }}
    {{ $videos := $page.Params.videos | default slice }}

    {{ $schema.Set "@context" "https://schema.org" }}
    {{ $schema.Set "@type" "Article" }}
    {{ $schema.Set "url" $page.Permalink }}

    {{ with $page.Summary }}
        {{ $schema.Set "abstract" (. | plainify | chomp) }}
    {{ end }}

    {{ with $page.Description }}
        {{ $schema.Set "alternativeHeadline" (plainify .) }}
    {{ end }}

    {{ with $page.CurrentSection.Title }}
        {{ $schema.Set "articleSection" . }}
    {{ end }}

    {{ with $audio }}
        {{ $objects := slice }}

        {{ range $audio }}
            {{ $objects = $objects | append (dict "@type" "AudioObject" "url" .) }}
        {{ end }}

        {{ $schema.Set "audio" $objects }}
    {{ end }}

    {{ with $authors }}
        {{ $objects := slice }}

        {{ range $authors }}
            {{ $email := cond (. | not | not) .Params.paige.author.email "" }}
            {{ $name := cond (. | not | not) .Params.paige.author.name "" }}
            {{ $url := cond (. | not | not) .Params.paige.author.url "" }}

            {{ if or $email $name $url }}
                {{ $object := newScratch }}

                {{ with $email }}
                    {{ $object.Set "email" . }}
                {{ end }}

                {{ with $name }}
                    {{ $object.Set "name" . }}
                {{ end }}

                {{ with $url }}
                    {{ $object.Set "url" . }}
                {{ end }}

                {{ $object.Set "@type" "Person" }}
                {{ $objects = $objects | append $object.Values }}
            {{ end }}
        {{ end }}

        {{ $schema.Set "author" $objects }}
    {{ end }}

    {{ with site.Copyright }}
        {{ $schema.Set "copyrightNotice" (plainify .) }}
    {{ end }}

    {{ if and (not $page.Date.IsZero) (ne $page.Date $page.PublishDate) }}
        {{ $schema.Set "dateCreated" $page.Date }}
    {{ end }}

    {{ if not $page.Lastmod.IsZero }}
        {{ $schema.Set "dateModified" $page.Lastmod }}
    {{ end }}

    {{ with and (not $page.PublishDate.IsZero) (ne $page.Date $page.PublishDate) }}
        {{ $schema.Set "datePublished" $page.PublishDate }}
    {{ end }}

    {{ with $page.Description }}
        {{ $schema.Set "description" (plainify .) }}
    {{ end }}

    {{ if not $page.ExpiryDate.IsZero }}
        {{ $schema.Set "expiresAt" $page.ExpiryDate }}
    {{ end }}

    {{ with $page.Title }}
        {{ $schema.Set "headline" (plainify .) }}
    {{ end }}

    {{ with $images }}
        {{ $originals := slice }}
        {{ $resizes := slice }}

        {{ range $images }}
            {{ $resource := partial "paige/func-resource.html" (dict "page" $page "url" .) }}

            {{ $full := $resource.Process "webp" }}

            {{ $originals = $originals | append (dict
                "@type" "ImageObject"
                "height" $full.Height
                "url" $full.RelPermalink
                "width" $full.Width
            ) }}

            {{ $ratio := div (float $resource.Width) (float $resource.Height) }}

            {{ if ne $ratio 1.0 }}
                {{ $min := int (math.Min $resource.Width $resource.Height) }}
                {{ $spec1x1 := printf "%dx%d webp" $min $min }}
                {{ $resource1x1 := $resource.Fill $spec1x1 }}

                {{ $resizes = $resizes | append (dict
                    "@type" "ImageObject"
                    "height" $resource1x1.Height
                    "url" $resource1x1.RelPermalink
                    "width" $resource1x1.Width
                ) }}
            {{ end }}

            {{ if ne $ratio (div 4.0 3.0) }}
                {{ $spec4x3 := printf "%dx%d" }}

                {{ if gt $resource.Width $resource.Height }}
                    {{ $width := int (mul (div (float $resource.Height) 3.0) 4.0) }}

                    {{ $spec4x3 = printf "%dx%d webp" $width $resource.Height }}
                {{ else }}
                    {{ $height := int (mul (div (float $resource.Width) 4.0) 3.0) }}

                    {{ $spec4x3 = printf "%dx%d webp" $resource.Width $height }}
                {{ end }}

                {{ $resource4x3 := $resource.Fill $spec4x3 }}

                {{ $resizes = $resizes | append (dict
                    "@type" "ImageObject"
                    "height" $resource4x3.Height
                    "url" $resource4x3.RelPermalink
                    "width" $resource4x3.Width
                ) }}
            {{ end }}

            {{ if ne $ratio (div 16.0 9.0) }}
                {{ $spec16x9 := printf "%dx%d" }}

                {{ if gt $resource.Width $resource.Height }}
                    {{ $width := int (mul (div (float $resource.Height) 9.0) 16.0) }}

                    {{ $spec16x9 = printf "%dx%d webp" $width $resource.Height }}
                {{ else }}
                    {{ $height := int (mul (div (float $resource.Width) 4.0) 3.0) }}

                    {{ $spec16x9 = printf "%dx%d webp" $resource.Width $height }}
                {{ end }}

                {{ $resource16x9 := $resource.Fill $spec16x9 }}

                {{ $resizes = $resizes | append (dict
                    "@type" "ImageObject"
                    "height" $resource16x9.Height
                    "url" $resource16x9.RelPermalink
                    "width" $resource16x9.Width
                ) }}
            {{ end }}
        {{ end }}

        {{ $objects := append $resizes $originals }}

        {{ $schema.Set "image" $objects }}
    {{ end }}

    {{ with site.Language }}
        {{ $schema.Set "inLanguage" .Lang }}
    {{ end }}

    {{ with $keywords }}
        {{ $schema.Set "keywords" . }}
    {{ end }}

    {{ with $license }}
        {{ $schema.Set "license" (plainify .) }}
    {{ end }}

    {{ with $page.Title }}
        {{ $schema.Set "name" (plainify .) }}
    {{ end }}

    {{ with $page.ReadingTime }}
        {{ $schema.Set "timeRequired" (printf "PT%dM" .) }}
    {{ end }}

    {{ with $videos }}
        {{ $objects := slice }}

        {{ range $videos }}
            {{ $objects = $objects | append (dict "@type" "VideoObject" "url" .) }}
        {{ end }}

        {{ $schema.Set "video" $objects }}
    {{ end }}

    {{ with $page.WordCount }}
        {{ $schema.Set "wordCount" . }}
    {{ end }}

    <script type="application/ld+json">{{ merge $pageBase $schema.Values | jsonify | safeJS }}</script>
{{ end }}
