import { ApiHandler, createHandlerChainRunner } from "@insite/client-framework/HandlerCreator";
import { getJobQuote, GetJobQuoteApiParameter } from "@insite/client-framework/Services/JobQuoteService";
import { JobQuoteModel } from "@insite/client-framework/Types/ApiModels";

type HandlerType = ApiHandler<GetJobQuoteApiParameter, JobQuoteModel>;

export const DispatchBeginLoadJobQuote: HandlerType = props => {
    props.dispatch({
        type: "Data/JobQuotes/BeginLoadJobQuote",
        jobQuoteId: props.parameter.jobQuoteId,
    });
};

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

export const RequestDataFromApi: HandlerType = async props => {
    try {
        props.apiResult = await getJobQuote(props.apiParameter);
    } catch (error) {
        if ("status" in error && (error.status === 400 || error.status === 404)) {
            props.dispatch({
                type: "Data/JobQuotes/FailedToLoadJobQuote",
                jobQuoteId: props.parameter.jobQuoteId,
                status: 404,
            });
            return false;
        }
        throw error;
    }
};

export const DispatchCompleteLoadJobQuote: HandlerType = props => {
    props.dispatch({
        type: "Data/JobQuotes/CompleteLoadJobQuote",
        jobQuote: props.apiResult,
    });
};

export const chain = [
    DispatchBeginLoadJobQuote,
    PopulateApiParameter,
    RequestDataFromApi,
    DispatchCompleteLoadJobQuote,
];

const loadJobQuote = createHandlerChainRunner(chain, "LoadJobQuote");
export default loadJobQuote;
