import { createEvent, OdpEvent, sendEvent } from "@insite/client-framework/OdpEvents/OdpEvent";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import { getCurrentCartState } from "@insite/client-framework/Store/Data/Carts/CartsSelector";

export const sendAddToCartEvent = (
    parameter: {
        productId: string;
        qtyOrdered: number;
    },
    state: ApplicationState,
    customize?: (odpEvent: OdpEvent) => void,
) => {
    const cart = getCurrentCartState(state);

    const odpEvent = createEvent("product", state);
    odpEvent.properties["action"] = "add_to_cart";
    odpEvent.properties["cart_identifier"] = cart.value?.trackId;
    odpEvent.properties["product_id"] = parameter.productId;
    odpEvent.properties["cart_item_quantity"] = parameter.qtyOrdered;

    customize?.(odpEvent);

    sendEvent(odpEvent, state);
};

export const sendRemoveFromCartEvent = (
    parameter: {
        productId: string;
        qtyOrdered: number;
    },
    state: ApplicationState,
    customize?: (odpEvent: OdpEvent) => void,
) => {
    const cart = getCurrentCartState(state);

    const odpEvent = createEvent("product", state);
    odpEvent.properties["action"] = "remove_from_cart";
    odpEvent.properties["cart_identifier"] = cart.value?.trackId;
    odpEvent.properties["product_id"] = parameter.productId;
    odpEvent.properties["cart_item_quantity"] = parameter.qtyOrdered;

    customize?.(odpEvent);

    sendEvent(odpEvent, state);
};
