import { createHandlerChainRunnerOptionalParameter, Handler } from "@insite/client-framework/HandlerCreator";
import { ProductRecommendationHomeModel } from "@insite/client-framework/Services/ProductRecommendationsService";
import prepareTrack from "@insite/client-framework/Store/Components/ProductRecommendations/Handlers/PrepareTrack";
import { generateGeneralPrepare } from "@insite/client-framework/Store/Components/ProductRecommendations/ProductRecommendationsSelector";

type HandlerType = Handler<{}, { recommendation: ProductRecommendationHomeModel }>;

export const PrepareData: HandlerType = props => {
    const { getState } = props;

    props.recommendation = {
        type: "home",
        ...generateGeneralPrepare(getState()),
    };
};

export const CallPrepareTrack: HandlerType = ({ dispatch, recommendation }) => {
    dispatch(prepareTrack({ prepareTrackData: recommendation }));
};

export const chain = [PrepareData, CallPrepareTrack];

const prepareHome = createHandlerChainRunnerOptionalParameter(chain, {}, "PrepareHome");
export default prepareHome;
