import { ArrayExpressionFuncs , ExpressionFuncs , IfArgs , IfFunction , QueryField , SwitchFunction , } from './types' ; import { handleError , queryBuilder } from './queryBuilder' ; import { isQueryObjectArray , isQueryObject , isIfArgs , isSwitchArgs , } from './typeCheckers' ; export const arrayArgFuncs = { $and : ( args ) : string => `AND(${ queryBuilder ( args ) })` , $or : ( args ) : string => `OR(${ queryBuilder ( args ) })` , $xor : ( args ) : string => `XOR(${ queryBuilder ( args ) })` , } ; export const expressionFuncs = { $not : ( expression ) : string => `NOT(${ queryBuilder ( expression ) })` , $isError : ( expression ) : string => `ISERROR(${ queryBuilder ( expression ) })` , } ; export const ifFunc = { $if : ( { expression , ifTrue , ifFalse } ) : string => `IF(${ queryBuilder ( expression ) }, ${ queryBuilder ( ifTrue ) }, ${ queryBuilder ( ifFalse ) })` , } ; export const switchFunc = { $switch : ( { expression , cases , defaultVal } ) : string => `SWITCH(${ queryBuilder ( expression ) }, ${ cases . slice ( 0 ) . map ( ( { switchCase , val } ) => `${ queryBuilder ( switchCase ) }, ${ queryBuilder ( val ) }` ) . join ( ', ' ) }, ${ queryBuilder ( defaultVal ) })` , } ; export const logicalFunctions = { ... arrayArgFuncs , ... expressionFuncs , ... ifFunc , ... switchFunc , } ; export const handleLogicalFunc = ( key , val ) : string => { if ( key in arrayArgFuncs && isQueryObjectArray ( val ) ) { return arrayArgFuncs [ key ] ( val ) ; } else if ( key in expressionFuncs && isQueryObject ( val ) ) { return expressionFuncs [ key ] ( val ) ; } else if ( key in ifFunc && isIfArgs ( val ) ) { return ifFunc [ key ] ( val ) ; } else if ( key in switchFunc && isSwitchArgs ( val ) ) { return switchFunc [ key ] ( val ) ; } throw handleError ( { key , val } ) ; } ;