import { query as q , Expr } from 'faunadb' ; import { FactoryContextDefinition } from '~/types/factory/factory.context' ; import { FaunaObject , FaunaUDFunctionOptions , WrapFunction } from '~/types/fauna' ; import { CONVENTION } from '~/consts' ; import { ContextExtend } from './context' ; export function BiotaFunctionName ( name ) { return `${ CONVENTION . UDFUNCTION_PREFIX }${ name . replace ( CONVENTION . UDFUNCTION_PREFIX , '' ) }` ; } export function Input ( params , query ) { return q . Let ( params , query ) ; } const removeOfflineCode = ( obj ) => { function reducer ( item ) { if ( item instanceof Expr ) { return new Expr ( reducer ( ( item as any ) . raw ) ) ; } else if ( Array . isArray ( item ) ) { return item . map ( reducer ) ; } else if ( item && typeof item === 'object' ) { try { const safeItem = JSON . parse ( JSON . stringify ( item ) ) ; if ( safeItem . if && safeItem . if . select ) { const select = Array . isArray ( safeItem . if . select ) ? safeItem . if . select : [ safeItem . if . select ] ; if ( select . includes ( 'offline' ) ) { return reducer ( reducer ( ( item . else as any ) . raw ) ) ; } } } catch ( error ) { } return Object . entries ( item ) . reduce ( ( newObj , [ key , value ] ) => { newObj [ key ] = reducer ( value ) ; return newObj ; } , { } ) ; } else { return item ; } } return reducer ( obj ) ; } ; export function UDFunctionFromMethod ( methodRaw ) { const getRaw = ( obj ) => { if ( typeof obj === 'object' && obj ) { try { if ( obj . raw ) { obj = obj . raw ; } } catch ( error ) { } return obj ; } } ; let definition = { } ; let args = [ ] ; if ( getRaw ( methodRaw ) . if && getRaw ( methodRaw ) . then && getRaw ( methodRaw ) . else ) { try { definition = getRaw ( getRaw ( getRaw ( methodRaw ) . else ) . let [ 0 ] . UDFunctionDefinition ) . object as FaunaUDFunctionOptions ; } catch ( error ) { } try { args = getRaw ( getRaw ( getRaw ( methodRaw ) . else ) . let [ 1 ] . Args ) || [ ] ; } catch ( error ) { } try { const query = getRaw ( getRaw ( methodRaw ) . then ) . in ; definition . body = q . Query ( q . Lambda ( [ 'ctx' , 'params' ] , q . Let ( args . reduce ( ( letObj , key ) => { letObj [ key ] = q . Select ( key , q . Var ( 'params' ) , null ) ; return letObj ; } , { } ) , removeOfflineCode ( query ) ) ) ) ; } catch ( error ) { } } if ( definition . name ) { return definition ; } else { return null ; } } export function CallFunction ( nameOrDefinition , context , params ) { const definition = typeof nameOrDefinition === 'object' ? nameOrDefinition : { name : nameOrDefinition } ; return q . Let ( { UDFunctionDefinition : definition , Args : Object . keys ( params || { } ) , } , q . Call ( definition . name , ContextExtend ( context , 'udf.CallFunction >' + definition . name , params ) , params ) ) ; } export function UDFunction ( fn ) { let { name = '' , body , data , role } = fn || { } ; let self = { name , body , data , role , } ; return self ; }