{{- /*
    Renders a work citation with semantic markup, called using the `cite` shortcode.

    @params cite  URI for work. Optional.
    @params citeStyle Force a citation style. Optional.
    @params href  URI for citation, or 'asCite' to use value of `cite`. Optional.
    @params schemaType The Schema.org `itemtype` value. Default 'CreativeWork'.
    @params shortTitle  Abbreviated display title. Optional.
    @params title Title of the work to display.
    @params titleLang Language code of a foreign-language work's title. Optional.
    @params titleTr English translation of a foreign-language work's title. Optional.
*/ -}}

<!-- Optional parameter overrides (for nested citations) -->

{{- $cite := default .Params.cite ( .Scratch.Get "cite" ) -}}
{{- $title := default .Params.title ( .Scratch.Get "title" ) -}}
{{- $titleLang := default .Params.titleLang ( .Scratch.Get "titleLang" ) -}}
{{- $titleTr := default .Params.titleTr ( .Scratch.Get "titleTr" ) -}}
{{- $schemaType := default .Params.schemaType ( .Scratch.Get "schemaType" ) -}}

{{- if ( strings.Contains $cite "#" ) -}}
  {{- erroridf "cite-with-fragment" "Cite parameter includes fragment identifier, which probably isn't what you want." -}}
  {{- $cite = delimit ( split $cite "#" ) "" -}}
{{- end -}}

<!-- Validation -->

{{- if ( and ( isset .Params "citeStyle" ) ( not ( in ( slice "inherit" "enquote" "normal" ) .Params.citeStyle ) ) ) -}}
  {{- erroridf "invalid-citeStyle" "Invalid `citeStyle` value %q for %q" .Params.citeStyle $title -}}
{{- end -}}

<!-- TODO: check `.Params.href` is URI or 'asCite' -->

{{- if ( and $schemaType ( not ( in $.Site.Data.itemtypes $schemaType ) ) ) -}}
  {{- erroridf "invalid-schemaType" "Invalid Schema.org type value %q for %q" $schemaType $title -}}
{{- end -}}

<!-- TODO: check `.Params.cite` is URI -->

{{- if ( or $titleLang $titleTr ) -}}
  {{- if ( not ( and $titleLang $titleTr ) ) -}}
    {{- erroridf "invalid-foreign-title" "Must have both title translation and language set for foreign language title %q" $title -}}
  {{- end -}}
{{- end -}}

<!-- Rendering -->

{{- $itemType := default "CreativeWork" $schemaType -}}
{{- if ( or ( in $.Site.Data.itemtypes $itemType ) ( eq .Params.suppress "true" ) ) -}}
  {{- with .Params.href -}}
  <a href="{{ if eq . "asCite" }}{{ $cite }}{{ else }}{{ . }}{{ end }}">
  {{- end -}}
    <cite
      class="cite{{ with .Params.citeStyle }} cite--{{ . }}{{ end }}"
      itemscope
      itemprop="citation"
      itemtype="https://schema.org/{{- $itemType -}}"
      {{- if $titleLang -}}
      lang="{{- $titleLang -}}"
      title="{{- $titleTr -}}"
      {{- end -}}>
      {{- with $cite -}}
      <meta itemprop="url" content="{{ . }}">
      {{- end -}}
      <span itemprop="name">
        {{- if $titleLang -}}
          <i
            lang="{{ $titleLang }}"
            title="
              {{- if $.Params.shortTitle -}}
                {{- printf "%s [%s]" $title $titleTr | plainify -}}
              {{- else -}}
                {{- $titleTr | plainify -}}
              {{- end -}}
            "
          >
        {{- end -}}

        {{- ( default $title .Params.shortTitle ) | markdownify | safeHTML -}}

        {{- if $titleLang -}}
          </i>
        {{- end -}}
      </span>{{- /**/ -}}
    </cite>
  {{- with .Params.href -}}
  </a>
  {{- end -}}
{{- else -}}
  {{- erroridf "invalid-schema-type" "Invalid Schema.org type value %q for %q" $itemType $title -}}
{{- end -}}