import type { LiteralUnion } from 'type-fest';

import prettyMsI18n from 'pretty-ms-i18n';
import dayjs from './dayjs';

export function formatTime(time: dayjs.ConfigType): string {
  return dayjs(time).utc().format('YYYY-MM-DD HH:mm:ss [UTC]');
}

export function getPrettyMsI18nLocale(
  locale: LiteralUnion<'en' | 'zh', string> = 'en'
): LiteralUnion<'zh_CN' | 'en', string> {
  if (locale === 'zh' || locale === 'zh_CN') {
    return 'zh_CN';
  }

  return 'en';
}

export function getSimplifyDuration(ms: number, locale: string): string {
  const options = {
    locale: getPrettyMsI18nLocale(locale),
    compact: true,
    verbose: true,
  };
  if (ms < 1000 && ms >= 0) {
    options.verbose = false;
  }
  return prettyMsI18n(ms, options);
}
