var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { differenceInHours, differenceInMinutes, differenceInSeconds, formatDistanceStrict, formatISO, lightFormat, } from 'date-fns';
import React, { useState } from 'react';
import { useInterval } from '../../hooks/useInterval';
import { makeStyles } from '../../shared/makeStyles';
export const meridiem = /[A|P]M$/;
const endsWithMeridiem = (formattedTime) => meridiem.test(formattedTime);
const Now = 'now';
const shortFormat = 'h:mm a';
const shortForm = (dateTime) => lightFormat(dateTime, shortFormat);
const longFormat = `M/d/yyyy ${shortFormat}`;
const longForm = (dateTime) => lightFormat(dateTime, longFormat);
/**
 * for the fp hard-liners:
 *
 * ```ts
 * const formatTimeFp = (dateTime: number) =>
 *   differenceInSeconds(Date.now(), dateTime) < 5
 *     ? 'Now'
 *     : differenceInSeconds(Date.now(), dateTime) < 60
 *     ? `< ${formatDistanceToNowStrict(dateTime, {
 *         roundingMethod: 'ceil',
 *         unit: 'minute',
 *       }).replace(/ute/, '')} Ago`
 *     : differenceInMinutes(Date.now(), dateTime) < 60
 *     ? `${formatDistanceToNowStrict(dateTime, {
 *         roundingMethod: 'ceil',
 *         unit: 'minute',
 *       }).replace(/ute/, '')} Ago`
 *     : lightFormat(dateTime, 'h:mm a');
 * ```
 */
const formatTime = (dateTime) => {
    const now = Date.now();
    const seconds = differenceInSeconds(now, dateTime);
    const minutes = differenceInMinutes(now, dateTime);
    const toNow = formatDistanceStrict(now, dateTime, {
        roundingMethod: 'ceil',
        unit: 'minute',
    }).replace(/ute/, '');
    if (seconds < 5) {
        // less than 5 seconds is "Now"
        return Now;
    }
    if (seconds < 30) {
        // less than 30 seconds is "< 1 Min Ago"
        return `< ${toNow} ago`;
    }
    if (seconds < 60 || minutes < 60) {
        // less than 60 seconds or minutes is "${n} Mins Ago"
        return `${toNow} ago`;
    }
    // greater than that and we just drop to "meridiem" time
    return differenceInHours(now, dateTime) < 24
        ? shortForm(dateTime)
        : longForm(dateTime);
};
const getPeriod = (formattedTime) => {
    if (endsWithMeridiem(formattedTime)) {
        return null;
    }
    if (formattedTime === Now || formattedTime.startsWith('<')) {
        return 5000;
    }
    return 30000;
};
const useStyles = makeStyles()(() => ({
    TimeAgo: {
        display: 'inline-block',
    },
}));
export const TimeAgo = (_a) => {
    var { className, dateTime } = _a, props = __rest(_a, ["className", "dateTime"]);
    const [formattedTime, setFormattedTime] = useState(formatTime(dateTime));
    const { classes, cx } = useStyles();
    useInterval(() => setFormattedTime(formatTime(dateTime)), getPeriod(formattedTime));
    return (React.createElement("time", Object.assign({ dateTime: formatISO(dateTime), title: longForm(dateTime), className: cx(className, classes.TimeAgo) }, props), formattedTime));
};
//# sourceMappingURL=TimeAgo.js.map