{{ $currentNode := . }}

{{ $_ := newScratch }}
{{ $debug := (slice) }}

{{ $sandnameSingular := (slice)   }}
{{ $sandnamePlural := (slice)  }}

{{ range $k, $v := $.Site.Data.sandpointsnamegraph }}
  {{ $sandnameSingular = $sandnameSingular | append $k }}
  {{ $sandnamePlural = $sandnamePlural | append $v }}
{{ end  }}

{{ $pPath := "" }}
{{ range $p := .Site.RegularPages }}
  {{ $pPath = printf "/%s" $p.File }}
  {{ $pageDir := "" }}
  {{ with $p.File }}
    {{ $pageDir = (substr .Dir 0 -1)}}
  {{ end }}

  {{ $content := $p.Content }}
  {{ with $links := $p.Scratch.Get "links" }}
    {{ range $link := $links }}
      {{ $linkPath := printf "/%s" $link.File}}
      {{ with ($_.Get $linkPath) }}
        {{ $_.SetInMap $linkPath "backlinks" (uniq ((index ($_.Get $linkPath) "backlinks") | append $p)) }}
      {{ else }}
        {{ $_.SetInMap $linkPath "backlinks" (slice $p) }}
      {{ end }}
    {{ end }}
  {{ end }}

  {{ $paramsKeys := (slice) }}
  {{ range $k, $v := $p.Params  }}
    {{ $paramsKeys = $paramsKeys | append $k  }}
  {{ end  }}
  
  {{ $mentions := intersect $paramsKeys $sandnamePlural }}
  {{ range $mention := $mentions  }}
    {{ range $m := index $p.Params $mention }}
      {{ $mentionLink := printf "/%s/%s" (substr $mention 0 -1) $m }}
      {{ with ($_.Get $mentionLink)  }}
        {{ $p.Scratch.Add "links" (uniq (($p.Scratch.Get "links")| append ($.GetPage $mentionLink))) }}
        {{ $_.SetInMap $mentionLink "backlinks" (uniq ((index ($_.Get $mentionLink) "backlinks") | append $p)) }}
      {{ else }}
        {{ $p.Scratch.Add "links" (uniq (($p.Scratch.Get "links")| append ($.GetPage $mentionLink))) }}
        {{ $_.SetInMap $mentionLink "backlinks" (slice $p) }} 
      {{ end  }}
    {{ end  }}
  {{ end  }}

  {{ $gp := $.GetPage $pPath }}
  {{ $gpPath := printf "/%s" $gp.File }}
  {{ range $k, $v := $gp.Params }}
    {{ with in $k "has_" }}
      {{ $s := index (split $k "has_") 1 }}
      {{ range $sin,$plur := $currentNode.Site.Data.sandpointsnamegraph }}
        {{ if eq $s $plur }}
          {{ range index $gp.Params $k }}
            {{ $cp := $.GetPage (printf "/%s/%s" $sin .) }}
            {{ $cpPath := printf "/%s" $cp.File }}
            {{ range $kcp, $vcp := $cp.Params }}
              {{ with in $kcp "has_" }}
                {{ $scp := index (split $kcp "has_") 1 }}
                {{ range $sincp,$plurcp := $currentNode.Site.Data.sandpointsnamegraph }}
                  {{ if eq $scp $plurcp }}

                    {{ $_.SetInMap $gpPath "depth" "core" }}
                    {{ $_.SetInMap $gpPath "child_depth" "mantle" }}
                    {{ $_.SetInMap $gpPath "has" $s }}

                    {{ with $gp.File }}
                      {{ $core := substr .Dir 0 -1 }}
                      {{ with index ($_.Get $gpPath) "core" }}
                        {{ $_.SetInMap $gpPath "core" (uniq ((index ($_.Get $gpPath) "core") | append $core)) }}
                      {{ else }}
                        {{ $_.SetInMap $gpPath "core" (slice $core) }}
                      {{ end }}
                      {{ $cor := $_.Get $cpPath }}
                      {{ with $cor.core }}
                        {{ $_.SetInMap $cpPath "core" (uniq ((index ($_.Get $cpPath) "core") | append $core)) }}
                      {{ else }}
                        {{ $_.SetInMap $cpPath "core" (slice $core) }}
                      {{ end }}
                    {{ end }}

                    {{ with $cp.File }}
                      {{ $mantle := substr .Dir 0 -1 }}
                      {{ with index ($_.Get $gpPath) "mantle" }}
                        {{ $_.SetInMap $gpPath "mantle" (uniq ((index ($_.Get $gpPath) "mantle") | append $mantle)) }}
                      {{ else }}
                        {{ $_.SetInMap $gpPath "mantle" (slice $mantle) }}
                      {{ end }}
                      {{ with index ($_.Get $cpPath) "mantle" }}
                        {{ $_.SetInMap $cpPath "mantle" (uniq ((index ($_.Get $cpPath) "mantle") | append $mantle)) }}
                      {{ else }}
                        {{ $_.SetInMap $cpPath "mantle" (slice $mantle) }}
                      {{ end }}
                    {{ end }}

                    {{ with $_.Get $gpPath }}
                      {{ if .has_children }}
                        {{ $hasChildren := index . "has_children" | append $cp }}
                        {{ $_.SetInMap $gpPath "has_children" (uniq $hasChildren) }}
                      {{ else }}
                        {{ $_.SetInMap $gpPath "has_children" (slice $cp) }}
                      {{ end }}
                    {{ end }}

                    {{ $_.SetInMap $cpPath "depth" "mantle" }}
                    {{ $_.SetInMap $cpPath "child_depth" "crust" }}
                    {{ $_.SetInMap $cpPath "plural" $plur }}
                    {{ $_.SetInMap $cpPath "has" $scp }}

                    {{ if isset $cp.Params $kcp }}
                      {{ $_.SetInMap $cpPath "has_children" (slice) }}
                      {{ with index ($_.Get $cpPath) "is_in" }}
                        {{ $isincore := index ($_.Get $cpPath) "is_in" | append $gp}}
                        {{ $_.SetInMap $cpPath "is_in" (uniq $isincore) }}
                      {{ else }}
                        {{ $_.SetInMap $cpPath "is_in" (slice $gp) }}
                      {{ end }}
                    {{ end }}

                    {{ range index $cp.Params $kcp }}
                      {{ $gcp := $.GetPage (printf "/%s/%s" $sincp .) }}
                      {{ $gcpPath := printf "/%s" $gcp.File }}

                      {{ with $_.Get $cpPath }}
                        {{ if .has_children }}
                          {{ $hasGChildren := index . "has_children" | append $gcp }}
                          {{ $_.SetInMap $cpPath "has_children" (uniq $hasGChildren) }}
                        {{ else }}
                          {{ $_.SetInMap $cpPath "has_children" (slice $gcp) }}
                        {{ end }}
                      {{ end }}

                      {{ $_.SetInMap $gcpPath "depth" "crust" }}
                      {{ $_.SetInMap $gcpPath "plural" $plurcp }}
                      {{ $_.SetInMap $gcpPath "in" (slice $sin $plur) }}

                      {{ with $gp.File }}
                        {{ $core := substr .Dir 0 -1 }}
                        {{ with index ($_.Get $gcpPath) "core" }}
                          {{ $_.SetInMap $gcpPath "core" (uniq ((index ($_.Get $gcpPath) "core") | append $core)) }}
                        {{ else }}
                          {{ $_.SetInMap $gcpPath "core" (slice $core) }}
                        {{ end }}
                      {{ end }}

                      {{ with $cp.File }}
                        {{ $mantle := substr .Dir 0 -1 }}
                        {{ with index ($_.Get $gcpPath) "mantle" }}
                          {{ $_.SetInMap $gcpPath "mantle" (uniq ((index ($_.Get $gcpPath) "mantle") | append $mantle)) }}
                        {{ else }}
                          {{ $_.SetInMap $gcpPath "mantle" (slice $mantle) }}
                        {{ end }}
                      {{ end }}

                      {{ with $gcp.File }}
                        {{ $crust := substr .Dir 0 -1 }}
                        {{ with index ($_.Get $gpPath) "crust" }}
                          {{ $_.SetInMap $gpPath "crust" (uniq ((index ($_.Get $gpPath) "crust") | append $crust)) }}
                        {{ else }}
                          {{ $_.SetInMap $gpPath "crust" (slice $crust) }}
                        {{ end }}
                        {{ with index ($_.Get $cpPath) "crust" }}
                          {{ $_.SetInMap $cpPath "crust" (uniq ((index ($_.Get $cpPath) "crust") | append $crust)) }}
                        {{ else }}
                          {{ $_.SetInMap $cpPath "crust" (slice $crust) }}
                        {{ end }}
                        {{ with index ($_.Get $gcpPath) "crust" }}
                          {{ $_.SetInMap $gcpPath "crust" (uniq ((index ($_.Get $gcpPath) "crust") | append $crust)) }}
                        {{ else }}
                          {{ $_.SetInMap $gcpPath "crust" (slice $crust) }}
                        {{ end }}
                      {{ end }}

                      {{ with $_.Get $gcpPath }}
                        {{ if .is_in }}
                          {{ $isinmantle := index . "is_in" | append $cp}}
                          {{ $_.SetInMap $gcpPath "is_in" (uniq $isinmantle) }}
                        {{ else }}
                          {{ $_.SetInMap $gcpPath "is_in" (slice $cp) }}
                        {{ end }}
                      {{ end }}
                    {{ end }}
                  {{ end }}
                {{ end }}
              {{ end }}
            {{ end }}
          {{ end }}
        {{ end }}
      {{ end }}
    {{ end }}
  {{ end }}

  {{ $_.SetInMap $pPath "debug" (printf "%#v" $debug) }}
{{ end }}

{{ return $_ }}
