import { create } from 'zustand';

interface DatasetManagementState {
    query: any;
    listLoading: boolean;
    list: any[];
    page: {
        pageNum: number;
        pageSize: number;
        total: number;
    };
}

interface DatasetManagementActions {
    setQuery: (query: any) => void;
    setListLoading: (loading: boolean) => void;
    setList: (list: any[]) => void;
    setPage: (page: any) => void;
}

export type DatasetManagementStore = DatasetManagementState & DatasetManagementActions & {
    fetchDatasetList: () => Promise<void>;
};

const initialState: DatasetManagementState = {
    query: undefined,
    listLoading: false,
    list: [],
    page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
    },
};

export const useDatasetManagementStore = create<DatasetManagementStore>((set, get) => ({
    ...initialState,
    
    setQuery: (query) => set({ query }),
    
    setListLoading: (loading) => set({ listLoading: loading }),
    
    setList: (list) => set({ list }),
    
    setPage: (page) => set({ page }),
    
    fetchDatasetList: async () => {
        set({ listLoading: true });
        try {
            const { query, page } = get();
            // TODO: 调用 API 获取数据集列表
            set({ list: [], page: { ...page, total: 0 } });
        } catch (error) {
            console.error('获取数据集列表失败:', error);
        } finally {
            set({ listLoading: false });
        }
    },
}));

export default useDatasetManagementStore;
