import { ApiHandler, createHandlerChainRunner } from "@insite/client-framework/HandlerCreator";
import { ServiceResult } from "@insite/client-framework/Services/ApiService";
import { AddVmiCountCollectionApiParameter, createVmiCounts } from "@insite/client-framework/Services/VmiCountsService";
import { DispatchCleanupUploadData } from "@insite/client-framework/Store/Components/OrderUpload/Handlers/CleanupUploadData";
import { VmiCountCollectionModel } from "@insite/client-framework/Types/ApiModels";

type HandlerType = ApiHandler<AddVmiCountCollectionApiParameter, ServiceResult<VmiCountCollectionModel>>;

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

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

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

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

const addVmiCounts = createHandlerChainRunner(chain, "AddVmiCounts");
export default addVmiCounts;
