import { create } from 'zustand';

type DeckPageStore = {
  id: string;
  setId: (id: string) => void;
  showEdit: boolean;
  setShowEdit: (showEdit: boolean) => void;
  formData: any;
  setFormData: (formData: any) => void;
  loading: boolean;
  setLoading: (loading: boolean) => void;
  pageData: any[];
  cids: string[];
  setCids: () => void;
  setPageData: (data: any[]) => void;
  getPageData: () => any[];
  selected: any;
  setSelected: (data: any) => void;
  getSeleted: () => any;
  code: string;
  setCode: (data: string) => void;
  getCode: () => string;
};
export const useDeckPageStore = create<DeckPageStore>((set, get) => ({
  id: '',
  setId: (id) => {
    set({ id });
  },
  showEdit: false,
  setShowEdit: (showEdit) => {
    set({ showEdit });
  },
  formData: {},
  setFormData: (formData) => {
    set({ formData });
  },
  loading: false,
  setLoading: (loading) => {
    set({ loading });
  },
  pageData: [],
  setPageData: (data) => {
    set({ pageData: data });
  },
  getPageData: () => {
    return get().pageData;
  },
  selected: null,
  setSelected: (data) => {
    set({ selected: data });
  },
  getSeleted: () => {
    return get().selected;
  },
  code: '',
  setCode: (data) => {
    set({ code: data });
  },
  getCode: () => {
    return get().code;
  },
  cids: [],
  setCids: () => {
    const pageData = get().pageData;
    const cids = pageData.map((item) => item.codeId).filter((item) => item);
    // cids 去重复
    const newCids = Array.from(new Set(cids));
    set({ cids: newCids });
  },
}));
