import { selectUnit } from '@formatjs/intl-utils';
import React from 'react';
import { useIntl } from 'react-intl';
import { FormattedRelativeTime } from './localization';

export default function RelativeTime({
  children: dateTimeValue,
}: {
  children: Date,
}) {
  const intl = useIntl();
  const { value, unit } = selectUnit(dateTimeValue, new Date());
  const title = `${intl.formatDate(dateTimeValue)} ${intl.formatTime(dateTimeValue)}`;
  return (
    <time title={title} dateTime={dateTimeValue.toISOString()}>
      <FormattedRelativeTime
        value={value}
        unit={unit}
      />
    </time>
  );
}
