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

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

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

        upsertProductOrEventApi({
            productName: selectedProduct.productName,
            productUuid: selectedProduct.productUuid,
            projectId: projectId ||'',
            eventDatas: event
        }).then((data) => {
            // console.log(data);
        })

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