import { GeneralizedProps , MetaKeyMap , MetaKeyMatch } from './type' ; const REQUEST_META = [ 'header' , 'query' ] ; export const getInnermostProperty = function getInnermostProperty ( propsList , propName ) { for ( let i = propsList . length - 1 ; i >= 0 ; i -- ) { const props = propsList [ i ] ; if ( props . hasOwnProperty ( propName ) ) { return props [ propName ] ; } } return null ; } ; export const getOuttermostProperty = function getOuttermostProperty ( propsList , propName ) { for ( const props of propsList ) { if ( props . hasOwnProperty ( propName ) ) { return props [ propName ] ; } } return null ; } ; export const aggKeysFromPropsList = function aggKeysFromPropsList ( propsList , propName ) { const initResult = REQUEST_META . reduce ( ( result , next ) => { const key = next as keyof MetaKeyMap ; result [ key ] = [ ] ; return result ; } , { } ) ; const res = propsList . filter ( props => usefulObject ( props [ propName ] ) ) . reduce ( ( result , next ) => { REQUEST_META . forEach ( key => { const prop = next [ propName ] ; if ( prop ?. hasOwnProperty ( key ) && usefulArray ( prop [ key ] ) ) { result [ key ] = unique ( result [ key ] . concat ( prop [ key ] ) ) ; } } ) ; return result ; } , initResult ) ; return REQUEST_META . reduce ( ( result , next ) => { const key = next as keyof MetaKeyMap ; if ( result [ key ] && result [ key ] ?. length === 0 ) { delete result [ key ] ; } return result ; } , res ) ; } ; export const aggMatchesFromPropsList = function aggMatchesFromPropsList ( propsList , propName ) { const initResult = REQUEST_META . reduce ( ( result , next ) => { const key = next as keyof MetaKeyMap ; result [ key ] = { } ; return result ; } , { } ) ; const res = propsList . filter ( props => usefulObject ( props [ propName ] ) ) . reduce ( ( result , next ) => { REQUEST_META . forEach ( key => { const prop = next [ propName ] ; if ( prop ?. hasOwnProperty ( key ) && usefulObject ( prop [ key ] ) ) { result [ key ] = Object . assign ( result [ key ] , prop [ key ] ) ; } } ) ; return result ; } , initResult ) ; return REQUEST_META . reduce ( ( result , next ) => { const key = next as keyof MetaKeyMatch ; if ( result [ key ] && Object . keys ( result [ key ] ! ) . length === 0 ) { delete result [ key ] ; } return result ; } , res ) ; } ; function unique ( arr ) { return Array . from ( new Set ( arr ) ) ; } function usefulObject ( target ) { if ( ! exist ( target ) ) { return false ; } return target . constructor === Object && Object . keys ( target ) . length > 0 ; } function usefulArray ( target ) { if ( ! exist ( target ) ) { return false ; } return Array . isArray ( target ) && target . length > 0 ; } export function exist ( target ) { return target != null ; }