import { createContext } from "preact";
import { useContext } from "preact/compat";
import { useState } from "preact/hooks";
import { DateTime } from "luxon";

type TTimezoneContext = [string, (tz: string) => void];

const TimezoneContext = createContext(["UTC", () => {}] as TTimezoneContext);

export const TimezoneProvider = ({ children }) => {
    const [timezone, setTimezone] = useState(DateTime.now().zoneName);

    return (
        <TimezoneContext.Provider value={[timezone, setTimezone]}>
            {children}
        </TimezoneContext.Provider>
    );
};

export const useTimezone = () => useContext(TimezoneContext);
