import { CoatyObject , CommunicationEventType , CoreType , CoreTypes , isPlainObject , Uuid } from ".." ; import { CommunicationEvent , CommunicationEventData } from "./communication-event" ; /**
 * Discover event.
 */ export class DiscoverEvent extends CommunicationEvent < DiscoverEventData > { get eventType ( ) { return CommunicationEventType . Discover ; } get responseEventType ( ) { return CommunicationEventType . Resolve ; } param event resolve : ( event : ResolveEvent ) => void ; param externalId static withExternalId ( externalId ) { return new DiscoverEvent ( new DiscoverEventData ( externalId ) ) ; } param externalId param coreTypes static withExternalIdAndCoreTypes ( externalId , coreTypes ) { return new DiscoverEvent ( new DiscoverEventData ( externalId , undefined , undefined , coreTypes ) ) ; } param externalId param objectTypes static withExternalIdAndObjectTypes ( externalId , objectTypes ) { return new DiscoverEvent ( new DiscoverEventData ( externalId , undefined , objectTypes , undefined ) ) ; } param objectId static withObjectId ( objectId ) { return new DiscoverEvent ( new DiscoverEventData ( undefined , objectId ) ) ; } param externalId param objectId static withExternalAndObjectId ( externalId , objectId ) { return new DiscoverEvent ( new DiscoverEventData ( externalId , objectId ) ) ; } param coreTypes static withCoreTypes ( coreTypes ) { return new DiscoverEvent ( new DiscoverEventData ( undefined , undefined , undefined , coreTypes ) ) ; } param objectTypes static withObjectTypes ( objectTypes ) { return new DiscoverEvent ( new DiscoverEventData ( undefined , undefined , objectTypes , undefined ) ) ; } internal param eventData throws ensureValidResponseParameters ( eventData ) { if ( this . data . coreTypes !== undefined && eventData . object ) { if ( ! this . data . coreTypes . some ( t => t === eventData . object . coreType ) ) { throw new TypeError ( "resolved coreType not contained in Discover coreTypes" ) ; } } if ( this . data . objectTypes !== undefined && eventData . object ) { if ( ! this . data . objectTypes . some ( t => t === eventData . object . objectType ) ) { throw new TypeError ( "resolved objectType not contained in Discover objectTypes" ) ; } } if ( this . data . objectId && eventData . object ) { if ( this . data . objectId !== eventData . object . objectId ) { throw new TypeError ( "resolved object's UUID doesn't match Discover objectId" ) ; } } if ( this . data . externalId && eventData . object ) { if ( this . data . externalId !== eventData . object . externalId ) { throw new TypeError ( "resolved object's external ID doesn't match Discover externalId" ) ; } } } } /**
 * Defines event data format for discovering objects based on external and
 * object IDs or object types.
 */ export class DiscoverEventData extends CommunicationEventData { /**
     * The external ID of the object(s) to be discovered.
     * Can be used exclusively or in combination with objectId property.
     * If used exclusively it can be combined with objectTypes or coreTypes properties.
     */ get externalId ( ) { return this . _externalId ; } /**
     * The internal UUID of the object to be discovered.
     * Can be used exclusively or in combination with externalId property.
     * Must not be used in combination with objectTypes or coreTypes properties.
     */ get objectId ( ) { return this . _objectId ; } /**
     * Restrict objects by object types (logical OR).
     * Not to be used with objectId property.
     * Should not be used in combination with coreTypes.
     */ get objectTypes ( ) { return this . _objectTypes ; } /**
     * Restrict objects by core types (logical OR).
     * Not to be used with objectId property.
     * Should not be used in combination with objectTypes.
     */ get coreTypes ( ) { return this . _coreTypes ; } /**
     * Determines whether this event data discovers an object based on an external ID
     * but not an object ID.
     */ get isDiscoveringExternalId ( ) { return this . _externalId !== undefined && this . _objectId === undefined ; } /**
     * Determines whether this event data discovers an object based on an object ID.
     */ get isDiscoveringObjectId ( ) { return this . _externalId === undefined && this . _objectId !== undefined ; } /**
     * Determines whether this event data discovers an object based on both
     * external ID and object ID.
     */ get isDiscoveringExternalAndObjectId ( ) { return this . _externalId !== undefined && this . _objectId !== undefined ; } /**
     * Determines whether this event data discovers an object based
     * on types only.
     */ get isDiscoveringTypes ( ) { return this . _externalId === undefined && this . _objectId === undefined ; } private _externalId : string ; private _objectId : Uuid ; private _objectTypes : string [ ] ; private _coreTypes : CoreType [ ] ; param externalId param objectId param objectTypes param coreTypes constructor ( externalId ? , objectId ? , objectTypes ? , coreTypes ? ) { super ( ) ; this . _externalId = externalId ; this . _objectId = objectId ; this . _objectTypes = objectTypes ; this . _coreTypes = coreTypes ; if ( ! this . _hasValidParameters ( ) ) { throw new TypeError ( "in DiscoverEventData: arguments not valid" ) ; } } internal static createFrom ( eventData ) { return new DiscoverEventData ( eventData . externalId , eventData . objectId , eventData . objectTypes , eventData . coreTypes ) ; } 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 . _objectId === undefined || object . objectId === this . _objectId ) && ( this . _externalId === undefined || object . externalId === this . _externalId ) && ( this . _coreTypes === undefined || this . coreTypes . some ( t => t === object . coreType ) ) && ( this . _objectTypes === undefined || this . objectTypes . some ( t => t === object . objectType ) ) ; } toJsonObject ( ) { return { externalId : this . _externalId , objectId : this . _objectId , objectTypes : this . _objectTypes , coreTypes : this . _coreTypes , } ; } private _hasValidParameters ( ) { return ( ( typeof this . _objectId === "string" && this . _externalId === undefined && this . _objectTypes === undefined && this . _coreTypes === undefined ) || ( typeof this . _externalId === "string" && this . _objectId === undefined && ( this . _objectTypes === undefined || CoreTypes . isStringArray ( this . _objectTypes ) ) && ( this . _coreTypes === undefined || CoreTypes . isCoreTypeArray ( this . _coreTypes ) ) ) || ( typeof this . _objectId === "string" && typeof this . _externalId === "string" && this . _objectTypes === undefined && this . _coreTypes === undefined ) || ( this . _objectId === undefined && this . _externalId === undefined && ( ( this . _objectTypes === undefined && CoreTypes . isCoreTypeArray ( this . _coreTypes ) ) || ( this . _coreTypes === undefined && CoreTypes . isStringArray ( this . _objectTypes ) ) ) ) ) ; } } /**
 * Resolve event with associated request event.
 */ export class ResolveEvent extends CommunicationEvent < ResolveEventData > { get eventType ( ) { return CommunicationEventType . Resolve ; } internal eventRequest : DiscoverEvent ; param object param relatedObjects param privateData static withObject ( object , relatedObjects ? , privateData ? ) { return new ResolveEvent ( new ResolveEventData ( object , relatedObjects , privateData ) ) ; } param relatedObjects param privateData static withRelatedObjects ( relatedObjects , privateData ? ) { return new ResolveEvent ( new ResolveEventData ( undefined , relatedObjects , privateData ) ) ; } } /**
 * Defines event data format for resolving objects.
 */ export class ResolveEventData extends CommunicationEventData { /**
     * The object to be resolved (may be undefined if relatedObjects property
     * is defined).
     */ get object ( ) { return this . _object ; } /**
     * Related objects, i.e. child objects to be resolved (may be undefined
     * if object property is defined).
     */ get relatedObjects ( ) { return this . _relatedObjects ; } /**
     * Application-specific options as an object hash (optional).
     */ get privateData ( ) { return this . _privateData ; } private _object : CoatyObject ; private _relatedObjects : CoatyObject [ ] ; private _privateData : { [ key ] : any ; } ; param object param relatedObjects param privateData constructor ( object ? , relatedObjects ? , privateData ? ) { super ( ) ; this . _object = object ; this . _relatedObjects = relatedObjects ; this . _privateData = privateData ; if ( ! this . _hasValidParameters ( ) ) { throw new TypeError ( "in ResolveEventData: arguments not valid" ) ; } } internal static createFrom ( eventData ) { return new ResolveEventData ( eventData . object , eventData . relatedObjects , eventData . privateData ) ; } toJsonObject ( ) { return { object : this . _object , relatedObjects : this . _relatedObjects , privateData : this . _privateData , } ; } private _hasValidParameters ( ) { if ( ! this . _isValidPrivateData ( ) ) { return false ; } if ( this . _relatedObjects === undefined && CoreTypes . isObject ( this . _object ) ) { return true ; } if ( this . _object === undefined && CoreTypes . isObjectArray ( this . _relatedObjects ) ) { return true ; } if ( CoreTypes . isObject ( this . _object ) && CoreTypes . isObjectArray ( this . _relatedObjects ) ) { return true ; } return false ; } private _isValidPrivateData ( ) { return this . _privateData === undefined || isPlainObject ( this . _privateData ) ; } }