import { SubModules } from "@/types/modules";
import axios from "axios";
import { create } from "zustand";

interface SettingState {
    subModules: SubModules[];

    fetchSubModules: () => Promise<void>;
    setSubModules: (modules: SubModules[]) => void;
}
interface FetchModulesResponse {
    data: SubModules[];
}

export const useSubModuleStore = create<SettingState>()((set) => ({
    subModules: [],
    setSubModules: (subModules) => set({ subModules }),
    fetchSubModules: async () => {
        try {
            //const response = await axios.get<{data: Setting[]}>(`${process.env.NEXT_PUBLIC_URL}/api/settings/client`);
            const response = await axios.get(`/api/server/submodules`, {});
            const data: FetchModulesResponse = response.data; //await response.json();
            //console.log(data);
            set({ subModules: data.data });
        } catch (error) {
            console.error("Failed to fetch settings:", error);
            set({ subModules: [] });
        }
    },
}));
