/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../chunks/_rollupPluginBabelHelpers","../../../../chunks/tslib.es6","../../../../core/HandleOwner","../../../../core/has","../../../../core/reactiveUtils","../../../../core/accessorSupport/decorators/property","../../../../core/arrayUtils","../../../../core/accessorSupport/ensureType","../../../../core/accessorSupport/decorators/subclass","../../../../geometry/support/spatialReferenceUtils","../../../../layers/support/TileInfo","./processors","./controllers/FeatureController2D","./support/TileStore","./support/UpdateToken","../../tiling/TileInfoView"],(function(e,t,r,o,i,s,n,l,c,a,u,p,h,d,f,y){"use strict";let _=function(t){function r(){var e;return(e=t.apply(this,arguments)||this).controller=null,e.processor=null,e.remoteClient=null,e.tileStore=null,e.service=null,e.viewState=null,e._paused=!1,e._pendingTileUpdates=[],e}e._inheritsLoose(r,t);var o=r.prototype;return o.initialize=function(){this.handles.add(i.watch((()=>this.updating),(e=>{this.remoteClient.invoke("setUpdating",e).catch((e=>{}))})))},o.destroy=function(){var e,t;this.stop(),null==(e=this.controller)||e.destroy(),null==(t=this.processor)||t.destroy(),this.controller=this.processor=this.tileStore=this.remoteClient=null},o.stop=function(){var e,t,r;this._paused=!0,Array.isArray(null==(e=this.service)?void 0:e.source)&&(this.service.source.forEach((e=>e.close())),this.service.source.length=0),null==(t=this.tileStore)||t.updateTiles({added:[],removed:this.tileStore.tiles.map((e=>e.id))}),null==(r=this.tileStore)||r.destroy(),this.tileStore=null,this._pendingTileUpdates.length=0},o.startup=function(){var t=e._asyncToGenerator((function*({service:e,config:t,tileInfo:r,tiles:o}){var i;if(this._paused=!0,Array.isArray(null==(i=this.service)?void 0:i.source)&&(this.service.source.forEach((e=>e.close())),this.service.source.length=0),this.service=e,!this.tileStore||!a.equals(this.tileStore.tileScheme.spatialReference,r.spatialReference)){var s,n;const e=new y(u.fromJSON(r));o.added.length=o.removed.length=0,null==(s=this.tileStore)||s.updateTiles({added:[],removed:this.tileStore.tiles.map((e=>e.id))}),null==(n=this.tileStore)||n.destroy(),this.tileStore=new d(e),this._pendingTileUpdates.length=0}for(yield this._createProcessorAndController(t),yield this.update({config:t}),this.controller.resume(),this.tileStore.clear(),this.tileStore.updateTiles(o),this._paused=!1;this._pendingTileUpdates.length;)this.tileStore.updateTiles(this._pendingTileUpdates.pop())}));function r(e){return t.apply(this,arguments)}return r}(),o.updateTiles=function(){var t=e._asyncToGenerator((function*(e){this._paused?this._pendingTileUpdates.push(e):this.tileStore.updateTiles(e)}));function r(e){return t.apply(this,arguments)}return r}(),o.update=function(){var t=e._asyncToGenerator((function*({config:e}){const t=f.UpdateToken.empty();return yield Promise.all([this.processor.update(t,e),this.controller.update(t,e)]),t.toJSON()}));function r(e){return t.apply(this,arguments)}return r}(),o.applyUpdate=function(){var t=e._asyncToGenerator((function*(e){return this.controller.applyUpdate(f.UpdateToken.create(e))}));function r(e){return t.apply(this,arguments)}return r}(),o._createProcessorAndController=function(){var t=e._asyncToGenerator((function*(e){yield Promise.all([this._handleControllerConfig(e),this._handleProcessorConfig(e)]),this.controller.processor=this.processor}));function r(e){return t.apply(this,arguments)}return r}(),o._handleControllerConfig=function(){var t=e._asyncToGenerator((function*(e){return this._createController(this.service,e)}));function r(e){return t.apply(this,arguments)}return r}(),o._handleProcessorConfig=function(){var t=e._asyncToGenerator((function*(e){return this._createProcessor(this.service,e)}));function r(e){return t.apply(this,arguments)}return r}(),o._createController=function(){var t=e._asyncToGenerator((function*(e,t){this.controller&&this.controller.destroy();const{tileStore:r,remoteClient:o}=this,i=new h({service:e,tileStore:r,remoteClient:o});return this.controller=i,i}));function r(e,r){return t.apply(this,arguments)}return r}(),o._createProcessor=function(){var t=e._asyncToGenerator((function*(e,t){const r=t.schema.processors[0].type,o=(yield p.loadProcessorModule(r)).default,{remoteClient:i,tileStore:s}=this,n=new o({service:e,config:t,tileStore:s,remoteClient:i});return this.processor&&this.processor.destroy(),this.processor=n,n}));function r(e,r){return t.apply(this,arguments)}return r}(),e._createClass(r,[{key:"updating",get:function(){return!this.controller||this.controller.updating}}]),r}(r.HandleOwner);t.__decorate([s.property()],_.prototype,"controller",void 0),t.__decorate([s.property()],_.prototype,"processor",void 0),t.__decorate([s.property()],_.prototype,"updating",null),t.__decorate([s.property()],_.prototype,"viewState",void 0),_=t.__decorate([c.subclass("esri.views.2d.layers.features.Pipeline")],_);return _}));
