import { create } from 'zustand';
import { query } from '@/modules';
import { isObjectNull } from '@/utils/is-null';
import { message } from '@/modules/message';
type AppVersionStore = {
  showEdit: boolean;
  setShowEdit: (showEdit: boolean) => void;
  formData: any;
  setFormData: (formData: any) => void;
  updateByFromData: () => void;
  loading: boolean;
  setLoading: (loading: boolean) => void;
  key: string;
  setKey: (key: string) => void;
  list: any[];
  getList: () => Promise<void>;
  app: any;
  getApp: (key: string, force?: boolean) => Promise<void>;
  updateData: (data: any) => Promise<void>;
  deleteData: (id: string) => Promise<void>;
  publishVersion: (data: any) => Promise<void>;
};
export const useAppVersionStore = create<AppVersionStore>((set, get) => {
  return {
    showEdit: false,
    setShowEdit: (showEdit) => set({ showEdit }),
    formData: {},
    setFormData: (formData) => set({ formData }),
    updateByFromData: () => {
      const { formData, list } = get();
      const data = list.map((item) => {
        if (item.id === formData.id) {
          return formData;
        }
        return item;
      });
      set({ list: data });
    },
    loading: false,
    setLoading: (loading) => set({ loading }),
    key: '',
    setKey: (key) => set({ key }),
    list: [],
    getList: async () => {
      set({ loading: true });
      const key = get().key;

      const res = await query.post({
        path: 'app',
        key: 'list',
        data: {
          key,
        },
      });
      get().getApp(key);
      set({ loading: false });
      if (res.code === 200) {
        set({ list: res.data });
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    app: {},
    getApp: async (key, force) => {
      const { app } = get();
      if (!force && !isObjectNull(app)) {
        return;
      }
      const res = await query.post({
        path: 'user-app',
        key: 'get',
        data: {
          key,
        },
      });
      if (res.code === 200) {
        set({ app: res.data });
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    updateData: async (data) => {
      const { getList } = get();
      const res = await query.post({
        path: 'app',
        key: 'update',
        data,
      });
      if (res.code === 200) {
        message.success('Success');
        set({ showEdit: false, formData: res.data });
        getList();
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    deleteData: async (id) => {
      const { getList } = get();
      const res = await query.post({
        path: 'app',
        key: 'delete',
        id,
      });
      if (res.code === 200) {
        getList();
        message.success('Success');
      } else {
        message.error(res.message || 'Request failed');
      }
    },
    publishVersion: async (data) => {
      const res = await query.post({
        path: 'app',
        key: 'publish',
        data,
      });
      if (res.code === 200) {
        message.success('Success');
        get().getApp(get().key, true);
      } else {
        message.error(res.message || 'Request failed');
      }
    },
  };
});
