import { Effect } from "dva";
import { Reducer } from "react";
import { SimpleResponse } from "@/utils/common";
import apis from "@/services";

export interface MetaDataCommonModelState {
    result: any,
}

export interface MetaDataCommonModelType {
    namespace: string;
    state: MetaDataCommonModelType;
    effects: {
        query: Effect;
        queryById: Effect;
        insert: Effect;
        remove: Effect;
        changeDeploy: Effect;
    };
    reducers: {
        save: Reducer<any, any>;
    }
}

const MetaDataCommonModel: MetaDataCommonModelType = {
    namespace: 'metadataCommon',
    state: {
        result: {},
    },
    effects: {
        *query({ payload, callback }, { call, put }) {
            const response: SimpleResponse = yield call(apis.metadataCommon.list, payload);
            if (response) {
                yield put({
                    type: 'save',
                    payload: response.result,
                });
            };
        },
        *queryById({ payload, callback }, { call }) {
            const response: SimpleResponse = yield call(apis.metadataCommon.info, payload);
            callback(response);
        },
        *insert({ payload, callback }, { call }) {
            const response: SimpleResponse = yield call(apis.metadataCommon.saveOrUpdate, payload);
            callback(response);
        },
        *remove({ payload, callback }, { call, put }) {
            const response: SimpleResponse = yield call(apis.metadataCommon.remove, payload);
            callback(response);
        },
        *deploy({ payload, callback }, { call, put }) {
            const response: SimpleResponse = yield call(apis.metadataCommon.deploy, payload);
            callback(response);
        },
        *unDeploy({ payload, callback }, { call, put }) {
            const response: SimpleResponse = yield call(apis.metadataCommon.unDeploy, payload);
            callback(response);
        }
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                result: { ...action.payload },
            }
        }
    }
}

export default MetaDataCommonModel;
