import i18n from '@config/i18n';

export const getTimeRemaining = (endsAt: Date) => {
  if (!endsAt) return '';

  const now = new Date();
  const endDate = new Date(endsAt);
  const diffMs = endDate.getTime() - now.getTime();

  if (diffMs <= 0) return '';

  const diffMinutes = Math.floor(diffMs / (1000 * 60));
  const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
  const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));

  if (diffDays > 0) {
    return i18n.t('post.ends_in_days', {
      count: diffDays,
    });
  } else if (diffHours > 0) {
    return i18n.t('post.ends_in_hours', {
      count: diffHours,
    });
  } else {
    return i18n.t('post.ends_in_minutes', {
      count: diffMinutes,
    });
  }
};
