import { createHandlerChainRunner, Handler } from "@insite/client-framework/HandlerCreator";
import { ProductRecommendationProductModel } 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";
import { ProductModel } from "@insite/client-framework/Types/ApiModels";

type HandlerType = Handler<{ product: ProductModel }, { recommendation: ProductRecommendationProductModel }>;

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

    props.recommendation = {
        type: "product",
        product: {
            refCode: product.productNumber,
        },
        ...generateGeneralPrepare(getState()),
    };
};

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

export const chain = [PrepareData, CallPrepareTrack];

const prepareProduct = createHandlerChainRunner(chain, "PrepareProduct");
export default prepareProduct;
