import { ConseilQuery , ConseilOperator , ConseilSortDirection , ConseilFunction , ConseilOutput } from "../types/conseil/QueryTypes" ; export namespace ConseilQueryBuilder { /**
     * Creates an empty ConseilQuery object with limit set to 100.
     */ export function blankQuery ( ) { return { fields : [ ] , predicates : [ ] , orderBy : [ ] , aggregation : [ ] , limit : 100 } ; } param query param fields export function addFields ( query , ... fields ) { let q = { ... query } ; let fieldSet = new Set ( query . fields ) ; fields . forEach ( f => fieldSet . add ( f ) ) ; q . fields = Array . from ( fieldSet . values ( ) ) ; return q ; } param query param field param operation param values param invert param group export function addPredicate ( query , field , operation , values , invert = false , group = undefined ) { if ( operation === ConseilOperator . BETWEEN && values . length !== 2 ) { throw new Error ( 'BETWEEN operation requires a list of two values.' ) ; } else if ( operation === ConseilOperator . IN && values . length < 2 ) { throw new Error ( 'IN operation requires a list of two or more values.' ) ; } else if ( values . length !== 1 && operation !== ConseilOperator . IN && operation !== ConseilOperator . BETWEEN && operation !== ConseilOperator . ISNULL ) { throw new Error ( `invalid values list for ${ operation }.` ) ; } let q = { ... query } ; q . predicates . push ( { field , operation , set : values , inverse : invert , group } ) ; return q ; } param query param field param direction export function addOrdering ( query , field , direction = ConseilSortDirection . ASC ) { let q = { ... query } ; q . orderBy . push ( { field , direction } ) ; return q ; } param query param limit export function setLimit ( query , limit ) { if ( limit < 1 ) { throw new Error ( 'Limit cannot be less than one.' ) ; } let q = { ... query } ; q . limit = limit ; return q ; } param query param limit export function setOutputType ( query , outputType ) { let q = { ... query } ; q . output = outputType ; return q ; } param query param field param { ConseilFunction } aggregationFunction export function addAggregationFunction ( query , field , aggregationFunction ) { if ( ! query . fields . includes ( field ) ) { throw new Error ( 'Cannot apply an aggregation function on a field not being returned.' ) ; } let q = { ... query } ; q . aggregation . push ( { 'field' : field , 'function' : aggregationFunction } ) ; return q ; } export function addExtendedParameter ( query , parameter , value ) { query [ parameter ] = value ; return query ; } export function removeParameter ( query , parameter ) { if ( query . hasOwnProperty ( parameter ) ) { delete query [ parameter ] ; } return query ; } }