{{/* We want to construct a plugins slice of unique plugins. We start by taking the
default plugins if they are enabled, then iterate on additional user defined
plugins.

We support the legacy style of plugin paths (although this might break in reveal-js
>= 4.0 - we might revisit this.)
*/}}

{{/* Use a scratch dict to ensure unicity of plugins. This dict has an order key
     because the plugins need to be loaded in a specific order (i.e. markdown
     depends on marked)
*/}}
{{ $startOrder := 0}}
{{ $plugins := newScratch }}
{{ if $.Param "reveal_hugo.load_default_plugins" | default true }}
  {{ $plugins.SetInMap "plugins" "RevealMarkdown" (dict "name" "RevealMarkdown" "order" 1 "source" "plugin/markdown/markdown.js") }}
  {{ $plugins.SetInMap "plugins" "RevealHighlight" (dict "name" "RevealHighlight" "order" 2 "source" "plugin/highlight/highlight.js") }}
  {{ $plugins.SetInMap "plugins" "zoom" (dict "name" "RevealZoom" "order" 3 "source" "plugin/zoom/zoom.js") }}

  <!-- always use local version of the notes plugin since HTML file it requires isn't on CDN -->
  {{ $plugins.SetInMap "plugins" "RevealNotes" (dict "name" "RevealNotes" "order" 4 "source" "reveal-js/plugin/notes/notes.js"
                                                     "verbatim" true) }}
  {{ $startOrder = 5 }}
{{ end }}

{{ $allPlugins := $.Site.Param "reveal_hugo.plugins" | default slice }}
{{ $allPlugins = append $allPlugins ($.Page.Param "reveal_hugo.plugins" | default slice ) }}

{{/*  load custom user plugins */}}
{{ range $allPlugins }}
  {{ if reflect.IsMap . }}
    {{/*  we already have a plugin definition object  */}}
    {{ with .order }}
      {{ $startOrder = . }}
    {{ end }}
    {{ $name := print .name }}
    {{ $plugins.SetInMap "plugins" $name (merge . (dict "name" $name "order" $startOrder) )}}
  {{ else if reflect.IsSlice . }}
    {{ range . }}
      {{/*  convert from old plugin path to new plugin object format  */}}
      {{ $name := path.BaseName .}}
      {{ $plugins.SetInMap "plugins" $name (dict "name" $name "source" . "order" $startOrder) }}
    {{ end }}
  {{ else }}
      {{ $name := path.BaseName .}}
      {{ $plugins.SetInMap "plugins" $name (dict "name" $name "source" . "order" $startOrder) }}
  {{ end }}

  {{ $startOrder = add $startOrder 1 }}
{{ end }}

{{ $revealLoc := dict "reveal_location" ($.Param "reveal_hugo.reveal_cdn" | default "reveal-js") }}
{{ $normalizedPlugins := slice }}

{{ range $plugin := (sort ($plugins.Get "plugins") "order") }}
  {{ $normalizedPlugin := $plugin }}

  {{ with $plugin.css }}
    {{ $normalizedCssPath := partial "internal/func/normalizePath" (merge $plugin (dict "path" . ) $revealLoc ) }}
    {{ $normalizedPlugin = merge $normalizedPlugin (dict "css" $normalizedCssPath)}}
  {{ end }}
  {{ with $plugin.source }}
    {{ $normalizedSourcePath := partial "internal/func/normalizePath" (merge $plugin (dict "path" . ) $revealLoc ) }}
    {{ $normalizedPlugin = merge $normalizedPlugin (dict "source" $normalizedSourcePath)}}
  {{ end }}

  {{ $normalizedPlugins = $normalizedPlugins | append $normalizedPlugin }}
{{ end}}

{{ return $normalizedPlugins }}
