/** * Shared helpers for displaying component/machine structure skeleton details. * * Extracted from pages/component/create.vue and pages/component/[id]/edit.vue * where these functions were duplicated verbatim. */ // --------------------------------------------------------------------------- // Structure accessors // --------------------------------------------------------------------------- type StructureLike = Record | null export const getStructureCustomFields = (structure: StructureLike): any[] => { return Array.isArray(structure?.customFields) ? structure.customFields : [] } export const getStructurePieces = (structure: StructureLike): any[] => { return Array.isArray(structure?.pieces) ? structure.pieces : [] } export const getStructureProducts = (structure: StructureLike): any[] => { return Array.isArray(structure?.products) ? structure.products : [] } export const getStructureSubcomponents = (structure: StructureLike): any[] => { if (Array.isArray(structure?.subcomponents)) { return structure.subcomponents } const legacy = (structure as any)?.subComponents return Array.isArray(legacy) ? legacy : [] } // --------------------------------------------------------------------------- // Label resolvers // --------------------------------------------------------------------------- export const resolvePieceLabel = ( piece: Record, labelMap: Record = {}, ): string => { const parts: string[] = [] if (piece.role) { parts.push(piece.role) } if (piece.typePiece?.name) { parts.push(piece.typePiece.name) } else if (piece.typePieceLabel) { parts.push(piece.typePieceLabel) } else if (piece.typePieceId && labelMap[piece.typePieceId]) { parts.push(labelMap[piece.typePieceId]!) } else if (piece.typePiece?.code) { parts.push(`Famille ${piece.typePiece.code}`) } else if (piece.familyCode) { parts.push(`Famille ${piece.familyCode}`) } else if (piece.typePieceId) { parts.push(`#${piece.typePieceId}`) } return parts.length ? parts.join(' • ') : 'Pièce' } export const resolveProductLabel = ( product: Record, labelMap: Record = {}, ): string => { const parts: string[] = [] if (product.role) { parts.push(product.role) } if (product.typeProduct?.name) { parts.push(product.typeProduct.name) } else if (product.typeProductLabel) { parts.push(product.typeProductLabel) } else if (product.typeProductId && labelMap[product.typeProductId]) { parts.push(labelMap[product.typeProductId]!) } else if (product.typeProduct?.code) { parts.push(`Catégorie ${product.typeProduct.code}`) } else if (product.familyCode) { parts.push(`Catégorie ${product.familyCode}`) } else if (product.typeProductId) { parts.push(`#${product.typeProductId}`) } return parts.length ? parts.join(' • ') : 'Produit' } export const resolveSubcomponentLabel = (node: Record): string => { const parts: string[] = [] if (node.alias) { parts.push(node.alias) } if (node.typeComposant?.name) { parts.push(node.typeComposant.name) } else if (node.typeComposantLabel) { parts.push(node.typeComposantLabel) } else if (node.familyCode) { parts.push(node.familyCode) } else if (node.typeComposantId) { parts.push(`#${node.typeComposantId}`) } const childCount = Array.isArray(node.subcomponents) ? node.subcomponents.length : Array.isArray(node.subComponents) ? node.subComponents.length : 0 if (childCount) { parts.push(`${childCount} sous-composant(s)`) } return parts.length ? parts.join(' • ') : 'Sous-composant' } // --------------------------------------------------------------------------- // Generic model type name fetcher (replaces fetchPieceTypeNames / fetchProductTypeNames) // --------------------------------------------------------------------------- export const fetchModelTypeNames = async ( ids: string[], existingMap: Record, get: (url: string) => Promise<{ success?: boolean; data?: any }>, ): Promise> => { const missing = ids.filter((id) => id && !existingMap[id]) if (!missing.length) { return {} } const results = await Promise.allSettled( missing.map((id) => get(`/model_types/${id}`)), ) const additions: Record = {} results.forEach((result, index) => { const key = missing[index] if (!key || result.status !== 'fulfilled') { return } const data = result.value?.data const name = data?.name || data?.code if (name) { additions[key] = name } }) return additions } // --------------------------------------------------------------------------- // Type label map builder // --------------------------------------------------------------------------- export const buildTypeLabelMap = ( types: any[], fetchedOverrides: Record = {}, ): Record => ({ ...Object.fromEntries( (types || []) .filter((type: any) => type?.id) .map((type: any) => [type.id, type.name || type.code || '']), ), ...fetchedOverrides, })