import{__decorate}from"tslib";import Bluebird from"../../deps/bluebird_fork.js";import{isArcGisApiVersionAtLeast}from"./ArcGisApiVersion.js";import{validation}from"../../Validation.js";import{inline,schedulePromiseCancellation,isAbortable}from"./tools.js";import{MapProvider}from"./MapProvider.js";import{activate}from"../../Hooks.js";import esriRequest from"@arcgis/core/request";import Geoprocessor from"@arcgis/core/tasks/Geoprocessor";import{Task}from"../../Task.js";const hasOwnProperty=function(e,s){return Object.prototype.hasOwnProperty.call(e,s)};function map(e,s){let o=void 0;if(null!=e){for(const t in e)hasOwnProperty(e,t)&&(void 0===o&&(o={}),o[t]=s(e[t],t));return o}}let GeoprocessingAsync=class{async execute(e,r,s){var o=validation.string("url",e.url,validation.never);const i=new Geoprocessor(o);i.outSpatialReference=s.getOutSR(e.outSpatialReference),i.processSpatialReference=s.getOutSR(e.processSpatialReference);e=validation.exists("arguments",e.arguments,{});const a=isAbortable(r.cancellationToken)?{signal:r.cancellationToken.signal}:void 0;e=i.submitJob(e,a);let t=e;return this.needToWaitForJobCompletion()&&(e=await e,t=i.waitForJobCompletion(e.jobId,a)),schedulePromiseCancellation(r,t),inline(t,async o=>{if("job-succeeded"!==o.jobStatus&&"esriJobSucceeded"!==o.jobStatus)return{id:o.jobId,status:o.jobStatus,results:void 0,messages:o.messages};{o.results||await this.populateResults(i.url,o,a);const t=[],e=map(o.results,function(e,s){s=Bluebird.resolve(i.getResultData(o.jobId,s,a));return schedulePromiseCancellation(r,s),t.push(s),s}),s=Bluebird.all(t);return Task.race([s,r.cancellationToken]).finally(function(){s.isPending()&&i.cancelJob(o.jobId)}),inline(s,function(){return{id:o.jobId,status:o.jobStatus,results:map(e,e=>e.value().value),messages:o.messages}})}})}needToWaitForJobCompletion(){return isArcGisApiVersionAtLeast("4.13")}async populateResults(e,s,o){var t=e.indexOf("?");let r=e,i="";-1!==t&&(r=e.substring(0,t),i=e.substring(t)),i=i?`${i}&f=json`:"?f=json";t=`${r}/jobs/${s.jobId}${i}`,o=await esriRequest(t,o);o&&o.data&&(s.results=o.data.results)}};GeoprocessingAsync.action="gcx:wf:arcgis::GeoprocessingAsync",GeoprocessingAsync.suite="gcx:wf:builtin",GeoprocessingAsync=__decorate([activate(MapProvider)],GeoprocessingAsync);export{GeoprocessingAsync};