import { ApiHandler, createHandlerChainRunner } from "@insite/client-framework/HandlerCreator";
import {
    deleteVmiLocations,
    DeleteVmiLocationsApiParameter,
} from "@insite/client-framework/Services/VmiLocationService";
import { VmiLocationCollectionModel } from "@insite/client-framework/Types/ApiModels";

type HandlerType = ApiHandler<DeleteVmiLocationsApiParameter, VmiLocationCollectionModel>;

export const DispatchBeginRemoveVmiLocations: HandlerType = props => {
    props.dispatch({
        type: "Data/VmiLocations/BeginRemoveVmiLocations",
    });
};

export const PopulateApiParameter: HandlerType = props => {
    props.apiParameter = { ...props.parameter };
};

export const RemoveDataFromApi: HandlerType = async props => {
    await deleteVmiLocations(props.apiParameter);
};

export const DispatchClearDataView: HandlerType = props => {
    props.dispatch({
        type: "Data/VmiLocations/Reset",
    });
    props.dispatch({
        type: "Data/Orders/Reset",
    });
};

export const chain = [PopulateApiParameter, DispatchBeginRemoveVmiLocations, RemoveDataFromApi, DispatchClearDataView];

const removeVmiLocations = createHandlerChainRunner(chain, "RemoveVmiLocations");
export default removeVmiLocations;
