---
import { SITE } from '@/consts'
import { isSubpost } from '@/lib/data-utils'
import type { CollectionEntry } from 'astro:content'

interface Props {
  post: CollectionEntry<'blog'>
}

const { post } = Astro.props

const title = post.data.title || SITE.title
const description = post.data.description || SITE.description
const fallbackOgImage = new URL('/static/1200x630.png', Astro.site).toString()
const author =
  post.data.authors && post.data.authors.length > 0
    ? post.data.authors.join(', ')
    : SITE.author
const heroImage = post.data.banner ?? post.data.image
const heroImageUrl =
  typeof heroImage === 'string'
    ? heroImage
    : heroImage?.src
      ? `${SITE.href}${heroImage.src}`
      : fallbackOgImage
---

<title>{`${title} | ${SITE.title}`}</title>
<meta name="title" content={`${title} | ${SITE.title}`} />
<meta name="description" content={description} />
<link rel="canonical" href={SITE.href} />

{isSubpost(post.id) && <meta name="robots" content="noindex" />}

<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={heroImageUrl} />
<meta property="og:image:alt" content={title} />
<meta property="og:type" content="website" />
<meta property="og:locale" content={SITE.locale} />
<meta property="og:site_name" content={SITE.title} />
<meta property="og:url" content={Astro.url} />
<meta property="og:author" content={author} />

<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={description} />
<meta property="twitter:image" content={heroImageUrl} />
<meta name="twitter:image:alt" content={title} />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:creator" content={author} />

{
  post?.data.tags &&
    post.data.tags.map((tag: string) => {
      return <meta property="article:tag" content={tag} />
    })
}
