import produce from 'immer';
import reducerRegistry from '~/Utils/reducerRegistry';

export const initialState = {
    callbacks: {
        default: {
            onEdit: () => {},
            onCancelEdit: () => {},
            onSave: () => {},
            onDelete: () => {},
            onAction: (action) => {},
        }
    },
};

export default function reducer(state = initialState, action = {}) {
    return produce(state, draft => {
        switch (action.type) {
            case SET_CALLBACK:
                if(typeof draft.callbacks[action.payload.id] === 'undefined') {
                    draft.callbacks[action.payload.id] = {};
                }
                draft.callbacks[action.payload.id] = action.payload.callbacks;
                break;
            case REMOVE_CALLBACK:
                if(typeof draft.callbacks[action.payload] !== 'undefined') {
                    delete draft.callbacks[action.payload];
                }
                break;
        }
    });
}

export const SET_CALLBACK = 'GLOBAL_FORM/SET_CALLBACK';
export const REMOVE_CALLBACK = 'GLOBAL_FORM/REMOVE_CALLBACK';
export const setCallbackAction = payload => ({ type: SET_CALLBACK, payload });
export const removeCallbackAction = payload => ({ type: REMOVE_CALLBACK, payload });
