import { getConveyorSequencesInProduct } from "./getConveyorSequencesForProduct";

export function findConveyorInSequences(
    product: {
        productName: string;
        productId: string;
        eventDatas: EventsSchema[];
    },
    conveyorUuid: string
): {
    allSequences: EventsSchema[][][];
    parentSequence: EventsSchema[][];
    currentSubSequence: EventsSchema[];
} | null {
    // Get all conveyor sequences
    const allSequences = getConveyorSequencesInProduct(product);

    // Search through all sequences
    for (const parentSequence of allSequences) {
        for (const currentSubSequence of parentSequence) {
            for (const conveyor of currentSubSequence) {
                // Check if this is the conveyor we're looking for
                if (conveyor.modelUuid === conveyorUuid) {
                    return {
                        allSequences,
                        parentSequence,
                        currentSubSequence
                    };
                }

                // Also check points in case the UUID matches a point's conveyor
                if (conveyor.type === 'transfer') {
                    for (const point of conveyor.points) {
                        if (point.uuid === conveyorUuid) {
                            return {
                                allSequences,
                                parentSequence,
                                currentSubSequence
                            };
                        }
                    }
                }
            }
        }
    }

    // Conveyor not found
    return null;
}