import reduce from 'lodash/reduce' ; import { getIsTiprack , getTiprackVolume , ProtocolFile , LabwareDefinition2 , getSlotHasMatingSurfaceUnitVector , } from '@opentrons/shared-data' ; import standardDeckDef from '@opentrons/shared-data/deck/definitions/2/ot2_standard.json' ; import type { PickUpTipCommand } from '@opentrons/shared-data/protocol/types/schemaV6/command/pipetting' ; import type { Command } from '@opentrons/shared-data/protocol/types/schemaV6' ; import type { LabwareToOrder } from '../types' ; import { getLabwareLocation } from '../../utils/getLabwareLocation' ; import { getModuleInitialLoadInfo } from '../../utils/getModuleInitialLoadInfo' ; export const tipRackOrderSort = ( tiprack1 , tiprack2 ) : - 1 | 1 => { const tiprack1Volume = getTiprackVolume ( tiprack1 . definition ) ; const tiprack2Volume = getTiprackVolume ( tiprack2 . definition ) ; if ( tiprack1Volume !== tiprack2Volume ) { return tiprack1Volume > tiprack2Volume ? - 1 : 1 ; } return orderBySlot ( tiprack1 , tiprack2 ) ; } ; export const orderBySlot = ( labware1 , labware2 ) : - 1 | 1 => { if ( labware1 . slot < labware2 . slot ) { return - 1 ; } return 1 ; } ; export const getTiprackIdsInOrder = ( labware , labwareDefinitions , commands ) : string [ ] => { const unorderedTipracks = reduce < typeof labware , LabwareToOrder [ ] > ( labware , ( tipracks , currentLabware , labwareId ) => { const labwareDef = labwareDefinitions [ currentLabware . definitionId ] ; const isTiprack = getIsTiprack ( labwareDef ) ; if ( isTiprack ) { const labwareLocation = getLabwareLocation ( labwareId , commands ) ; if ( ! ( 'slotName' in labwareLocation ) ) { throw new Error ( 'expected tiprack location to be a slot' ) ; } return [ ... tipracks , { definition : labwareDef , labwareId : labwareId , slot : labwareLocation . slotName , } , ] ; } return [ ... tipracks ] ; } , [ ] ) ; const orderedTiprackIds = unorderedTipracks . sort ( tipRackOrderSort ) . map ( ( { labwareId } ) => labwareId ) ; return orderedTiprackIds ; } ; export const getAllTipracksIdsThatPipetteUsesInOrder = ( pipetteId , commands , labware , labwareDefinitions ) : string [ ] => { const pickUpTipCommandsWithPipette = commands . filter ( ( command ) : command is PickUpTipCommand => command . commandType === 'pickUpTip' ) . filter ( command => command . params . pipetteId === pipetteId ) ; const tipracksVisited = pickUpTipCommandsWithPipette . reduce < string [ ] > ( ( visited , command ) => { const tiprack = command . params . labwareId ; return visited . includes ( tiprack ) ? visited : [ ... visited , tiprack ] ; } , [ ] ) ; const orderedTiprackIds = tipracksVisited . map < LabwareToOrder > ( tiprackId => { const labwareDefId = labware [ tiprackId ] . definitionId ; const definition = labwareDefinitions [ labwareDefId ] ; const tiprackLocation = getLabwareLocation ( tiprackId , commands ) ; if ( ! ( 'slotName' in tiprackLocation ) ) { throw new Error ( 'expected tiprack location to be a slot' ) ; } return { labwareId : tiprackId , definition , slot : tiprackLocation . slotName , } ; } ) . sort ( tipRackOrderSort ) . map ( ( { labwareId } ) => labwareId ) ; return orderedTiprackIds ; } ; export const getLabwareIdsInOrder = ( labware , labwareDefinitions , modules , commands ) : string [ ] => { const unorderedLabware = reduce < typeof labware , LabwareToOrder [ ] > ( labware , ( unorderedLabware , currentLabware , labwareId ) => { const labwareDef = labwareDefinitions [ currentLabware . definitionId ] ; const isTiprack = getIsTiprack ( labwareDef ) ; const labwareLocation = getLabwareLocation ( labwareId , commands ) ; if ( ! isTiprack ) { if ( 'moduleId' in labwareLocation ) { return [ ... unorderedLabware , { definition : labwareDef , labwareId : labwareId , slot : getModuleInitialLoadInfo ( labwareLocation . moduleId , commands ) . location . slotName , } , ] ; } else { if ( ! getSlotHasMatingSurfaceUnitVector ( standardDeckDef as any , labwareLocation . slotName . toString ( ) ) ) { return [ ... unorderedLabware ] ; } } return [ ... unorderedLabware , { definition : labwareDef , labwareId : labwareId , slot : labwareLocation . slotName , } , ] ; } return [ ... unorderedLabware ] ; } , [ ] ) ; const orderedLabwareIds = unorderedLabware . sort ( orderBySlot ) . map ( ( { labwareId } ) => labwareId ) ; return orderedLabwareIds ; } ;