import { unixEOL } from '@sap-cloud-sdk/util' ; import { Import , serializeImports } from '../../generator-common' ; import { VdmEntity , VdmServiceMetadata } from '../../vdm-types' ; import { navPropertyFieldTypeImportNames , propertyFieldTypeImportNames , propertyTypeImportNames } from '../../imports' ; import { odataImport , complexTypeImports , enumTypeImports } from './imports' ; import { classContent } from './class' ; export function entityApiFile ( entity , service ) { const imports = serializeImports ( getImports ( entity , service ) ) ; const content = classContent ( entity , service ) ; return [ imports , "import { BigNumber } from 'bignumber.js';" , `import { ${ service . oDataVersion === 'v2' ? 'Moment' : 'Moment, Duration' } } from 'moment';` , content ] . join ( unixEOL ) ; } function getImports ( entity , service ) { return [ { names : [ `${ entity . className }` ] , moduleIdentifier : `./${ entity . className }` , typeOnly : false } , { names : [ `${ entity . className }RequestBuilder` ] , moduleIdentifier : `./${ entity . className }RequestBuilder` , typeOnly : false } , ... otherEntityApiImports ( entity , service ) , ... complexTypeImports ( entity . properties ) , ... enumTypeImports ( entity . properties ) , odataImport ( [ 'CustomField' , 'defaultDeSerializers' , 'DefaultDeSerializers' , 'DeSerializers' , 'mergeDefaultDeSerializersWith' , 'AllFields' , 'entityBuilder' , 'EntityBuilderType' , 'EntityApi' , 'FieldBuilder' , 'Time' , ... propertyTypeImportNames ( entity . properties ) , ... propertyFieldTypeImportNames ( entity . properties ) , ... navPropertyFieldTypeImportNames ( entity . navigationProperties , service . oDataVersion ) ] , service . oDataVersion ) ] ; } function otherEntityApiImports ( entity , service ) { return Array . from ( new Set ( entity . navigationProperties . map ( n => n . to ) ) ) . map ( to => { const matchedEntity = service . entities . find ( e => e . entitySetName === to ) ; if ( ! matchedEntity ) { throw Error ( `Failed to find the entity from the service: ${ JSON . stringify ( service ) } for entity ${ entity }` ) ; } return matchedEntity . className ; } ) . filter ( name => name !== entity . className ) . flatMap ( name => otherEntityImports ( name ) ) ; } function otherEntityImports ( name ) { return [ { names : [ `${ name }` ] , moduleIdentifier : `./${ name }` } , { names : [ `${ name }Api` ] , moduleIdentifier : `./${ name }Api` } ] ; }