"use client"

import { create } from "zustand"
import { persist } from "zustand/middleware"
import { debouncedPersistJSONStorage } from "@/lib/stores/debounced-json-storage"
import type { MailThemeMode } from "@/lib/mail-settings/types"

export const CLIENT_THEME_STORAGE_KEY = "ultisuite-client-theme"

type ClientThemeState = {
  themeMode: MailThemeMode
  setThemeMode: (mode: MailThemeMode) => void
}

export const useClientThemeStore = create<ClientThemeState>()(
  persist(
    (set) => ({
      themeMode: "system",
      setThemeMode: (themeMode) => set({ themeMode }),
    }),
    {
      name: CLIENT_THEME_STORAGE_KEY,
      storage: debouncedPersistJSONStorage,
      partialize: (s) => ({ themeMode: s.themeMode }),
    }
  )
)
