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;
    versionId: string;
    projectId: string;
}

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

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

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