import { Selectable , Link } from '@sap-cloud-sdk/odata-common/internal' ; import { DeSerializers } from '../de-serializers' ; import { Entity } from '../entity' ; typeparam param selects returns internal export function getSelect < EntityT extends Entity , DeSerializersT extends DeSerializers > ( selects = [ ] ) { const select = getSelectsAsStrings ( selects ) ; return select . length ? { select : filterSelects ( select ) . join ( ',' ) } : { } ; } function selectionLevel ( select ) { return select . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ; } function filterSelects ( selects ) { const allFieldSelects = selects . filter ( select => select . endsWith ( '*' ) ) ; const selectionLevels = allFieldSelects . map ( select => selectionLevel ( select ) ) ; return [ ... allFieldSelects , ... selects . filter ( select => ! selectionLevels . includes ( selectionLevel ( select ) ) ) ] ; } function getSelectsAsStrings < EntityT extends Entity , DeSerializersT extends DeSerializers > ( selectables , initialSelect = [ ] , parent = '' ) { return selectables . reduce ( ( select , selectable ) => { const fullFieldName = getPath ( parent , selectable . _fieldName ) ; if ( selectable instanceof Link ) { if ( selectable . _selects . length ) { return getSelectsAsStrings ( selectable . _selects , select , fullFieldName ) ; } return [ ... select , `${ fullFieldName }/*` ] ; } return [ ... select , fullFieldName ] ; } , initialSelect ) ; } function getPath ( parent , fieldName ) { if ( parent ) { return `${ parent }/${ fieldName }` ; } return fieldName ; }