import { createHandlerChainRunner, Handler } from "@insite/client-framework/HandlerCreator";
import { sendProductRecommendationTracking as sendProductRecommendationTrackingApi } from "@insite/client-framework/Services/ProductRecommendationsService";
import log from "@insite/client-framework/Store/Components/ProductRecommendations/Handlers/Log";

type HandlerType = Handler<{ recommendationId: number; isNewPageWasOpen: boolean }>;

export const LogRequest: HandlerType = ({ dispatch, parameter: { recommendationId } }) => {
    dispatch(log({ message: "Send Product Recommendation Tracking: ", data: recommendationId }));
};

export const CallApi: HandlerType = ({ parameter: { recommendationId, isNewPageWasOpen } }) => {
    sendProductRecommendationTrackingApi(recommendationId, isNewPageWasOpen);
};

export const chain = [LogRequest, CallApi];

const sendProductRecommendationTracking = createHandlerChainRunner(chain, "SendProductRecommendationTracking");
export default sendProductRecommendationTracking;
