import { create } from 'zustand';
import type { FilterState } from '../appPlazaList/store';

interface CategoryAppState extends FilterState {
    filterOpen: boolean;
    selectedSort: string;
    currentPage: number;
    pageSize: number;
    
    // Actions
    setFilterOpen: (open: boolean) => void;
    setSelectedSort: (sort: string) => void;
    setFilters: (filters: Partial<FilterState>) => void;
    setCurrentPage: (page: number) => void;
    setPageSize: (size: number) => void;
    resetFilters: () => void;
}

const initialState: FilterState & {
    filterOpen: boolean;
    selectedSort: string;
    currentPage: number;
    pageSize: number;
} = {
    category1: [],
    category2: [],
    sort: '综合排序',
    filterOpen: false,
    selectedSort: '综合排序',
    currentPage: 1,
    pageSize: 12,
};

export const useCategoryAppStore = create<CategoryAppState>((set) => ({
    ...initialState,
    
    setFilterOpen: (open) => set({ filterOpen: open }),
    
    setSelectedSort: (sort) => set({ selectedSort: sort, sort }),
    
    setFilters: (filters) => set((state) => ({
        ...state,
        ...filters,
    })),
    
    setCurrentPage: (page) => set({ currentPage: page }),
    
    setPageSize: (size) => set({ pageSize: size, currentPage: 1 }),
    
    resetFilters: () => set(initialState),
}));
