import { PromoText } from '../molecules/promo-text.js'
import h from '../shared/h.js'

export function LastModified ({ _model, ...props }) {
  const { title } = _model
  const locale = document.documentElement.lang || navigator.language
  const dateModified = document.head.querySelector('meta[itemprop="dateModified"]')?.getAttribute('content')

  if (dateModified) {
    const formattedDate = new Date(dateModified).toLocaleDateString(locale, {
      year: 'numeric',
      month: 'short',
      day: 'numeric'
    })

    return h(PromoText, { _model: { text: title ? `${title} ${formattedDate}` : formattedDate }, ...props })
  } else {
    return null
  }
}
