import { deepClone } from "@/helpers/object-helper"; import { partTypeStrings } from "@/constants/part-type-strings"; const recalPartTypes = [partTypeStrings.RECALIBRATION, partTypeStrings.ADAS_RECALIBRATION]; export function anyPartWithRequiresRecalFlag(lineItems) { if (!lineItems) { return false; } return lineItems.glassParts?.some((li) => li.requiresRecalibration === true); } export function isRecalPartOrHasChildRecalPart(lineItem) { if (lineItem.childParts && lineItem.childParts.length > 0) { return isRecalPart(lineItem) || containsRecalParts(lineItem.childParts); } else { return isRecalPart(lineItem); } } export function isRecalPart(lineItem) { return recalPartTypes.some((type) => lineItem.partType === type); } export function containsRecalParts(lineItems) { if (!lineItems) { return false; } if (Array.isArray(lineItems)) { return lineItems.some((li) => isRecalPartOrHasChildRecalPart(li)); } else { // complex object form -- flatten and re-call. const flattened = [ ...(lineItems.glassParts ?? []), ...(lineItems.supportingItems ?? []), ...(lineItems.vaps ?? []), ...(lineItems.promos ?? []), ]; return flattened.some((li) => isRecalPartOrHasChildRecalPart(li)); } } export function getItemsWithoutRecalParts(lineItemsArray) { if (!lineItemsArray || !Array.isArray(lineItemsArray)) return null; const firstLevelFiltered = deepClone(lineItemsArray).filter((li) => !isRecalPart(li)); const childrenFiltered = firstLevelFiltered.map((li) => { if (li.childParts && li.childParts.length > 0) { li.childParts = getItemsWithoutRecalParts(li.childParts); } return li; }); return childrenFiltered; } export function getTopLevelPartsWithRecal(lineItems) { if (!lineItems) { return null; } return lineItems.filter((li) => isRecalPartOrHasChildRecalPart(li)); } export function getRecalPartNumbers(glassPartsArray) { if (glassPartsArray && glassPartsArray.length > 0) { const topLevel = glassPartsArray.filter((gp) => isRecalPart(gp)).map((gp) => gp.partNumber); const children = glassPartsArray.map((gp) => getRecalPartNumbers(gp.childParts)).flat(); return [...topLevel, ...children]; } else { return []; } }