"use client"; import { create } from "zustand"; import { persist } from "zustand/middleware"; import type { SelectedLocation } from "@/types/location"; import type { Province } from "@/types/province"; import type { WindSpeedUnit } from "@/types/units"; import { DEFAULT_WIND_SPEED_UNIT } from "@/lib/weather-utils"; type NotificationProvinceMode = "selected" | "manual"; interface WeatherStore { favorites: string[]; selectedStationId: string | null; selectedLocation: SelectedLocation | null; warningNotificationsEnabled: boolean; morningBriefNotificationsEnabled: boolean; tomorrowBriefNotificationsEnabled: boolean; warningNotificationProvinceMode: NotificationProvinceMode; warningNotificationProvince: Province | null; windSpeedUnit: WindSpeedUnit; toggleFavorite: (id: string) => void; selectStation: (id: string) => void; selectLocation: (location: SelectedLocation) => void; setWarningNotificationsEnabled: (enabled: boolean) => void; setMorningBriefNotificationsEnabled: (enabled: boolean) => void; setTomorrowBriefNotificationsEnabled: (enabled: boolean) => void; setWarningNotificationProvinceMode: (mode: NotificationProvinceMode) => void; setWarningNotificationProvince: (province: Province | null) => void; setWindSpeedUnit: (unit: WindSpeedUnit) => void; } export const useWeatherStore = create()( persist( (set) => ({ favorites: [], selectedStationId: null, selectedLocation: null, warningNotificationsEnabled: false, morningBriefNotificationsEnabled: false, tomorrowBriefNotificationsEnabled: false, warningNotificationProvinceMode: "selected", warningNotificationProvince: null, windSpeedUnit: DEFAULT_WIND_SPEED_UNIT, toggleFavorite: (id) => set((state) => ({ favorites: state.favorites.includes(id) ? state.favorites.filter((favoriteId) => favoriteId !== id) : [...state.favorites, id], })), selectStation: (id) => set({ selectedStationId: id, selectedLocation: null }), selectLocation: (location) => set({ selectedStationId: location.stationId, selectedLocation: location }), setWarningNotificationsEnabled: (enabled) => set({ warningNotificationsEnabled: enabled }), setMorningBriefNotificationsEnabled: (enabled) => set({ morningBriefNotificationsEnabled: enabled }), setTomorrowBriefNotificationsEnabled: (enabled) => set({ tomorrowBriefNotificationsEnabled: enabled }), setWarningNotificationProvinceMode: (mode) => set({ warningNotificationProvinceMode: mode }), setWarningNotificationProvince: (province) => set({ warningNotificationProvince: province }), setWindSpeedUnit: (unit) => set({ windSpeedUnit: unit }), }), { name: "wtr:preferences" }, ), );