import axios from "axios";
import { create } from "zustand";
import type { Modules } from "../../-types/modules";

interface SettingState {
	modules: Modules[];

	fetchModules: () => Promise<void>;
	setModules: (modules: Modules[]) => void;
}
interface FetchModulesResponse {
	data: Modules[];
}

export const useModuleStore = create<SettingState>()((set) => ({
	modules: [],
	setModules: (modules) => set({ modules }),
	fetchModules: async () => {
		try {
			//const response = await axios.get<{data: Setting[]}>(`${process.env.NEXT_PUBLIC_URL}/api/settings/client`);
			const response = await axios.get(`/lst/api/system/modules`, {});
			const data: FetchModulesResponse = response.data; //await response.json();
			//console.log(data);
			set({ modules: data.data });
		} catch (error) {
			console.error("Failed to fetch modules:", error);
			set({ modules: [] });
		}
	},
}));
