import { PropsWithChildren, createContext, useContext } from 'react';

/** Value for {@link TimeZoneContext} */
export interface TimeZoneContextValue {
  /** Current time zone */
  timeZone: string;
}

/** Context that returns current time zone */
const TimeZoneContext = createContext<TimeZoneContextValue | undefined>(undefined);

/** Provider that used to pass current time zone inside app */
export function TimeZoneContextProvider({ children, ...value }: PropsWithChildren<TimeZoneContextValue>) {
  return <TimeZoneContext.Provider value={value}>{children}</TimeZoneContext.Provider>;
}

/** Return current time zone context data */
export function useTimeZoneContext(): TimeZoneContextValue {
  const value = useContext(TimeZoneContext);
  if (!value) {
    throw new Error('useTimeZoneContext should be used only inside TimeZoneContextProvider');
  }

  return value;
}
