var DisplayForm_1;import{__decorate}from"tslib";import Bluebird from"../../deps/bluebird_fork.js";import{setFormForElement}from"./FormElementMap.js";import{FormRenderer}from"./FormRenderer.js";import{activate}from"../../Hooks.js";function loop(r,o){const s=Bluebird.defer();var e=s.promise;let i=r();return function e(){for(;i.isFulfilled();){var t;if(void 0===(t=i.value()))break;if(void 0!==(t=o(t))){s.resolve(t);break}i=r()}i.isPending()&&i.then(e),i.isRejected()&&s.reject(i.reason())}(),e}let DisplayForm=DisplayForm_1=class{static createRenderer(e,t,r){let o=t.luid,s=!1!==e.reset;var i=e.name;"string"==typeof i&&(o=":"+i);const n=this.renderers;let a=n[o];if(void 0===a){s=!0,a=n[o]=r.create();const l=a.state=t.ambient;var u=function(){delete n[o],a.dispose()};l.completion.then(u,u)}s?a.load(e.form,e.template):a.load();u="string"==typeof e.container?e.container:"default",e="string"==typeof e.region?e.region:void 0;return a.form.defaults.container=void 0!==e||"default"!==u&&"modal"!==u?{name:u,region:e}:u,a}execute(e,n,t){let a=n.state;if(void 0!==a){var r=a.outputs,o=n.caught;if(void 0!==o){var s=r.event,s=s?`Error in ${s.name} element's ${s.type} event in`:"Error in a subworkflow of";return console.error(`${s} a Display Form activity: ${o.message}\n${o.stack}`),n.fork("catch"),a.busy(),a.outputs={actor:a,state:a.form.elements,event:r.event,route:!1,error:o},DisplayForm_1.updateElementMap(a.outputs),a.outputs}o=r.result;if("string"==typeof o)return a.spinNoBlock(),a.outputs={actor:a,state:a.form.elements,event:r.event,route:!1,result:o,status:o},DisplayForm_1.updateElementMap(a.outputs),a.outputs;if("object"==typeof o&&null!==o)return a.spinNoBlock(),Bluebird.reject(o);r.route&&a.route(r.event)}else a=DisplayForm_1.createRenderer(e,n,t),n.persist(a);return a.outputs=void 0,loop(function(){const e=a.observe();return e.isFulfilled()&&void 0===e.value()&&a.dispose(),e},function(e){let t;var r=e.value;const o=("submit"===e.type||"end"===e.type)&&"string"==typeof r?r:void 0;function s(){return t=a.outputs={actor:a,state:a.form.elements,event:e,route:!1,result:o,status:o},DisplayForm_1.updateElementMap(a.outputs),a.outputs}if(!a.trap(e)&&n.hasBranch(e.routingKey)){const i=e.cancellationToken;if(void 0!==i&&null!==i){if(Bluebird.resolve(i).isFulfilled())return void a.route(e);i.then(function(){a.outputs===t&&n.goto()})}else a.busy();return n.fork(e.routingKey),n.registerForErrors(),s()}if(void 0!==o)return a.spinNoBlock(),s();a.route(e)})}static updateElementMap(e){if(e&&e.state)for(const t in e.state)setFormForElement(e.state[t],e)}};DisplayForm.renderers={},DisplayForm.action="gcx:wf:forms:form:DisplayForm",DisplayForm.suite="gcx:wf:builtin",DisplayForm=DisplayForm_1=__decorate([activate(FormRenderer)],DisplayForm);export{DisplayForm};