/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../../chunks/_rollupPluginBabelHelpers","../../../../../TimeExtent","../../../../../core/Evented","../../../../../core/has","../../../../../core/maybe","../../../../../core/promiseUtils","../../../../../core/accessorSupport/diffUtils","../../../../../rest/support/Query","./DataTileSubscription","../support/UpdateToken"],(function(e,t,r,i,s,n,o,u,c,a,h){"use strict";function l(e,t){const r=new Set;return e&&e.forEach((e=>r.add(e))),t&&t.forEach((e=>r.add(e))),r.has("*")?["*"]:Array.from(r)}let d=function(){function e(e){this.events=new i,this._resolver=o.createResolver(),this._didEdit=!1,this._subscriptions=new Map,this._outSR=e.outSR,this._serviceInfo=e.serviceInfo,this._onTileUpdateMessage=e.onMessage}var d=e.prototype;return d.destroy=function(){},d._onMessage=function(){var e=t._asyncToGenerator((function*(e){var t,r;const i=this._subscriptions.get(e.id);if(!i)return;const s={...e,remove:null!=(t=e.remove)?t:[],status:null!=(r=e.status)?r:h.UpdateToken.empty()};return o.ignoreAbortErrors(this._onTileUpdateMessage(s,i.options))}));function r(t){return e.apply(this,arguments)}return r}(),d.update=function(e,t){var i;const n=t.fields.length;t.outFields=l(null==(i=this._schema)?void 0:i.outFields,t.outFields),t.outFields=t.outFields.length>=.75*n?["*"]:t.outFields,t.outFields.sort();const o=u.diff(this._schema,t);if(!o)return;s("esri-2d-update-debug")&&console.debug("Applying Update - Source:",o);const c="orderByFields"in this._serviceInfo&&this._serviceInfo.orderByFields?this._serviceInfo.orderByFields:this._serviceInfo.objectIdField+" ASC",a={returnCentroid:s("esri-2d-query-centroid-enabled")&&"esriGeometryPolygon"===this._serviceInfo.geometryType,returnGeometry:!0,timeReferenceUnknownClient:"stream"!==this._serviceInfo.type&&this._serviceInfo.timeReferenceUnknownClient,outFields:t.outFields,outSpatialReference:this._outSR,orderByFields:[c],where:t.definitionExpression||"1=1",gdbVersion:t.gdbVersion,historicMoment:t.historicMoment,timeExtent:r.fromJSON(t.timeExtent)},h=this._schema&&u.hasDiff(o,"outFields");this._schema&&u.hasDiffAny(o,["timeExtent","definitionExpression","gdbVersion","historicMoment","customParameters"])&&(e.why.mesh.push("Layer filter and/or custom parameters changed"),e.why.source.push("Layer filter and/or custom parameters changed"),e.mesh=!0,e.source=!0,e.queryFilter=!0),h&&(e.why.source.push("Layer required fields changed"),e.source=!0),u.diff(a,this._queryInfo)&&(this._queryInfo=a),this._schema=t,this._resolver.resolve()},d.whenInitialized=function(){return this._resolver.promise},d.applyUpdate=function(){var e=t._asyncToGenerator((function*(e){if(e.queryFilter||e.source&&this._didEdit)return this.refresh(e.version),void(this._didEdit=!1);this._subscriptions.forEach((t=>t.applyUpdate(e))),yield this.resend()}));function r(t){return e.apply(this,arguments)}return r}(),d.refresh=function(e){for(const t of this._tiles())this.unsubscribe(t),this.subscribe(t,e)},d.subscribe=function(e,t){const r=new a.DataTileSubscription(e,t);this._subscriptions.set(e.id,r)},d.unsubscribe=function(e){const t=this.get(e.id);n.isSome(t)&&t.abort(),this._subscriptions.delete(e.id)},d.createQuery=function(e={}){const t=this._queryInfo.historicMoment?new Date(this._queryInfo.historicMoment):null;return new c({...this._queryInfo,historicMoment:t,...e})},d.get=function(e){return this._subscriptions.has(e)?this._subscriptions.get(e):null},d.queryLastEditDate=function(){var e=t._asyncToGenerator((function*(){throw new Error("Service does not support query type")}));function r(){return e.apply(this,arguments)}return r}(),d.query=function(){var e=t._asyncToGenerator((function*(e){throw new Error("Service does not support query")}));function r(t){return e.apply(this,arguments)}return r}(),d._tiles=function*(){const e=Array.from(this._subscriptions.values());for(const t of e)yield t.tile},d.edit=function(){var e=t._asyncToGenerator((function*(e,r){var i=this;const s=Array.from(this._subscriptions.values()),n=s.map((({tile:e})=>e));for(const t of s)t.removeIds(r);if(e.length){const s=n.map((t=>{const r=this.createTileQuery(t);return r.objectIds=e,{tile:t,query:r}})).map(function(){var e=t._asyncToGenerator((function*({tile:e,query:t}){return{tile:e,result:yield i.query(t),query:t}}));return function(t){return e.apply(this,arguments)}}()),u=(yield o.eachAlwaysValues(s)).map(function(){var s=t._asyncToGenerator((function*({tile:t,result:s}){if(!s.hasFeatures&&!r.length&&!e.length)return;const n=i._subscriptions.get(t.key.id);n&&n.edit(s,e)}));return function(e){return s.apply(this,arguments)}}());yield o.eachAlways(u)}this._didEdit=!0}));function r(t,r){return e.apply(this,arguments)}return r}(),e}();e.DataTileSource=d,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
