import dayjs from 'dayjs'
import 'dayjs/locale/ru'
import relativeTime from 'dayjs/plugin/relativeTime'
import updateLocale from 'dayjs/plugin/updateLocale'

dayjs.extend(relativeTime)
dayjs.extend(updateLocale)

dayjs.locale('ru')

dayjs.updateLocale('ru', {
	relativeTime: {
		past: '%s назад',
		s: 'несколько секунд',
		m: '1 минута',
		mm: '%d минут',
		h: '1 час',
		hh: '%d часов',
		d: '1 день',
		dd: '%d дней'
	}
})

export function isWeekPassed(fromDate: string | number | Date): boolean {
	const startDate = dayjs(fromDate)
	const dateAfter7Days = startDate.add(7, 'day')
	return dayjs().isAfter(dateAfter7Days)
}

export function fromNow(fromDate: string | number | Date): string {
	return dayjs(fromDate).fromNow()
}

export function getFormatDate(
	date: string | number | Date,
	locale: string
): string {
	const getDateFormat = (locale: string) => {
		switch (locale) {
			case 'en':
				return 'MM/DD/YYYY, hh:mm A'
			case 'ru':
			default:
				return 'DD.MM.YYYY, HH:mm'
		}
	}

	return dayjs(date).format(getDateFormat(locale))
}
