import {DateTime, Duration} from "luxon/build/es6/luxon";

export const FormatDuration = (d) => {
    const duration = Duration.fromObject({hours: 0, minutes: 0, seconds: d}).normalize().toObject()

    if (duration.hours > 1)
        return `${duration.hours} h ${duration.minutes} min`
    else if (duration.hours < 1)
        return `${duration.minutes} min`
}

export const FormatFullDuration = (d) => {
    const duration = Duration.fromObject({hours: 0, minutes: 0, seconds: d}).normalize()

    if (duration.hours > 1)
        return duration.toFormat('hh:mm:ss')
    else if (duration.hours < 1)
        return duration.toFormat('mm:ss')
}

export const FormatDate = (date) => {
    const matchDate = DateTime.fromISO(date)
    const diff = DateTime.now().diff(matchDate)

    if (diff.as('days') > 10)
        return matchDate.toLocaleString({weekday: 'short', day: '2-digit', month: '2-digit', year: 'numeric'})
    else if (diff.as('days') < 1)
        if (diff.as('hours') < 1)
            return Math.floor(diff.as('minutes')) + ' minutes ago'
        else
            return Math.floor(diff.as('hours')) + ' hours ago'
    else
        return Math.floor(diff.as('days')) + ' days ago'
}

export const FormatFullDate = (date) => {
    const matchDate = DateTime.fromISO(date)

    return matchDate.toLocaleString({
        weekday: 'short',
        day: '2-digit',
        month: '2-digit',
        year: 'numeric',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
    })
}