import { createHandlerChainRunner, Handler } from "@insite/client-framework/HandlerCreator";
import { sendPageViewEvent } from "@insite/client-framework/OdpEvents/PageViewEvent";
import PageProps from "@insite/client-framework/Types/PageProps";

type HandlerType = Handler<{ page: PageProps }>;

export const SendPageView: HandlerType = props => {
    // the following required extra state being loaded which isn't always ready here
    // they have their page views events sent off from their page components
    const pageType = props.parameter.page.type;
    if (pageType === "CartPage" || pageType === "CategoryDetailsPage" || pageType === "ProductListPage") {
        return;
    }

    sendPageViewEvent(props.parameter.page, props.getState());
};

export const chain = [SendPageView];

const sendPageView = createHandlerChainRunner(chain, "SendPageView");
export default sendPageView;
