import type { ComponentPropsWithoutRef } from 'react'

type DateTimeProps = {
  children: string
  options?: Intl.DateTimeFormatOptions
  locale?: string
} & ComponentPropsWithoutRef<'time'>

export function DateTime({
  children,
  options,
  locale = 'pt-BR',
  ...props
}: DateTimeProps) {
  const optionsInit: Intl.DateTimeFormatOptions = {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit'
  }

  const datetime = new Intl.DateTimeFormat(locale, {
    ...optionsInit,
    ...options
  })

  return (
    <time dateTime={children} {...props}>
      {datetime.format(new Date(children))}
    </time>
  )
}
