import { createSelector } from 'reselect' ; import head from 'lodash/head' ; import uniqWith from 'lodash/uniqWith' ; import { getLabwareDisplayName , getModuleDisplayName , } from '@opentrons/shared-data' ; import { selectors as robotSelectors } from '../../robot' ; import { matchesLabwareIdentityForQuantity , matchesLabwareIdentityForCalibration , formatCalibrationData , } from './utils' ; import type { State } from '../../types' ; import type { LabwareCalibration } from '../types' ; import type { LabwareSummary , BaseProtocolLabware } from './types' ; export const getLabwareCalibrations = ( state , robotName ) : LabwareCalibration [ ] => { return state . calibration [ robotName ] ?. labwareCalibrations ?. data ?? [ ] ; } ; export const getProtocolLabwareList = createSelector ( ( state , robotName ) => robotSelectors . getLabware ( state ) , getLabwareCalibrations , ( state , robotName ) => robotSelectors . getModulesBySlot ( state ) , ( protocolLabware , calibrations , modulesBySlot ) => { return protocolLabware . map ( lw => { const baseLabware = { ... lw , loadName : lw . definition ?. parameters . loadName ?? lw . type , namespace : lw . definition ?. namespace ?? null , version : lw . definition ?. version ?? null , parent : modulesBySlot [ lw . slot ] ?. model ?? null , calibrationData : null , } ; const calData = calibrations . filter ( calibration => matchesLabwareIdentityForCalibration ( calibration , baseLabware ) ) . map ( formatCalibrationData ) ; return { ... baseLabware , calibrationData : head ( calData ) ?? null , } ; } ) ; } ) ; export const getUniqueProtocolLabwareSummaries = createSelector ( getProtocolLabwareList , getLabwareCalibrations , ( baseLabwareList , calibrations ) => { const uniqueLabware = uniqWith < BaseProtocolLabware > ( baseLabwareList , matchesLabwareIdentityForQuantity ) ; return uniqueLabware . map ( lw => { const { definition : def , loadName , parent , calibrationData , definitionHash , } = lw ; const displayName = def ? getLabwareDisplayName ( def ) : loadName ; const parentDisplayName = parent ? getModuleDisplayName ( parent ) : null ; const quantity = baseLabwareList . filter ( t => matchesLabwareIdentityForQuantity ( t , lw ) ) . length ; const summary = { displayName , parentDisplayName , quantity , calibration : calibrationData , calDataAvailable : definitionHash !== null , } ; return summary ; } ) ; } ) ;