import { flat , ODataVersion } from '@sap-cloud-sdk/util' ; import { ImportDeclarationStructure , StructureKind } from 'ts-morph' ; import voca from 'voca' ; import { VdmActionFunctionImportReturnType , VdmParameter , VdmReturnTypeCategory , VdmServiceMetadata } from '../vdm-types' ; import { odataImportDeclaration , propertyTypeImportNames , externalImportDeclarations , mergeImportDeclarations } from '../imports' ; import { isEntityNotDeserializable } from '../edmx-to-vdm/common' ; import { responseTransformerFunctionName } from './response-transformer-function' ; function actionFunctionImportDeclarations ( returnTypes , parameters , additionalImports , { oDataVersion , className } ) { const responseTransformerFunctionCommon = returnTypes . find ( returnType => isEntityNotDeserializable ( returnType ) ) ? [ 'throwErrorWhenReturnTypeIsUnionType' ] : [ ] ; const responseTransformerFunctionVersionDependent = returnTypes . filter ( returnType => ! isEntityNotDeserializable ( returnType ) ) . map ( returnType => responseTransformerFunctionName ( returnType ) ) ; const [ version , common ] = additionalImports . reduce ( ( [ v , c ] , current ) => { if ( current . version === 'common' ) { return [ v , [ ... c , current . name ] ] ; } return [ [ ... v , current . name ] , c ] ; } , [ [ ] , [ ] ] ) ; return [ ... externalImportDeclarations ( parameters ) , odataImportDeclaration ( [ ... edmRelatedImports ( returnTypes ) , ... complexTypeRelatedImports ( returnTypes ) , ... version , ... responseTransformerFunctionVersionDependent , 'DeSerializers' , 'DefaultDeSerializers' , 'defaultDeSerializers' , ... propertyTypeImportNames ( parameters ) , ... common , ... responseTransformerFunctionCommon ] , oDataVersion ) , { kind : StructureKind . ImportDeclaration , namedImports : [ voca . decapitalize ( className ) ] , moduleSpecifier : './service' } , ... returnTypeImports ( returnTypes ) ] ; } function complexTypeRelatedImports ( returnTypes ) { return returnTypes . some ( returnType => returnType . returnTypeCategory === VdmReturnTypeCategory . COMPLEX_TYPE ) ? [ 'entityDeserializer' ] : [ ] ; } function edmRelatedImports ( returnTypes ) { return returnTypes . some ( returnType => returnType . returnTypeCategory === VdmReturnTypeCategory . EDM_TYPE ) ? [ 'edmToTs' ] : [ ] ; } function returnTypeImports ( returnTypes ) { return mergeImportDeclarations ( returnTypes . filter ( returnType => returnType . returnTypeCategory !== VdmReturnTypeCategory . EDM_TYPE && returnType . returnTypeCategory !== VdmReturnTypeCategory . VOID && returnType . returnTypeCategory !== VdmReturnTypeCategory . NEVER ) . reduce ( ( imports , returnType ) => [ ... imports , ... returnTypeImport ( returnType ) ] , [ ] ) ) ; } function returnTypeImport ( returnType ) { const typeImports = [ { kind : StructureKind . ImportDeclaration , namedImports : [ returnType . returnType ] , moduleSpecifier : `./${ returnType . returnType }` } ] ; if ( returnType . returnTypeCategory === VdmReturnTypeCategory . ENTITY ) { return [ ... typeImports , { kind : StructureKind . ImportDeclaration , namedImports : [ `${ returnType . returnType }Api` ] , moduleSpecifier : `./${ returnType . returnType }Api` } ] ; } return typeImports ; } internal export function importDeclarationsFunction ( service ) { if ( ! service . actionImports ) { return [ ] ; } const actionImportPayloadElements = flat ( service . actionImports . map ( actionImport => actionImport . parameters ) ) ; const returnTypes = service . actionImports . map ( actionImport => actionImport . returnType ) ; return actionFunctionImportDeclarations ( returnTypes , actionImportPayloadElements , [ { name : 'ActionImportRequestBuilder' , version : service . oDataVersion } , { name : 'ActionImportParameter' , version : service . oDataVersion } ] , service ) ; } internal export function importDeclarationsAction ( service ) { const functionImportParameters = flat ( service . functionImports . map ( functionImport => functionImport . parameters ) ) ; const returnTypes = service . functionImports . map ( functionImport => functionImport . returnType ) ; return actionFunctionImportDeclarations ( returnTypes , functionImportParameters , [ { name : 'FunctionImportRequestBuilder' , version : service . oDataVersion } , { name : 'DeSerializers' , version : service . oDataVersion } , { name : 'FunctionImportParameter' , version : 'common' } ] , service ) ; }