import { OdpEvent } from "@insite/client-framework/OdpEvents/OdpEvent";
import { sendPageViewEvent } from "@insite/client-framework/OdpEvents/PageViewEvent";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import { getCurrentCartState } from "@insite/client-framework/Store/Data/Carts/CartsSelector";
import PageProps from "@insite/client-framework/Types/PageProps";

export const sendCartPageViewEvent = (
    page: PageProps,
    state: ApplicationState,
    customize?: (odpEvent: OdpEvent) => void,
) => {
    sendPageViewEvent(page, state, odpEvent => {
        const cart = getCurrentCartState(state)?.value;
        odpEvent.properties["action"] = "cart";
        odpEvent.properties["cart_identifier"] = cart?.trackId;

        const cartDetails = cart?.cartLines?.map(o => ({
            product_id: o.productId,
            quantity: o.qtyOrdered,
        }));

        odpEvent.properties["cart_product_details"] = JSON.stringify(cartDetails);

        customize?.(odpEvent);
    });
};
