/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../request.js";import{strict as o}from"../../core/jsonMap.js";import{JSONSupport as s}from"../../core/JSONSupport.js";import{onAbort as r,createAbortError as i}from"../../core/promiseUtils.js";import{property as a,subclass as c}from"../../core/accessorSupport/decorators.js";import{parseUrl as n}from"../utils.js";import l from"../geoprocessor/GPOptions.js";import{gpEncode as u,decode as b}from"../geoprocessor/utils.js";import m from"./GPMessage.js";import{enumeration as j}from"../../core/accessorSupport/decorators/enumeration.js";var p;const h=o()({esriJobCancelled:"job-cancelled",esriJobCancelling:"job-cancelling",esriJobDeleted:"job-deleted",esriJobDeleting:"job-deleting",esriJobTimedOut:"job-timed-out",esriJobExecuting:"job-executing",esriJobFailed:"job-failed",esriJobNew:"job-new",esriJobSubmitted:"job-submitted",esriJobSucceeded:"job-succeeded",esriJobWaiting:"job-waiting"},{ignoreUnknown:!1}),d=1e3;let g=class extends s{static{p=this}constructor(e){super(e),this.jobId=null,this.jobStatus=null,this.messages=null,this.progress=null,this.requestOptions=null,this.sourceUrl=null,this._cancelJobTimer=void 0,this._jobCompletionTimer=void 0}async cancelJob(e){const{jobId:o,sourceUrl:s}=this,{path:a}=n(s),c={...this.requestOptions,...e,query:{f:"json"}},l=`${a}/jobs/${o}/cancel`,{data:u}=await t(l,c),{messages:b,jobStatus:m,progress:j}=p.fromJSON(u);return this.set({messages:b,jobStatus:m,progress:j}),"job-cancelled"===m?this:new Promise((t,o)=>{r(c.signal,()=>{this.clearCancelJobTimer(),o(i())}),this.clearCancelJobTimer();const s=()=>{this._cancelJobTimer||o(i()),this.checkJobStatus(e).then(({jobStatus:e})=>{switch(e){case"job-cancelling":default:this._cancelJobTimer=setTimeout(s,d);break;case"job-deleted":case"job-deleting":case"job-executing":case"job-failed":case"job-new":case"job-submitted":case"job-succeeded":case"job-timed-out":case"job-waiting":o(this);break;case"job-cancelled":t(this)}}).catch(e=>{o(e)})};this._cancelJobTimer=setTimeout(s,d)})}destroy(){clearInterval(this._cancelJobTimer),clearInterval(this._jobCompletionTimer)}async checkJobStatus(e){const{path:o}=n(this.sourceUrl),s={...this.requestOptions,...e,query:{...e?.query,f:"json"}},r=`${o}/jobs/${this.jobId}`,{data:i}=await t(r,s),{messages:a,jobStatus:c,progress:l}=p.fromJSON(i);return this.set({messages:a,jobStatus:c,progress:l}),this}async fetchResultData(e,o,s){o=l.from(o||{});const{returnColumnName:r,returnFeatureCollection:i,returnM:a,returnZ:c,outSpatialReference:m}=o,{path:j}=n(this.sourceUrl),p={returnColumnName:r||null,returnFeatureCollection:i||null,returnM:a||null,returnZ:c||null,outSR:m,returnType:"data",f:"json",...s?.query},h=u(p,null),d={...this.requestOptions,...s,query:h},g=`${j}/jobs/${this.jobId}/results/${e}`,{data:J}=await t(g,d);return b(J)}async fetchResultImage(e,o,s){const{path:r}=n(this.sourceUrl),i={...o.toJSON(),f:"json"},a=u(i),c={...this.requestOptions,...s,query:a},l=`${r}/jobs/${this.jobId}/results/${e}`,{data:m}=await t(l,c);return b(m)}async fetchResultMapImageLayer(){const{path:e}=n(this.sourceUrl),t=e.indexOf("/GPServer/"),o=`${e.slice(0,Math.max(0,t))}/MapServer/jobs/${this.jobId}`;return new(0,(await import("../../layers/MapImageLayer.js")).default)({url:o})}async waitForJobCompletion(e={}){const{interval:t=d,signal:o,statusCallback:s,apiKey:a}=e;return new Promise((e,c)=>{r(o,()=>{this.clearJobCompletionTimer(),c(i())}),this.clearJobCompletionTimer();const n=()=>{this._jobCompletionTimer||c(i()),this.checkJobStatus({query:{token:a}}).then(({jobStatus:o})=>{switch(o){case"job-succeeded":e(this);break;case"job-executing":case"job-new":case"job-submitted":case"job-waiting":s&&s(this),this._jobCompletionTimer=setTimeout(n,t);break;case"job-cancelled":case"job-cancelling":case"job-deleted":case"job-deleting":case"job-failed":case"job-timed-out":c(this);break;default:this._jobCompletionTimer=setTimeout(n,t)}}).catch(e=>{c(e)})};this._jobCompletionTimer=setTimeout(n,t)})}clearCancelJobTimer(){clearTimeout(this._cancelJobTimer),this._cancelJobTimer=void 0}clearJobCompletionTimer(){clearTimeout(this._jobCompletionTimer),this._jobCompletionTimer=void 0}};e([a()],g.prototype,"jobId",void 0),e([j(h,{ignoreUnknown:!1})],g.prototype,"jobStatus",void 0),e([a({type:[m]})],g.prototype,"messages",void 0),e([a()],g.prototype,"progress",void 0),e([a()],g.prototype,"requestOptions",void 0),e([a({json:{write:!0}})],g.prototype,"sourceUrl",void 0),g=p=e([c("esri.rest.support.JobInfo")],g);export{g as default};
