import { useMemo } from 'react';
import useDateFormat from "../../useDateFormat/index.js";
import useNowInterval from "../useNowInterval/index.js";
import useIsMounted from "../../useIsMounted/index.js";
const useFormattedTimestamp = function useFormattedTimestamp(timestamp, opts = {}) {
  const {
    format = 'relative',
    allowFuture = false
  } = opts;
  const isMounted = useIsMounted();
  const formatOpts = isMounted ? {} : {
    timeZone: 'America/New_York'
  };
  const formatted = useDateFormat(timestamp, formatOpts);
  const now = useNowInterval(30000);
  return useMemo(() => {
    if (!now) return formatted;
    if (format !== 'relative') return formatted;
    if (!allowFuture || now > new Date(timestamp)) {
      const diffMs = now - new Date(timestamp);

      // Handle minutes-old formats
      const diffMins = Math.round(diffMs / (1000 * 60));
      if (diffMins < 1) return 'just now';
      if (diffMins < 2) return '1 minute ago';
      if (diffMins < 60) return `${diffMins} minutes ago`;

      // Handle hours-old formats
      const diffHrs = Math.round(diffMins / 60);
      const hr = diffHrs === 1 ? 'hour' : 'hours';
      if (diffHrs < 24) return `${diffHrs} ${hr} ago`;
    } else {
      const diffMs = new Date(timestamp) - now;

      // Handle formats for events happening in minutes
      const diffMins = Math.round(diffMs / (1000 * 60));
      if (diffMins < 2) return 'in 1 minute';
      if (diffMins < 60) return `in ${diffMins} minutes`;

      // Handle formats for events happening in hours
      const diffHrs = Math.round(diffMins / 60);
      const hr = diffHrs === 1 ? 'hour' : 'hours';
      if (diffHrs < 24) return `in ${diffHrs} ${hr}`;
    }

    // Fall back to full timestamp
    return formatted;
  }, [formatted, now]);
};
export default useFormattedTimestamp;