import{__decorate}from"tslib";import Bluebird from"../../deps/bluebird_fork.js";import{validation}from"../../Validation.js";import{GenericActivityError}from"../../Errors.js";import{ChannelProvider}from"../core/ChannelProvider.js";import{activate}from"../../Hooks.js";class WorkflowSchema{constructor(){this.inputs={}}}let RunEssentialsWorkflow=class{execute(r,n,o){const s=validation.string("url",r.url,validation.never);return this.getWorkflowSchema(s,o).then(e=>{var t=this.validateInputs(r.arguments,e);if(t)return Bluebird.reject(t);const l=o.create(void 0,"geocortex");l.request.url=s+"/run",l.timeout=validation.number("timeout",r.timeout,6e4),l.request.method="POST",l.request.headers["Content-Type"]="application/x-www-form-urlencoded",l.request.json={inargs:this.constructInArgs(r.arguments,e),f:"json"},n.cancellationToken.finally(function(){l.cancel()});const u=(new Date).valueOf();return l.send().then(function(a){return Bluebird.attempt(function(){var e=(new Date).valueOf()-u;const t={result:{}};if(a){var r=l.getResponseData(l.response.payload);if("Completed"===r.status){r=r.outputs;if(r)for(const i of r){var n=i.name,o=i.value;t.result[n]=o}return t}}const s=new GenericActivityError(void 0,"Error running Essentials Workflow activity.");return s.timedOut=e>=l.timeout,s.elapsed=e,s.error=l.response.error,s.partialResult=t,Bluebird.reject(s)})})})}isString(e){return"string"==typeof e}getWorkflowSchema(e,t){const i=t.create(void 0,"geocortex");return i.request.url=e,i.request.json={f:"json",deep:!0},i.send().then(e=>{if(e){const o=new WorkflowSchema;var t=i.getResponseData(i.response.payload).inputs;if(t)for(const s of t){var r=s.name,n=s.typeName;this.isString(r)&&this.isString(n)&&(o.inputs[r]={name:r,typeName:n})}return Bluebird.resolve(o)}t=new GenericActivityError(void 0,"Error running Essentials Workflow activity. Error retrieving workflow schema.");return Bluebird.reject(t)})}validateInputs(e,t){const r=[],n=[];for(const s in t.inputs)e.hasOwnProperty(s)||r.push(s);for(const i in e)t.inputs.hasOwnProperty(i)||n.push(i);if(0<r.length+n.length){let e="",t="";r.length&&(e=" Missing parameters: "+r),n.length&&(t=" Unexpected parameters: "+n);var o="Invalid parameters supplied to Essentials Workflow activity."+e+t;return new GenericActivityError(void 0,o)}}constructInArgs(e,t){const r=[];for(const n in e)r.push({name:n,typeName:t.inputs[n].typeName,value:e[n]});return r}};RunEssentialsWorkflow.action="gcx:wf:essentials::RunEssentialsWorkflow",RunEssentialsWorkflow.suite="gcx:wf:builtin",RunEssentialsWorkflow=__decorate([activate(ChannelProvider)],RunEssentialsWorkflow);export{RunEssentialsWorkflow};