<!-- Styles -->
{{ $defaultTheme := .Param "defaultTheme" | default "light" }}

{{ $critical := sort (resources.Match "css/critical/*.css") "Name" | resources.Concat nil | resources.ExecuteAsTemplate nil . | resources.PostCSS }}
{{ $nonCritical := sort (resources.Match "css/non-critical/*.css") "Name" | resources.Concat "css/non-critical.css" | resources.ExecuteAsTemplate "css/non-critical.css" . | resources.PostCSS }}
{{ $prismDark := resources.Get "prism-themes/prism-coldark-dark.css" }}
{{ $prismLight := resources.Get "prism-themes/prism-coldark-dark.css" }}

{{ if hugo.IsProduction }}
  {{ $critical = $critical | resources.PostProcess }}
  {{ $nonCritical = $nonCritical | fingerprint "sha512" | resources.PostProcess }}
  {{ $prismDark = $prismDark | minify | fingerprint "sha512" }}
  {{ $prismLight = $prismLight | minify | fingerprint "sha512" }}
{{ end }}

<style>
  /* stylelint-disable */
  {{ $critical.Content | safeCSS }}
  /* stylelint-enable */
</style>

<link
  rel="preload"
  href="{{ $nonCritical.RelPermalink }}"
  as="style"
  onload="this.onload=null;this.rel='stylesheet'"
/>

<!-- TODO Use prefetch when more widely supported -->
<link
  id="prism-dark"
  rel="preload"
  href="{{ $prismDark.RelPermalink }}"
  as="style"
  onload="this.onload=null;this.rel='stylesheet'"
  {{ if eq $defaultTheme "light" }}disabled{{ end }}
/>

<!-- TODO Use prefetch when more widely supported -->
<link
  id="prism-light"
  rel="preload"
  href="{{ $prismLight.RelPermalink }}"
  as="style"
  onload="this.onload=null;this.rel='stylesheet'"
  {{ if eq $defaultTheme "dark" }}disabled{{ end }}
/>

<noscript>
  {{ if eq $defaultTheme "light" }}
    <link
      rel="stylesheet"
      href="{{ $prismLight.RelPermalink }}"
    />
  {{ else }}
    <link
      rel="stylesheet"
      href="{{ $prismDark.RelPermalink }}"
    />
  {{ end }}


  <link
    rel="stylesheet"
    href="{{ $nonCritical.RelPermalink }}"
  />
</noscript>
