@@ -2,7 +2,7 @@ import { getAddressableAreasInProtocol, getDeckDefFromRobotType } from '.'
22import { FLEX_ROBOT_TYPE } from '../constants'
33import {
44 getAddressableAreaFromSlotId ,
5- getMainNonComboFixtureId ,
5+ getMainFixtureIdForAA ,
66} from '../fixtures'
77
88import type { AddressableAreaName , CutoutFixtureId , CutoutId } from '../../deck'
@@ -44,29 +44,34 @@ export function getSimplestDeckConfigForProtocol(
4444) : CutoutConfigProtocolSpec [ ] {
4545 // TODO(BC, 2023-11-06): abstract out the robot type
4646 const deckDef = getDeckDefFromRobotType ( FLEX_ROBOT_TYPE )
47-
4847 const addressableAreas =
4948 protocolAnalysis != null
5049 ? getAddressableAreasInProtocol ( protocolAnalysis , deckDef )
5150 : [ ]
51+ // iterates through the list of required addressable areas for the protocol
5252 const simplestDeckConfig = addressableAreas . reduce <
5353 CutoutConfigProtocolSpec [ ]
5454 > ( ( acc , addressableArea ) => {
55+ // finds all cutout fixtures that provide this addressable area
5556 const cutoutFixturesForAddressableArea = getCutoutFixturesForAddressableAreas (
5657 [ addressableArea ] ,
5758 deckDef . cutoutFixtures
5859 )
60+ // grabs the cutout id for the addressable area
5961 const cutoutIdForAddressableArea = getCutoutIdForAddressableArea (
6062 addressableArea ,
6163 cutoutFixturesForAddressableArea
6264 )
65+ // grabs all possible cutout fixtures for that cutout id
6366 const cutoutFixturesForCutoutId =
6467 cutoutIdForAddressableArea != null
6568 ? getCutoutFixturesForCutoutId (
6669 cutoutIdForAddressableArea ,
6770 deckDef . cutoutFixtures
6871 )
6972 : null
73+ // this grabs the previously found CutoutConfig if we've already added one to our acc
74+ // for a different addressable area required by the same CutoutId
7075 const existingCutoutConfig = acc . find (
7176 cutoutConfig => cutoutConfig . cutoutId === cutoutIdForAddressableArea
7277 )
@@ -76,32 +81,29 @@ export function getSimplestDeckConfigForProtocol(
7681 cutoutFixturesForCutoutId != null &&
7782 cutoutIdForAddressableArea != null
7883 ) {
79- const indexOfExistingFixture = cutoutFixturesForCutoutId . findIndex (
80- ( { id } ) => id === existingCutoutConfig . cutoutFixtureId
81- )
84+ // finds what index in our acc that the `existingCutoutConfig` is at so that
85+ // we know which item to swap out with a new CutoutConfig that provides the previously
86+ // and newly found addressable areas
8287 const accIndex = acc . findIndex (
8388 ( { cutoutId } ) => cutoutId === cutoutIdForAddressableArea
8489 )
90+ // what addressable areas we've already looped through and added to this cutout
8591 const previousRequiredAAs = acc [ accIndex ] ?. requiredAddressableAreas
8692 const allNextRequiredAddressableAreas =
8793 previousRequiredAAs != null &&
8894 previousRequiredAAs . includes ( addressableArea )
8995 ? previousRequiredAAs
9096 : [ ...previousRequiredAAs , addressableArea ]
91-
97+ // check for the next compatible fixture for the new, longer list of addressable areas
9298 const nextCompatibleCutoutFixture = getSimplestFixtureForAddressableAreas (
9399 cutoutIdForAddressableArea ,
94100 allNextRequiredAddressableAreas ,
95101 cutoutFixturesForCutoutId
96102 )
97- const indexOfCurrentFixture = cutoutFixturesForCutoutId . findIndex (
98- ( { id } ) => id === nextCompatibleCutoutFixture ?. id
99- )
100103
101- if (
102- nextCompatibleCutoutFixture != null &&
103- indexOfCurrentFixture > indexOfExistingFixture
104- ) {
104+ // this logic swaps out the newly found cutoutfixture id with the existing one
105+ // that was added for the last addressable area we referenced
106+ if ( nextCompatibleCutoutFixture != null ) {
105107 return [
106108 ...acc . slice ( 0 , accIndex ) ,
107109 {
@@ -192,7 +194,7 @@ export function getSimplestFixtureForAddressableAreas(
192194 cutoutFixturesForCutoutId
193195 )
194196 if ( nextCompatibleCutoutFixtures . length > 1 ) {
195- const mainFixture = getMainNonComboFixtureId (
197+ const mainFixture = getMainFixtureIdForAA (
196198 nextCompatibleCutoutFixtures . map ( cf => cf . id ) ,
197199 requiredAddressableAreas ,
198200 cutoutId
0 commit comments