import { createHandlerChainRunner, Handler } from "@insite/client-framework/HandlerCreator";
import { sendCategoryPageViewEvent } from "@insite/client-framework/OdpEvents/CategoryPageViewEvent";
import { CatalogPage } from "@insite/client-framework/Services/CategoryService";
import { getCurrentPage } from "@insite/client-framework/Store/Data/Pages/PageSelectors";

type Parameter = {
    catalogPage?: CatalogPage;
};

type HandlerType = Handler<Parameter>;

export const SendCategoryPageView: HandlerType = props => {
    if (props.parameter.catalogPage) {
        const page = getCurrentPage(props.getState());
        sendCategoryPageViewEvent(page, props.parameter.catalogPage, undefined, props.getState());
    }
};

export const chain = [SendCategoryPageView];

const sendCategoryPageView = createHandlerChainRunner(chain, "SendCategoryPageView");
export default sendCategoryPageView;
