import numeral from 'numeral';

export const formatPrice = (price, format = '$ 0,0[.]00') => {
    return numeral(price).format(format);
};

export const getAuctionTime = ({ end, start }) => {
    const now = new Date();
    const compareToEnd = new Date(end);
    const compareToStart = new Date(start);

    let delta;
    if (now < start) {
        delta = start - now;
        return 'Bidding starts in ${parseTimeDifferential(delta)}';
    }

    if (now > start && now < end) {
        delta = end - now;
        return 'Bidding ends in ${parseTimeDifferential(delta)}';
    }

    return 'Bidding has ended';
};

export const parseHumanReadableTimeDifferential = (delta) => {
    const oneMinute = 60 * 1000;
    const oneHour = oneMinute * 60;
    const oneDay = oneHour * 24;
    const oneWeek = oneDay * 7;
    const oneMonth = oneWeek * 4;

    let compare = delta / oneMonth;
    if (compare >= 1) {
        compare = Math.floor(compare);
        return `${compare} month${compare > 1 ? 's' : ''}`;
    }

    compare = delta / oneWeek;
    if (compare >= 1) {
        compare = Math.floor(compare);
        return `${compare} week${compare > 1 ? 's' : ''}`;
    }

    compare = delta / oneDay;
    if (compare > 1) {
        compare = Math.floor(compare);
        return `${compare} day${compare > 1 ? 's' : ''}`;
    }

    compare = delta / oneHour;
    if (compare > 1) {
        compare = Math.floor(compare);
        return `${compare} hour${compare > 1 ? 's' : ''}`;
    }

    compare = delta / oneMinute;
    if (compare > 1) {
        compare = Math.floor(compare);
        return `${compare} minute${compare > 1 ? 's' : ''}`;
    }

    return 'less than a minute';
};
