import { useContext } from "react";
import { TimezoneContext } from "../providers/TimezoneProvider";

/**
 * Return the configured IANA timezone from the mounted TimezoneProvider.
 *
 * @throws {Error} When called outside of `<TimezoneProvider>`.
 */
export function useTimezone(): string {
  const context = useContext(TimezoneContext);
  if (context === undefined) {
    throw new Error("useTimezone must be used within <TimezoneProvider>.");
  }
  return context.timezone;
}
