define(["require","exports","tslib","../../deps/bluebird_fork","./ArcGisApiVersion","../../Validation","./tools","./MapProvider","../../Hooks","esri/request","esri/tasks/Geoprocessor","../../Task"],function(e,t,l,c,s,d,f,n,o,a,p,b){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),c=l.__importDefault(c),a=l.__importDefault(a),p=l.__importDefault(p);function v(e,t){var s,n=void 0;if(null!=e){for(var o in e)s=o,Object.prototype.hasOwnProperty.call(e,s)&&((n=void 0===n?{}:n)[o]=t(e[o],o));return n}}i.prototype.execute=function(o,a,u){return l.__awaiter(this,void 0,void 0,function(){var i,r,t,s,n=this;return l.__generator(this,function(e){switch(e.label){case 0:return s=d.validation.string("url",o.url,d.validation.never),(i=new p.default(s)).outSpatialReference=u.getOutSR(o.outSpatialReference),i.processSpatialReference=u.getOutSR(o.processSpatialReference),t=d.validation.exists("arguments",o.arguments,{}),r=f.isAbortable(a.cancellationToken)?{signal:a.cancellationToken.signal}:void 0,s=i.submitJob(t,r),t=s,this.needToWaitForJobCompletion()?[4,s]:[3,2];case 1:s=e.sent(),t=i.waitForJobCompletion(s.jobId,r),e.label=2;case 2:return f.schedulePromiseCancellation(a,t),[2,f.inline(t,function(o){return l.__awaiter(n,void 0,void 0,function(){var s,t,n;return l.__generator(this,function(e){switch(e.label){case 0:return"job-succeeded"!==o.jobStatus&&"esriJobSucceeded"!==o.jobStatus?[3,3]:o.results?[3,2]:[4,this.populateResults(i.url,o,r)];case 1:e.sent(),e.label=2;case 2:return s=[],t=v(o.results,function(e,t){t=c.default.resolve(i.getResultData(o.jobId,t,r));return f.schedulePromiseCancellation(a,t),s.push(t),t}),n=c.default.all(s),b.Task.race([n,a.cancellationToken]).finally(function(){n.isPending()&&i.cancelJob(o.jobId)}),[2,f.inline(n,function(){return{id:o.jobId,status:o.jobStatus,results:v(t,function(e){return e.value().value}),messages:o.messages}})];case 3:return[2,{id:o.jobId,status:o.jobStatus,results:void 0,messages:o.messages}]}})})})]}})})},i.prototype.needToWaitForJobCompletion=function(){return s.isArcGisApiVersionAtLeast("4.13")},i.prototype.populateResults=function(o,i,r){return l.__awaiter(this,void 0,void 0,function(){var t,s,n;return l.__generator(this,function(e){switch(e.label){case 0:return t=o.indexOf("?"),s=o,n="",-1!==t&&(s=o.substring(0,t),n=o.substring(t)),n=n?n+"&f=json":"?f=json",n=s+"/jobs/"+i.jobId+n,[4,a.default(n,r)];case 1:return(n=e.sent())&&n.data&&(i.results=n.data.results),[2]}})})},i.action="gcx:wf:arcgis::GeoprocessingAsync",i.suite="gcx:wf:builtin",n=l.__decorate([o.activate(n.MapProvider)],i);function i(){}t.GeoprocessingAsync=n});