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

type HandlerType = Handler<{ recommendationIds: number[] }>;

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

export const CallApi: HandlerType = ({ getState, parameter: { recommendationIds } }) => {
    sendImpressionTrackingApi(peeriusSiteName(getState()), recommendationIds);
};

export const chain = [LogRequest, CallApi];

const sendImpressionTracking = createHandlerChainRunner(chain, "SendImpressionTracking");
export default sendImpressionTracking;
