import groupBy from 'lodash/groupBy' ; import type { LabwareDefinition1 , LabwareDefinition2 , } from '@opentrons/shared-data' ; const labwareSchemaV1DefsContext = require . context ( '@opentrons/shared-data/labware/definitions/1' , true , /\.json$/ , 'sync' ) ; let labwareSchemaV1Defs = null ; function getLegacyLabwareDefs ( ) { if ( ! labwareSchemaV1Defs ) { labwareSchemaV1Defs = labwareSchemaV1DefsContext . keys ( ) . map ( ( name ) => labwareSchemaV1DefsContext ( name ) ) ; } return labwareSchemaV1Defs as Readonly < LabwareDefinition1 [ ] > ; } export function getLegacyLabwareDef ( loadName ) { const def = getLegacyLabwareDefs ( ) . find ( d => d . metadata . name === loadName ) ; return def || null ; } const labwareSchemaV2DefsContext = ( require as any ) . context ( '@opentrons/shared-data/labware/definitions/2' , true , /\.json$/ , 'sync' ) ; let labwareSchemaV2Defs = null ; function getLatestLabwareDefs ( ) { if ( ! labwareSchemaV2Defs ) { const allDefs = labwareSchemaV2DefsContext . keys ( ) . map ( ( name ) => labwareSchemaV2DefsContext ( name ) ) ; const labwareDefGroups = groupBy ( allDefs , d => `${ d . namespace }/${ d . parameters . loadName }` ) ; labwareSchemaV2Defs = Object . keys ( labwareDefGroups ) . map ( ( groupKey ) => { const group = labwareDefGroups [ groupKey ] ; const allVersions = group . map ( d => d . version ) ; const highestVersionNum = Math . max ( ... allVersions ) ; const resultIdx = group . findIndex ( d => d . version === highestVersionNum ) ; return group [ resultIdx ] ; } ) ; } return labwareSchemaV2Defs ; } export function getLatestLabwareDef ( loadName ) { const def = getLatestLabwareDefs ( ) . find ( d => d . parameters . loadName === loadName ) ; return def || null ; }