import { query } from '@/modules';
import { message } from 'antd';
import { create } from 'zustand';

type Node = {
  id: string;
  type: string;
  data: {
    cid: string;
  };
};
type AppStore = {
  id: string;
  setId: (id: string) => void;
  getData: (id: string) => Promise<any>;
  nodes: Node[];
  data: any;
};
export const useAppStore = create<AppStore>((set, get) => ({
  id: '',
  setId: (id: string) => set({ id }),
  data: {},
  nodes: [],
  getData: async (id: string) => {
    console.log('getData');
    if (!id) {
      message.error('Invalid id');
      return;
    }
    set({ id });
    const loaded = message.loading('Loading...', 0);
    const res = await query.post({
      path: 'page',
      key: 'get',
      id,
    });
    loaded();
    if (res.code === 200) {
      console.log(res.data);
      const data = res.data;
      const nodes = data?.data?.nodes;
      set({ data: data, nodes });
    }
  },
}));
