const DEV_SERVICE_URL="http://localhost:3000/";async function checkJobStatus(e,t,r,o,n){const s=o.new();if(n.finally(function(){s.cancel()}),s.request.url=`${e}/job/artifacts?ticket=${r}`,t&&(s.request.headers.Authorization=`Bearer ${t}`),!await s.send())throw console.error("Unable to check job status.",s.response.message),new Error("Unable to check job status.");const a=JSON.parse(s.response.content);if(console.debug(a),a.results&&a.results.some(e=>"JobQuit"===e.$type)){t=a.results.filter(e=>"JobResult"===e.$type)[0];if(t)return`${e}/job/result?ticket=${r}&tag=${t.tag}`;console.error("Job failed.");throw new Error("Job failed.")}return null}async function checkResult(e,t,r,o,n,s){var a=await checkJobStatus(e,t,r,n,s);return a||(await delay(o),checkResult(e,t,r,1.1*o,n,s))}async function delay(t){return new Promise(e=>setTimeout(e,t))}async function getMapControlNames(e,t,r,o,n){const s=[];n=await getTemplateMetadata(e,t,r,o,n);if(n&&n.response&&n.response.controls)for(const a of n.response.controls)if("Map"===a.controlType){const c=a.purpose;s.push(c.substring(0,c.indexOf(".")))}return s}async function getServiceToken(e,t,r,o){const n=o.new();if(n.request.url=`${e}/auth/token/run`,n.request.method="POST",n.request.headers={"Content-Type":"application/json"},n.request.json={accessToken:r,portalUrl:t},!await n.send())throw console.error("Unable to generate run token.",n.response.message),Error("Unable to generate run token");t=JSON.parse(n.response.content);return(t.response||t).token}async function getTemplateMetadata(e,t,r,o,n){const s=o.new();if(n.finally(function(){s.cancel()}),s.request.url=`${t}/job/metadata`,s.request.method="POST",s.request.json={template:{itemId:e.itemId,portalUrl:e.portalUrl}},s.request.headers["Content-Type"]="application/json",r&&(s.request.headers.Authorization=`Bearer ${r}`),!await s.send())throw console.error("Unable to retrieve template metadata.",s.response.message),new Error("Unable to retrieve template metadata.");r=JSON.parse(s.response.content);return console.debug(r),r}async function runJob(e,t,r,o){const n=o.new();if(n.request.url=`${e}/job/run`,n.request.method="POST",n.request.headers={"Content-Type":"application/json"},t&&(n.request.headers.Authorization=`Bearer ${t}`),n.request.json=r,!await n.send())throw console.error("Unable to run job.",n.response.message),Error("Unable to run job");r=JSON.parse(n.response.content);return console.debug("job/run",r),r}function getServiceUrl(e,t){let r=e;return r?0===r.toLowerCase().indexOf(DEV_SERVICE_URL)?r="http://localhost:43222/":-1!==(e=r.indexOf("#"))&&(r=r.substring(0,e)):r=`https://apps.geocortex.com/${t}/`,r+="/"===r.slice(-1)?"service":"/service",r}export{checkJobStatus,checkResult,getMapControlNames,getServiceToken,runJob,getServiceUrl};