text: // All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  // See https://js.arcgis.com/4.9/esri/copyright.txt for details.
  //>>built
  define("../../../core/declare dojo/Deferred ../../../core/Accessor ../../../core/Promise ../../../core/Collection ../../../core/Handles ../../../core/Evented ../../../core/promiseUtils ../../../core/Error ../../support/StreamPurger ../../../Graphic ../../../geometry/support/jsonUtils ../../../tasks/support/Query".split(" "),function(n,k,p,q,l,r,t,g,u,v,h,w,m){return n([p,q,t],{declaredClass:"esri.layers.graphics.controllers.StreamController",constructor:function(){this._addFeatures=this._addFeatures.bind(this);
  this._handleMessage=this._handleMessage.bind(this);this._handles=new r;this._nextId=0},initialize:function(){var a=this.layer.when(function(){var a=l.ofType(h);this.source=this.layer.source;this.graphics=this.graphics||new a;this._initializeFilter();return(new v(this)).when()}.bind(this)).then(function(a){this.purger=a;return this._makeConnection()}.bind(this)).catch(function(a){throw new u("stream-controller:initialize","Error during initialization process",a.message);}.bind(this));this.addResolvingPromise(a)},
  destroy:function(){this.connection&&(this.connection.disconnect(),this._set("connection",null));this.purger&&(this.purger.destroy(),this.purger=null);this.graphics=null;this._handles&&(this._handles.destroy(),this._handles=null)},properties:{connection:{readOnly:!0,value:null},graphics:{type:l.ofType(h),set:function(a){this._get("graphics")!==a&&(this._handles.remove("graphics"),a&&(this._collectionChanged({added:a.toArray()}),this._handles.add(a.on("change",this._collectionChanged.bind(this)),"graphics")),
  this._set("graphics",a))}},layerView:{},type:{readOnly:!0,value:"stream"},filter:{value:{geometry:null,where:null},readOnly:!0},definitionExpression:{value:null,get:function(){console.warn("StreamController.definitionExpression is deprecated. Access the filter.where property");return this.filter.where},set:function(a){console.warn("StreamController.definitionExpression is deprecated. Use the updateFilter method to change the attribute filter");this.updateFilter({where:a})}},geometryDefinition:{value:null,
  get:function(){console.warn("StreamController.geometryDefinition is deprecated. Access the filter.geometry property");return this.filter.geometry},set:function(a){console.warn("StreamController.geometryDefinition is deprecated. Use the updateFilter method to change the spatial filter");this.updateFilter({geometry:a})}},layer:{value:{}},purger:{set:function(a){this._get("purger")!==a&&this._set("purger",a)}},updating:{value:!1,readOnly:!0,dependsOn:["connection","connection.connectionStatus"],get:function(){return!this.connection||
  "connected"===this.connection.connectionStatus}}},updateFilter:function(a){return this._filterValid(a)?this._filterChanged(a)?this._setFilter(a):g.resolve({filter:this.filter}):g.reject(Error("Invalid properties in filter. geometry must be an extent and where must be a string"))},_makeConnection:function(){this._handles.remove("websocket");return this._addBuddiedServiceFeatures(!0).then(function(){return this._addBuddiedServiceFeatures(!1)}.bind(this),function(a){return g.reject(Error("Error fetching related features. Layer cannot be created"))}.bind(this)).then(function(a){return this.source.createWebSocketConnector(this.layerView.view.spatialReference)}.bind(this)).then(function(a){this._set("connection",
  a);this._handles.add(a.on("data-received",function(a){this._handleMessage(a)}.bind(this)),"websocket");return a.connect()}.bind(this))},_handleMessage:function(a){a=JSON.parse(a);this.emit("data-received",a);a.filter||(a=a instanceof Array?a:[a],this._addFeatures(a))},_addFeatures:function(a){if(a){for(var b=this.layer.objectIdField,c=[],d=0,e=a.length;d<e;d++){var f=a[d];if(f.geometry&&(!f.geometry.hasOwnProperty("x")||f.geometry.x)){if(!f.attributes||!f.attributes[b]&&0!==f.attributes[b])f.attributes=
  f.attributes||{},f.attributes[b]=this._nextId++;f=f.declaredClass?f:h.fromJSON(f);c.push(f)}}this.purger.addMany(c)}},_collectionChanged:function(a){var b,c,d;if(d=a.added)for(b=0;c=d[b];b++)c.layer=this.layer,c.sourceLayer=this.layer;if(d=a.removed)for(b=0;c=d[b];b++)c.layer=null,c.sourceLayer=null},_initializeFilter:function(){var a=this.layer;a&&a.filter&&this._set("filter",{where:a.filter.where||null,geometry:a.filter.geometry||null});this._handles.add(this.watch("layer.filter",function(a){this._setFilter(a)}.bind(this)))},
  _filterChanged:function(a){var b=!1,c=!1;a=a?this.source.makeFilter(a):{geometry:null,where:null};a.hasOwnProperty("geometry")&&(b=a.geometry?!a.geometry.equals(this.filter.geometry):a.geometry!==this.filter.geometry);a.hasOwnProperty("where")&&(c=a.where!==this.filter.where);return b||c},_filterValid:function(a){var b=!0;a&&(a.hasOwnProperty("geometry")&&a.geometry&&(a.geometry.type&&"extent"===a.geometry.type||(b=!1)),b&&a.hasOwnProperty("where")&&a.where&&"string"!==typeof a.where&&(b=!1));return b},
  _setFilter:function(a){var b=null,c=new k;a&&(b=this.source.makeFilter(a),b.geometry&&"string"!==typeof b.geometry&&(b.geometry=b.geometry.toJSON?JSON.stringify(b.geometry.toJSON()):JSON.stringify(b.geometry)));a={filter:b};var d=this.connection.on("data-received",function(a){a=JSON.parse(a);a.hasOwnProperty("filter")&&(d.remove(),a=this._processFilterMessage(a),a.error?c.reject(a.error):c.resolve({filter:this.filter}))}.bind(this));this.connection.send(a);return c.promise},_processFilterMessage:function(a){var b,
  c={};if(a.error)c.error=Error(a.error.join(",")),c.filter={where:this.filter.where,geometry:this.filter.geometry};else{a.filter=a.filter||{};if(b=a.filter.geometry)"string"===typeof b&&(b=JSON.parse(b)),b=w.fromJSON(b);a={where:a.filter.where||null,geometry:b||null};this._set("filter",a);this.notifyChange("geometryDefinition");this.notifyChange("definitionExpression");c.filter=a}return c},_addBuddiedServiceFeatures:function(a){var b,c;if(a){if(!this.source.relatedFeaturesInfo)return g.resolve();a=
  this.source.relatedFeaturesQueryTask;b=this.source.relatedLayerDefinition;c=this._createQuery(b,!0)}else{if(!this.source.latestUrl)return g.resolve();a=this.source.latestQueryTask;b=this.source.archivedLayerDefinition;c=this._createQuery(b,!1)}(b.advancedQueryCapabilities||{}).supportsPagination&&(c.num=b.maxRecordCount);return this._query({query:c,queryTask:a}).then(function(a){if(a){var b=this._fixFieldNameCasing(this.source.layer,a);a.features=b;this._addFeatures(a.features)}return g.resolve()}.bind(this),
  function(a){return g.reject(a)},function(a){a&&this._addFeatures(a.features)}.bind(this))},_query:function(a){var b=a.query,c=a.queryTask,d=new k;b.num&&(b.start=0);var e=function(a){if(a||0===a)b.start=a;c.execute(b).then(f,function(a){d.reject(a)})},f=function(a){a.exceededTransferLimit&&b.num?(e(b.start+b.num),d.progress(a)):d.resolve(a)};e(b.start);return d.promise},_createQuery:function(a,b){var c=this.layer,d=this.layerView,e={where:"1\x3d1",returnGeometry:!0,outFields:["*"]};if(!a)return new m(e);
  c=(c=b?this.source.relatedFeaturesInfo&&this.source.relatedFeaturesInfo.outFields:c.outFields)?c.slice(0):["*"];e=this.filter.where||"1\x3d1";b&&(b=this._getFieldsNotShared(a,this.layer.fields),c=this._removeInvalidOutfields(b,c),"1\x3d1"!==e&&this._checkForInvalidFieldInWhere(b,e)&&(e="1\x3d1"));c=this._addObjectIdFieldToOutfields(a,c);return new m({where:e,geometry:this.filter.geometry,outFields:c,outSpatialReference:d.view.spatialReference,returnGeometry:!0})},_addObjectIdFieldToOutfields:function(a,
  b){b=b||["*"];if("*"!==b[0]){var c=this._getObjectIdFieldName(a);c&&(b.some(function(a){return a.toLowerCase()===c.toLowerCase()})||b.push(c))}return b},_removeInvalidOutfields:function(a,b){b=b||["*"];var c;"*"!==b[0]&&(c=b.filter(function(b){if(-1===a.indexOf(b))return b}));return c&&0!==c.length?c:["*"]},_checkForInvalidFieldInWhere:function(a,b){return b&&"1\x3d1"!==b?a.some(function(a){return(new RegExp("\\s*"+a+"\\b","i")).test(b)}):!1},_getObjectIdFieldName:function(a){var b=null;a.fields.some(function(a){return"esriFieldTypeOID"===
  a.type?(b=a.name,!0):!1});return b},_getFieldsNotShared:function(a,b){var c=a.fields.map(function(a){return a.name.toLowerCase()}),d=[];b.forEach(function(a){a=a.name;-1===c.indexOf(a.toLowerCase())&&d.push(a)});return d},_fixFieldNameCasing:function(a,b){var c,d,e=[];if(!a)throw Error("streamLayer is a required argument for _fixFieldNameCasingmethod");a&&(c=a.fields);b&&(e=b.features||[],d=b.fields);if(!d||!e.length||!c)return e;a=this._mapFieldNameDifferences(c,d);c=[];var f;d=0;for(var g=b.features.length;d<
  g;d++)b=e[d],f=this._swizzleResponseAttributes(b.attributes,a),b.attributes=f,c.push(b);return c},_mapFieldNameDifferences:function(a,b){var c=[],d={},e,f;a=a||[];b=b||[];e=0;for(f=b.length;e<f;e++)c.push(b[e].name);e=0;for(f=a.length;e<f;e++){b=a[e].name;var g=this._checkForStreamFieldName(b,c);g&&(d[g]=b)}return d},_checkForStreamFieldName:function(a,b){var c,d;if(b&&b.length){if(a&&a.toLowerCase){a=a.toLowerCase();for(var e=0,f=b.length;e<f;e++)if(c=b[e],c.toLowerCase&&c.toLowerCase()===a){d=c;
  break}}return d}},_swizzleResponseAttributes:function(a,b){var c={},d;for(d in a)if(a.hasOwnProperty(d)){var e=a[d];b.hasOwnProperty(d)?c[b[d]]=e:c[d]=e}return c}})});