import { getLevenshteinClosest } from '@sap-cloud-sdk/generator-common/internal' ; import { VdmActionImport , VdmEntity , VdmFunctionImport , VdmParameter } from '../vdm-types' ; internal export function getODataEntity ( serviceName , vdmEntities ) { return ( getLevenshteinClosest ( serviceName , vdmEntities , x => x . className ) || getShortestNameEntity ( vdmEntities ) ) ; } internal export function getShortestNameEntity ( vdmEntities ) { return vdmEntities . sort ( ( a , b ) => a . className . length < b . className . length ? - 1 : 1 ) [ 0 ] ; } internal export function getActionFunctionImport ( serviceName , actionFunctionImports ) { if ( actionFunctionImports . length === 1 ) { return actionFunctionImports [ 0 ] ; } return ( getLevenshteinClosest ( serviceName , actionFunctionImports , x => x . name ) || getFunctionWithoutParameters ( actionFunctionImports ) || getFunctionWithMinParameters ( actionFunctionImports ) ) ; } internal export function getFunctionWithoutParameters ( actionFunctionImports ) { return actionFunctionImports . find ( func => func . parameters ?. length === 0 ) ; } param actionFunctionImports returns internal export function getFunctionWithMinParameters ( actionFunctionImports ) { const getFunctions = actionFunctionImports . filter ( func => func . httpMethod ?. toLowerCase ( ) === 'get' ) ; if ( getFunctions . length > 0 ) { actionFunctionImports = getFunctions ; } const sortedfunctions = actionFunctionImports . sort ( ( funcA , funcB ) => funcA . parameters ?. length < funcB . parameters ?. length ? - 1 : 1 ) ; return sortedfunctions [ 0 ] ; } internal export function getActionFunctionParams ( parameters ) { const paramString = parameters . slice ( 0 , 2 ) . reduce ( ( cumulator , currentParam ) => `${ cumulator }, ${ currentParam . parameterName }: '${ currentParam . parameterName }'` , '' ) ; return `{${ paramString . substring ( 1 ) }${ parameters . length > 2 ? ', ...' : '' } }` ; }