{{- $citationStyle := "apa" }}
{{- $partialPath := string (printf "bibliography/%s-style.html" $citationStyle) }}

{{/* -------------------- BEGIN Bibliography path -------------------- */}}
{{- $bibliographyPath := "" }}

{{/* Default: check for a JSON file in the leaf bundle. */}}
{{- $pageResource := $.Page.Resources.GetMatch "*bib*.json" -}}
{{- if $pageResource }}
{{- $constructedBibResource := printf "content/%s%s" $.Page.File.Dir $pageResource.Name }}
{{- $bibliographyPath = $constructedBibResource }}
{{- end }}

{{- /* If a `bibFile` is specified in the page front-matter, it takes precedence
  over a page resource. */ -}}
{{- /* `specifiedBib` must be relative to project root */ -}}
{{- if $.Page.Params.bibFile }}
{{- $bibliographyPath = $.Page.Params.bibFile -}}
{{- end }}
{{- $bibliography := getJSON $bibliographyPath -}}

{{- /* -------------------- END Bibliography path -------------------- */ -}}

{{ $table := .Inner | transform.Unmarshal }}
{{ $page := .}}
<div class="usa-table-container--scrollable" tabindex="0">
    <table class="usa-table usa-table--compact usa-table--striped">
    {{ with $table.caption }} <caption>{{ . | markdownify }}</caption> {{ end }}
    <thead>
        <tr>
        {{ range $index, $header := $table.headers }} 
            <th>{{ . | humanize }}</th> 
        {{ end }}
        </tr>
    </thead>
    <tbody>
        {{ range $table.rows }}
        {{ $row := . }}
        <tr>
            {{ range $header := $table.headers }}
            {{ with $v :=(index $row .) }}
                {{ if eq $header "Data Provider"}}
                    <td>
                    {{range $v}}
                        {{ if or (eq .name "unknown") (eq .name "unreleased") }}
                            {{.name}}
                        {{else}}
                        <a href={{.href}} target="_blank">{{ .name }}</a></br>
                        {{end}}
                    {{end}}
                    </td>
                {{else if eq $header "Product Name"}}
                    <td>{{.}}</td>
                {{else if eq $header "References"}}
                    <td>
                       {{ range $v}}
                       <span class="hugo-cite-intext" itemprop="citation">

                        {{- range where $bibliography "id" "eq" . -}}
                        {{- $currentRef := . -}}
                            <span class="hugo-cite-group">
                                <a href="#{{- . | urlize -}}">
                                    <span class="visually-hidden">Citation: </span>
                                    {{- /* -------------------- BEGIN Display authors -------------------- */ -}}

                                    {{- $reference := . -}}
                                    {{- $displayAuthors := $reference.author -}}
                                    {{- if not $reference.author -}}
                                        {{- $displayAuthors = $reference.editor -}}
                                    {{- end -}}
                                    {{- if not $displayAuthors -}}
                                    <span rel="noauthor">
                                      {{- i18n "apa_no_author_abbr" | default "n.a." | upper -}}
                                    </span>
                                  {{- else -}}
                                    {{- range $authorIndex, $author := $displayAuthors | first 2 -}}
                                      <span itemprop="author" itemscope itemtype="https://schema.org/Person">
                                        {{- with $author.given -}}
                                          <meta itemprop="givenName" content="{{ . }}">
                                        {{- end -}}
                                        {{- with $author.family -}}
                                          <span itemprop="familyName">{{ . | markdownify }}</span>
                                        {{- end -}}
                                      </span>
                                      {{- if and (eq $authorIndex 0) (gt (len $displayAuthors) 2) -}}
                                      ,&#32;
                                      {{- end -}}
                                      {{- if and (eq (len $displayAuthors) 2) (eq $authorIndex 0) -}}&#32;&amp;&#32;
                                      {{- end -}}
                                    {{- end -}}
                                    {{ if gt (len $displayAuthors) 2 }}
                                      <em>&amp; al.</em>
                                    {{- end -}}
                                  {{- end -}},&#32;
                                  {{- /* -------------------- END Display authors -------------------- */ -}}

                                  {{- if and (isset $reference "issued") (isset $reference.issued "date-parts") -}}
                                  {{- range $index, $dateParts := (index .issued "date-parts") -}}{{/* range of dates */}}
                                    {{- range first 1 $dateParts -}}{{/* First element in date-part is the year */ -}}
                                    <span itemprop="datePublished">
                                      {{- . -}}
                                    </span>
                                    {{- end -}}
                                  {{- end -}}
                                {{- end -}}
                                </a>
                                {{- /* Eliminate space between css-hidden citation hover block */ -}}
                                <span class="hugo-cite-citation"> {{ partial $partialPath $reference }}</span>
                            </span>
                        {{end}}
                        </span></br>
                    {{ end }}
                    </td>
                {{else}}
                    <td>{{ . }}</td>
                {{end}}
            {{ end }}
            {{ end }}
        </tr>
        {{ end }}
    </tbody>
    </table>
</div>