import { ApiHandler, createHandlerChainRunner } from "@insite/client-framework/HandlerCreator";
import { ServiceResult } from "@insite/client-framework/Services/ApiService";
import { AddVmiCountApiParameter, postVmiCount } from "@insite/client-framework/Services/VmiCountsService";
import { VmiCountModel } from "@insite/client-framework/Types/ApiModels";

type HandlerType = ApiHandler<AddVmiCountApiParameter, ServiceResult<VmiCountModel>>;

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

export const RequestDataFromApi: HandlerType = async props => {
    props.apiResult = await postVmiCount(props.apiParameter);
};

export const DispatchResetVmiBins: HandlerType = props => {
    if (props.apiResult.successful) {
        props.dispatch({
            type: "Data/VmiBins/Reset",
        });
    }
};

export const chain = [PopulateApiParameter, RequestDataFromApi, DispatchResetVmiBins];

const addVmiCount = createHandlerChainRunner(chain, "AddVmiCount");
export default addVmiCount;
