import { upsertProductOrEventApi } from "../../../../../services/simulation/UpsertProductOrEventApi";

interface HandleAddEventToProductParams {
    event: EventsSchema | undefined;
    addEvent: (productId: string, event: EventsSchema) => void;
    selectedProduct: {
        productId: string;
        productName: string;
    }
    clearSelectedAsset?: () => void;
}

export const handleAddEventToProduct = ({
    event,
    addEvent,
    selectedProduct,
    clearSelectedAsset
}: HandleAddEventToProductParams) => {
    if (event && selectedProduct.productId) {
        addEvent(selectedProduct.productId, event);

        const email = localStorage.getItem('email')
        const organization = (email!.split("@")[1]).split(".")[0];

        upsertProductOrEventApi({
            productName: selectedProduct.productName,
            productId: selectedProduct.productId,
            organization: organization,
            eventDatas: event
        }).then((data) => {
            // console.log(data);
        })

        if (clearSelectedAsset) {
            clearSelectedAsset();
        }
    }
};
