import { useRecoilValue } from 'recoil';
import { interval } from "../../../atoms/time/index.js";

/**
 * Subscribe to an updating time interval. The argument is the period of the
 * interval in milliseconds. The return value is a JavaScript `Date` object.
 * Note that if the `now` atom has not been set the return value of this hook
 * will be `undefined`.
 *
 * This hook subscribes to the \`interval\` selector family internally, which
 * is implemented such that it will _only_ trigger a rerender at most once each
 * time the interval expires. Consequently, the general recommendation is to
 * set up frequent updates to the \`now\` atom and use this hook to access the
 * current time on whatever interval is appropriate.
 *
 * If no period is provided or if the period is invalid (less than one, falsey,
 * etc.), then the period is automatically set to `1`, which means that the
 * interval will update _each time the `now` atom updates_. In general you
 * should _always_ provide an explicit interval value.
 *
 * @param {number} period
 *   The period of the interval in milliseconds
 * @returns {Date}
 */
const useNowInterval = function useNowInterval(period) {
  return useRecoilValue(interval(period));
};
export default useNowInterval;