import { QueryOptionNames } from '../../types' ; import { Objects , Types } from '../../utils' ; import { alias , Expand , Filter , OrderBy , Select , Transform } from './builder' ; import { Connector , Expression } from './expressions' ; import type { ODataQueryArguments , ODataQueryOptions } from './options' ; import { ODataFunctions , ODataOperators } from './syntax' ; export class ODataQueryOptionHandler < T > { constructor ( private o , private n ) { } get name ( ) { return this . n ; } toJSON ( ) { return this . o [ this . n ] ; } empty ( ) { return Types . isEmpty ( this . o [ this . n ] ) ; } value ( v ? ) { return ( v !== undefined && ( this . o [ this . n ] = v ) ) || this . o [ this . n ] ; } private assertArray ( ) { if ( ! Types . isArray ( this . o [ this . n ] ) ) this . o [ this . n ] = this . o [ this . n ] !== undefined ? [ this . o [ this . n ] ] : [ ] ; return this . o [ this . n ] ; } push ( value ) { this . assertArray ( ) . push ( value ) ; } remove ( value ) { this . o [ this . n ] = this . assertArray ( ) . filter ( ( v ) => v !== value ) ; if ( this . o [ this . n ] . length === 1 ) this . o [ this . n ] = this . o [ this . n ] [ 0 ] ; } at ( index ) { return this . assertArray ( ) [ index ] ; } private assertObject ( create ) { if ( ! Types . isArray ( this . o [ this . n ] ) && Types . isPlainObject ( this . o [ this . n ] ) ) { return this . o [ this . n ] ; } let arr = this . assertArray ( ) ; let obj = arr . find ( ( v ) => Types . isPlainObject ( v ) ) ; if ( ! obj && create ) { obj = { } ; arr . push ( obj ) ; } return obj ; } set ( path , value ) { let obj = this . assertObject ( true ) ; Objects . set ( obj , path , value ) ; } get ( path , def ? ) { let obj = this . assertObject ( false ) || { } ; return Objects . get ( obj , path , def ) ; } unset ( path ) { let obj = this . assertObject ( true ) ; Objects . unset ( obj , path ) ; if ( Types . isArray ( this . o [ this . n ] ) ) { this . o [ this . n ] = this . o [ this . n ] . filter ( ( v ) => ! Types . isEmpty ( v ) ) ; if ( this . o [ this . n ] . length === 1 ) this . o [ this . n ] = this . o [ this . n ] [ 0 ] ; } } has ( path ) { let obj = this . assertObject ( false ) || { } ; return Objects . has ( obj , path ) ; } assign ( values ) { let obj = this . assertObject ( true ) ; return Objects . merge ( obj , values ) ; } clear ( ) { delete this . o [ this . n ] ; } } export class ODataQueryOptionsHandler < T > { constructor ( protected options ) { } link param value param name returns alias ( value , name ? ) { return alias ( value , name ) ; } select ( opts ) ; select ( ) ; select ( opts ? ) { return this . options . option < Select < T > > ( QueryOptionNames . select , opts ) ; } expand ( opts ) ; expand ( ) ; expand ( opts ? ) { return this . options . option < Expand < T > > ( QueryOptionNames . expand , opts ) ; } link compute ( opts ) ; compute ( opts ) ; compute ( ) ; compute ( opts ? ) { if ( Types . isFunction ( opts ) ) { return this . options . expression ( QueryOptionNames . compute , opts ( { e : ( ) => Expression . e < T > ( ) , } ) as Expression < T > ) ; } return this . options . option < string > ( QueryOptionNames . compute , opts ) ; } link format ( opts ) ; format ( ) ; format ( opts ? ) { return this . options . option < string > ( QueryOptionNames . format , opts ) ; } transform ( opts ) ; transform ( ) ; transform ( opts ? ) { return this . options . option < Transform < T > > ( QueryOptionNames . transform , opts ) ; } search ( opts ) ; search ( ) ; search ( opts ? ) { return this . options . option < string > ( QueryOptionNames . search , opts ) ; } filter ( opts ) ; filter ( opts ) ; filter ( ) ; filter ( opts ? ) { if ( Types . isFunction ( opts ) ) { return this . options . expression ( QueryOptionNames . filter , opts ( { s : Expression . s < any > ( ) as T , e : Expression . e , o : Expression . o < T > ( ) , f : Expression . f < T > ( ) , } ) as Expression < T > ) ; } return this . options . option < Filter < T > > ( QueryOptionNames . filter , opts ) ; } orderBy ( opts ) ; orderBy ( opts ) ; orderBy ( ) ; orderBy ( opts ? ) { if ( Types . isFunction ( opts ) ) { return this . options . expression ( QueryOptionNames . orderBy , opts ( { e : ( ) => Expression . e < T > ( ) , } ) as Expression < T > ) ; } return this . options . option < OrderBy < T > > ( QueryOptionNames . orderBy , opts ) ; } top ( opts ) ; top ( ) ; top ( opts ? ) { return this . options . option < number > ( QueryOptionNames . top , opts ) ; } skip ( opts ) ; skip ( ) ; skip ( opts ? ) { return this . options . option < number > ( QueryOptionNames . skip , opts ) ; } skiptoken ( opts ) ; skiptoken ( ) ; skiptoken ( opts ? ) { return this . options . option < string > ( QueryOptionNames . skiptoken , opts ) ; } paging ( { skip , skiptoken , top , } = { } ) { if ( skiptoken !== undefined ) this . skiptoken ( skiptoken ) ; else if ( skip !== undefined ) this . skip ( skip ) ; if ( top !== undefined ) this . top ( top ) ; } clearPaging ( ) { this . skip ( ) . clear ( ) ; this . top ( ) . clear ( ) ; this . skiptoken ( ) . clear ( ) ; } apply ( query ) { if ( query . select !== undefined ) { this . select ( query . select ) ; } if ( query . expand !== undefined ) { this . expand ( query . expand ) ; } if ( query . transform !== undefined ) { this . transform ( query . transform ) ; } if ( query . search !== undefined ) { this . search ( query . search ) ; } if ( query . filter !== undefined ) { this . filter ( query . filter ) ; } if ( query . orderBy !== undefined ) { this . orderBy ( query . orderBy ) ; } this . paging ( query ) ; } }