import type { StateCreator } from "zustand";
import type { AppState } from "../types";
import { THEME_KEY, readTheme } from "../helpers";

export type ThemeSlice = Pick<AppState, "theme" | "setTheme">;

export const createThemeSlice: StateCreator<AppState, [], [], ThemeSlice> = (set) =>
({
    theme: readTheme(),
    setTheme: (t) =>
    {
        if (typeof window !== "undefined")
        {
            window.localStorage.setItem(THEME_KEY, t);
        }
        set({ theme: t });
    },
});
