import { areObjectJoinConditionsValid , CoatyObject , CommunicationEventType , CoreType , CoreTypes , isObjectFilterValid , isPlainObject , ObjectFilter , ObjectJoinCondition , ObjectMatcher , } from ".." ; import { CommunicationEvent , CommunicationEventData } from "./communication-event" ; /**
 * Query event.
 */ export class QueryEvent extends CommunicationEvent < QueryEventData > { get eventType ( ) { return CommunicationEventType . Query ; } get responseEventType ( ) { return CommunicationEventType . Retrieve ; } param event retrieve : ( event : RetrieveEvent ) => void ; param objectTypes param objectFilter param objectJoinConditions static withObjectTypes ( objectTypes , objectFilter ? , objectJoinConditions ? ) { return new QueryEvent ( new QueryEventData ( objectTypes , undefined , objectFilter , objectJoinConditions ) ) ; } param coreTypes param objectFilter param objectJoinConditions static withCoreTypes ( coreTypes , objectFilter ? , objectJoinConditions ? ) { return new QueryEvent ( new QueryEventData ( undefined , coreTypes , objectFilter , objectJoinConditions ) ) ; } internal param eventData throws ensureValidResponseParameters ( eventData ) { for ( const obj of eventData . objects ) { if ( this . data . coreTypes !== undefined ) { if ( ! this . data . coreTypes . some ( t => t === obj . coreType ) ) { throw new TypeError ( "retrieved coreType not contained in Query coreTypes" ) ; } } if ( this . data . objectTypes !== undefined ) { if ( ! this . data . objectTypes . some ( t => t === obj . objectType ) ) { throw new TypeError ( "retrieved objectType not contained in Query objectTypes" ) ; } } } } } /**
 * Defines event data format for querying objects based on object types, object filters,
 * and object join conditions.
 */ export class QueryEventData extends CommunicationEventData { /**
     * Restrict objects by object types (logical OR).
     * Should not be used in combination with coreTypes.
     */ get objectTypes ( ) { return this . _objectTypes ; } /**
     * Restrict objects by core types (logical OR).
     * Should not be used in combination with objectTypes.
     */ get coreTypes ( ) { return this . _coreTypes ; } /**
     * Restrict objects by an object filter.
     */ get objectFilter ( ) { return this . _objectFilter ; } /**
     * Join related objects by join conditions.
     */ get objectJoinConditions ( ) { return this . _objectJoinConditions ; } private _objectTypes : string [ ] ; private _coreTypes : CoreType [ ] ; private _objectFilter : ObjectFilter ; private _objectJoinConditions : ObjectJoinCondition | ObjectJoinCondition [ ] ; param objectTypes param coreTypes param objectFilter param objectJoinConditions constructor ( objectTypes ? , coreTypes ? , objectFilter ? , objectJoinConditions ? ) { super ( ) ; this . _objectTypes = objectTypes ; this . _coreTypes = coreTypes ; this . _objectFilter = objectFilter ; this . _objectJoinConditions = objectJoinConditions ; if ( ! this . _hasValidParameters ( ) ) { throw new TypeError ( "in QueryEventData: arguments not valid" ) ; } } internal static createFrom ( eventData ) { return new QueryEventData ( eventData . objectTypes , eventData . coreTypes , eventData . objectFilter , eventData . objectJoinConditions ) ; } param coreType isCoreTypeCompatible ( coreType ) { return this . coreTypes && this . coreTypes . some ( t => t === coreType ) ; } param objectType isObjectTypeCompatible ( objectType ) { return this . objectTypes && this . objectTypes . some ( t => t === objectType ) ; } param object matchesObject ( object ) { if ( ! object ) { return false ; } return ( this . _coreTypes === undefined || this . _coreTypes . some ( t => t === object . coreType ) ) || ( this . _objectTypes === undefined || this . _objectTypes . some ( t => t === object . objectType ) ) || ( this . _objectFilter === undefined || ObjectMatcher . matchesFilter ( object , this . _objectFilter ) ) ; } toJsonObject ( ) { return { objectTypes : this . _objectTypes , coreTypes : this . _coreTypes , objectFilter : this . _objectFilter , objectJoinConditions : this . _objectJoinConditions , } ; } private _hasValidParameters ( ) { return isObjectFilterValid ( this . _objectFilter ) && areObjectJoinConditionsValid ( this . _objectJoinConditions ) && ( ( this . _objectTypes === undefined && CoreTypes . isCoreTypeArray ( this . _coreTypes ) ) || ( this . _coreTypes === undefined && CoreTypes . isStringArray ( this . _objectTypes ) ) ) ; } } /**
 * Retrieve event with associated request event.
 */ export class RetrieveEvent extends CommunicationEvent < RetrieveEventData > { get eventType ( ) { return CommunicationEventType . Retrieve ; } internal eventRequest : QueryEvent ; param objects param privateData static withObjects ( objects , privateData ? ) { return new RetrieveEvent ( new RetrieveEventData ( objects , privateData ) ) ; } } /**
 * Defines event data format for retrieving objects.
 */ export class RetrieveEventData extends CommunicationEventData { private _objects : CoatyObject [ ] ; private _privateData : { [ key ] : any ; } ; param objects param privateData constructor ( objects , privateData ? ) { super ( ) ; this . _objects = objects ; this . _privateData = privateData ; if ( ! this . _hasValidParameters ( ) ) { throw new TypeError ( "in RetrieveEventData: arguments not valid" ) ; } } internal static createFrom ( eventData ) { return new RetrieveEventData ( eventData . objects , eventData . privateData ) ; } /**
     * An array of objects to be retrieved (array may be empty).
     */ get objects ( ) { return this . _objects ; } /**
     * Application-specific options as an object hash (optional).
     */ get privateData ( ) { return this . _privateData ; } toJsonObject ( ) { return { objects : this . _objects , privateData : this . _privateData , } ; } private _hasValidParameters ( ) { return this . _isValidPrivateData ( ) && CoreTypes . isObjectArray ( this . _objects ) ; } private _isValidPrivateData ( ) { return this . _privateData === undefined || isPlainObject ( this . _privateData ) ; } }