!function(e){"object"==typeof module&&"object"==typeof module.exports?e(require,exports):"function"==typeof define&&define.amd&&define(["require","exports","tslib","../core/ChannelProvider","../../Hooks","../../Validation","../../Errors","./shared","./runUtils","../constants"],e)}(function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var d=e("tslib"),r=e("../core/ChannelProvider"),n=e("../../Hooks"),l=e("../../Validation"),a=e("../../Errors"),f=e("./shared"),p=e("./runUtils"),w=e("../constants"),r=(o.prototype.execute=function(a,u,c){return d.__awaiter(this,void 0,void 0,function(){var t,r,n,o,i,s;return d.__generator(this,function(e){switch(e.label){case 0:return(t=l.validation.string("url",a.url,l.validation.never),r=f.parsePortalItemUrl(t))?(n=this.getAuthenticationToken(r.portalUrl),i=f.getPortalDataUrl(r,n),o=u.cancellationToken,[4,f.getPortalItem(i,c,o)]):[3,2];case 1:if(i=e.sent(),s=c.create(),o.finally(function(){s.cancel()}),this.isServerWorkflow(i))return[2,this.runServerWorkflow(i,a.arguments,s,o,r.portalUrl,n)];e.label=2;case 2:return[2,this.runClientWorkflow(t,a.arguments,u)]}})})},o.prototype.getAuthenticationToken=function(e){return f.getAuthenticationToken(e)},o.prototype.isServerWorkflow=function(e){if(null==e)return!1;var t=e.properties;if(null==t)return!1;e=t.isServerWorkflow,t=t.serverUrl;return!(!0!==e||!t)},o.prototype.runServerWorkflow=function(u,c,l,f,h,v){return d.__awaiter(this,void 0,void 0,function(){var t,r,n,o,i,s,a;return d.__generator(this,function(e){switch(e.label){case 0:if(t=(t=u.properties.serverUrl).replace(/\/+$/,""),!v||"public"===u.access)return[3,7];e.label=1;case 1:return e.trys.push([1,3,,7]),[4,p.getServiceToken(t,h,v,l)];case 2:return r=e.sent(),[3,7];case 3:return(n=e.sent(),w.oldDefaultServerVirtualPath.test(t))?(t=t.replace(w.oldDefaultServerVirtualPath,w.defaultServerVirtualPath),[4,p.getServiceToken(t,h,v,l)]):[3,5];case 4:return r=e.sent(),[3,6];case 5:throw n;case 6:return[3,7];case 7:o={workflow:{id:u.id},inputs:c},e.label=8;case 8:return e.trys.push([8,10,,14]),[4,p.runJob(t,r,o,l)];case 9:return i=e.sent(),[3,14];case 10:return(s=e.sent(),w.oldDefaultServerVirtualPath.test(t))?(t=t.replace(w.oldDefaultServerVirtualPath,w.defaultServerVirtualPath),[4,p.runJob(t,r,o,l)]):[3,12];case 11:return i=e.sent(),[3,13];case 12:throw s;case 13:return[3,14];case 14:return a=i.ticket,[4,this.checkResult(t,r,a,1e3,l,f)];case 15:return[2,{result:e.sent()}]}})})},o.prototype.delay=function(t){return d.__awaiter(this,void 0,void 0,function(){return d.__generator(this,function(e){return[2,new Promise(function(e){return setTimeout(e,t)})]})})},o.prototype.checkResult=function(r,n,o,i,s,a){return d.__awaiter(this,void 0,void 0,function(){var t;return d.__generator(this,function(e){switch(e.label){case 0:return[4,this.checkJobStatus(r,n,o,s,a)];case 1:return(t=e.sent())?[2,t]:[3,2];case 2:return[4,this.delay(i)];case 3:return e.sent(),[4,this.checkResult(r,n,o,1.1*i,s,a)];case 4:return[2,e.sent()]}})})},o.prototype.checkJobStatus=function(r,n,o,i,s){return d.__awaiter(this,void 0,void 0,function(){var t;return d.__generator(this,function(e){switch(e.label){case 0:return i=i.new(),s.finally(function(){i.cancel()}),i.request.url=r+"/job/artifacts?ticket="+o,n&&(i.request.headers.Authorization="Bearer "+n),[4,i.send()];case 1:if(!e.sent())throw console.error("Unable to check workflow status.",i.response.message),new Error("Unable to check workflow status.");if(t=JSON.parse(i.response.content),console.debug(t),t.results&&t.results.some(function(e){return"JobQuit"===e.$type})){if(t=t.results.filter(function(e){return"JobResult"===e.$type})[0])return[2,t.outputs];throw t=r+"/job/logs?ticket="+o+"&",console.error("Server workflow failed.",t),new Error("Server workflow failed.")}return[2,null]}})})},o.prototype.runClientWorkflow=function(o,i,s){return d.__awaiter(this,void 0,void 0,function(){var t,r,n;return d.__generator(this,function(e){switch(e.label){case 0:t=s.ambient.trivia,r=s.ambient.activityContexts,r=r?r.$$form:void 0,e.label=1;case 1:return e.trys.push([1,3,,4]),n=r?{$$form:r}:{},[4,t.runWorkflow(o,i,n,t.isDescendantOf)];case 2:return[2,{result:e.sent()}];case 3:if(!((n=e.sent())instanceof a.ActivityCancellationError))throw n;return s.ambient.reject(n),[3,4];case 4:return[2]}})})},o.action="gcx:wf:core::RunWorkflow",o.suite="gcx:wf:builtin",d.__decorate([n.activate(r.ChannelProvider)],o));function o(){}t.RunWorkflow=r});