var RunReport_1;import{__decorate}from"tslib";import{ChannelProvider}from"../core/ChannelProvider.js";import{activateTwo}from"../../Hooks.js";import{validation}from"../../Validation.js";import{MapProvider}from"./MapProvider.js";import{checkResult,getMapControlNames,getServiceToken,runJob,getServiceUrl}from"./runUtils.js";import{getAuthenticationToken,getPortalDataUrl,getPortalItem,parsePortalItemUrl}from"./shared.js";import{ActivityNotImplementedError}from"../../Errors.js";let RunReport=RunReport_1=class{async execute(e,t,r,a){var o=validation.string("url",e.url,validation.never),n=validation.boolean("includeMap",e.includeMap,!1),i=parsePortalItemUrl(o),l=validation.array("webmapParameters",e.webmapParameters,[]),p=validation.string("resultFileName",e.resultFileName,"Report"),s=validation.array("dataSources",e.dataSources,void 0);let m,c;if(i&&(c=this.getAuthenticationToken(i.portalUrl),m=getPortalDataUrl(i,c)),!i||!i.itemId)throw Error(`Unable to get item ID for report '${o}.`);if(!m)throw Error(`Unable to get item URL for report '${o}.`);const u=t.cancellationToken;o=await getPortalItem(m,r,u);const d=r.create();u.finally(function(){d.cancel()});r=getServiceUrl(o.url,"reporting");let v;c&&"public"!==o.access&&(v=await getServiceToken(r,i.portalUrl,c,d));const g=[{name:"FeatureIds",containsMultipleValues:!0,value:void 0,values:e.featureIds||[]}];if(e.parameters)for(const R of Object.keys(e.parameters)){var f,b=e.parameters[R];b&&(f=b instanceof Array,g.push({name:R,containsMultipleValues:f,value:f?void 0:b,values:f?b:void 0}))}if(l.length)for(const h of l)for(const w of this.prepareWebmapParameters(h.mapName,h.webmap,h.scale,h.extent))g.push(w);else if(n){n=await getMapControlNames(i,r,v,d,u);if(n&&n.length){t=await this.getWebmapParameters(a,n[0],t.ambient.trivia);if(t&&t.length)for(const P of t)g.push(P)}}s={template:{itemId:i.itemId,portalUrl:i.portalUrl,title:p},parameters:g,data:s},s=(await runJob(r,v,s,d)).response.ticket;return{href:await checkResult(r,v,s,1e3,d,u)}}getAuthenticationToken(e){return getAuthenticationToken(e)}async getWebmap(e,t,r){throw console.warn("getWebmap needs to be overridden by a client implementation."),new ActivityNotImplementedError(RunReport_1.action)}async getWebmapExtent(e){throw console.warn("getWebmapExtent needs to be overridden by a client implementation."),new ActivityNotImplementedError(RunReport_1.action)}async getWebmapParameters(e,t,r){const a=e.create();await a.load();e=await this.getWebmap(a,a.getScale(),r),r=await this.getWebmapExtent(a);return this.prepareWebmapParameters(t,e,0,r)}prepareWebmapParameters(e,t,r,a){throw console.warn("prepareWebmapParameters needs to be overriden by a client implementation."),new ActivityNotImplementedError(RunReport_1.action)}};RunReport.action="gcx:wf:geocortex::RunReport",RunReport.suite="gcx:wf:builtin",RunReport=RunReport_1=__decorate([activateTwo(ChannelProvider,MapProvider)],RunReport);export{RunReport};