import type { StateCreator } from "zustand";
import type { AppState } from "../types";
import { getLocale, setLocale as i18nSetLocale } from "../../i18n";

export type LocaleSlice = Pick<AppState, "locale" | "setLocale">;

// 初始值取自 i18n 模块（已从 localStorage 还原）；setLocale 同步到 i18n
// 模块（更新 currentLocale + 写 localStorage）。
export const createLocaleSlice: StateCreator<AppState, [], [], LocaleSlice> = (set) =>
({
    locale: getLocale(),
    setLocale: (l) =>
    {
        i18nSetLocale(l);
        set({ locale: l });
    },
});
