import dayjs from 'dayjs'
import 'dayjs/locale/it'
import 'dayjs/locale/en'
import 'dayjs/locale/ru'


// default presets
interface ILocale {
  dateFormat?: string,
  timeFormat?: string
}

interface ILocales {
  [key: string]: ILocale
}

const locales: ILocales = {
  en: {
    dateFormat: "MMMM YYYY, dddd",
    timeFormat: "hh:mm"
  },
  it: {
    dateFormat: "MMMM YYYY, dddd",
    timeFormat: "HH:mm"    
  },
  ru: {
    dateFormat: "MMMM YYYY, dddd",
    timeFormat: "HH:mm"
  }
}

enum FormatType {
  Date,
  Time
}

const dateTimeFormat = (formatType: FormatType, value: string, locale?: string, format?: string) => {

  // fallback in case provided value is not managed
  if(!locale || (locale && !Object.keys(locales).includes(locale))) {
    locale = Object.keys(locales)[0]

    switch(formatType) {
      case FormatType.Date:
        format = locales[locale].dateFormat
        break
      case FormatType.Time:
        format = locales[locale].timeFormat
    }
  }

  return dayjs(value)
    .locale(locale)
    .format(format)
}

const dateFormat = (value: string, locale?: string, format?: string): string =>
  dateTimeFormat(FormatType.Date, value, locale, format)

const timeFormat = (value: string, locale?: string, format?: string): string =>
  dateTimeFormat(FormatType.Time, value, locale, format)

export {
  dateFormat,
  timeFormat
}