---
import '../styles/global.css'
import '../styles/katex.css'
import '../styles/typography.css'

import '@fontsource/geist-sans'
import '@fontsource/geist-mono'

import { ViewTransitions } from 'astro:transitions'

interface Props {
  title: string
  description: string
  image?: string
}

const canonicalURL = new URL(Astro.url.pathname, Astro.site)

const { title, description, image = '/blog-placeholder-1.jpg' } = Astro.props
---

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="generator" content={Astro.generator} />

<link rel="canonical" href={canonicalURL} />

<title>{title}</title>
<meta name="title" content={title} />
<meta name="description" content={description} />

<meta property="og:type" content="website" />
<meta property="og:url" content={Astro.url} />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={new URL(image, Astro.url)} />

<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:url" content={Astro.url} />
<meta property="twitter:title" content={title} />
<meta property="twitter:description" content={description} />
<meta property="twitter:image" content={new URL(image, Astro.url)} />

<ViewTransitions />

<script is:inline>
  function setDarkMode(document) {
    const getThemePreference = () => {
      if (
        typeof localStorage !== 'undefined' &&
        localStorage.getItem('theme')
      ) {
        return localStorage.getItem('theme')
      }
      return window.matchMedia('(prefers-color-scheme: dark)').matches
        ? 'dark'
        : 'theme-light'
    }
    const isDark = getThemePreference() === 'dark'
    document.documentElement.classList[isDark ? 'add' : 'remove']('dark')

    if (typeof localStorage !== 'undefined') {
      const observer = new MutationObserver(() => {
        const isDark = document.documentElement.classList.contains('dark')
        localStorage.setItem('theme', isDark ? 'dark' : 'theme-light')
      })
      observer.observe(document.documentElement, {
        attributes: true,
        attributeFilter: ['class'],
      })
    }
  }

  setDarkMode(document)

  document.addEventListener('astro:before-swap', (ev) => {
    // Pass the incoming document to set the theme on it
    setDarkMode(ev.newDocument)
  })
</script>
