import {
    createHandlerChainRunner,
    HandlerWithResult,
    HasOnSuccess,
    markSkipOnCompleteIfOnSuccessIsSet,
} from "@insite/client-framework/HandlerCreator";
import { updateQuoteLine as updateQuoteLineApi } from "@insite/client-framework/Services/QuoteService";
import { QuoteLineModel } from "@insite/client-framework/Types/ApiModels";

interface UpdateQuoteLineResult {
    quoteLine?: QuoteLineModel;
    errorMessage?: string;
}

type HandlerType = HandlerWithResult<
    {
        quoteId: string;
        quoteLineId: string;
        quoteLine: QuoteLineModel;
    } & HasOnSuccess,
    UpdateQuoteLineResult
>;

export const DispatchBeginApplyQuote: HandlerType = props => {
    props.dispatch({
        type: "Pages/RfqQuoteDetails/BeginApplyQuote",
    });
};

export const RequestUpdateQuoteLine: HandlerType = async props => {
    await updateQuoteLineApi({
        quoteId: props.parameter.quoteId,
        quoteLineId: props.parameter.quoteLineId,
        quoteLine: props.parameter.quoteLine,
    });
};

export const DispatchCompleteApplyQuote: HandlerType = props => {
    props.dispatch({
        type: "Pages/RfqQuoteDetails/CompleteApplyQuote",
    });
};

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

export const ResetCartData: HandlerType = props => {
    props.dispatch({
        type: "Data/Carts/ResetCart",
        id: props.parameter.quoteId,
    });
};

export const ExecuteOnSuccessCallback: HandlerType = props => {
    markSkipOnCompleteIfOnSuccessIsSet(props);
    props.parameter.onSuccess?.();
};

export const chain = [
    DispatchBeginApplyQuote,
    RequestUpdateQuoteLine,
    DispatchCompleteApplyQuote,
    ResetQuotesData,
    ResetCartData,
    ExecuteOnSuccessCallback,
];

const updateQuoteLine = createHandlerChainRunner(chain, "UpdateQuoteLine");
export default updateQuoteLine;
