import{r as e}from"./chunk-BHKSVoKr.js";import{T as t,n,o as r}from"./dist-BYo8Gno2.js";import{Dn as i,Gt as a,H as o,Mn as s,Q as c,S as l,X as u,a as d,at as f,en as p,g as m,k as h,n as g,ot as _,r as v,st as y,un as b,yn as x}from"./attribute-changes-CXiYB8h9.js";import{s as S}from"./dist-CSfKNxQW.js";var C=e({getHookByToken:()=>T,resumeHook:()=>E,resumeWebhook:()=>D});async function w(e){let t=await i(),n=await t.hooks.getByToken(e),r=await t.runs.get(n.runId),a=await t.getEncryptionKeyForRun?.(r),o=a?await s(a):void 0;return n.metadata!==void 0&&(n.metadata=await h(n.metadata,n.runId,o)),{hook:n,run:r,encryptionKey:o}}async function T(e){let{hook:t}=await w(e);return t}async function E(e,t,n){return await v(()=>c(`hook.resume`,async r=>{let c=await i();try{let i,f,h;if(typeof e==`string`){let t=await w(e);i=t.hook,f=t.run,h=n??t.encryptionKey}else if(i=e,f=await c.runs.get(i.runId),n)h=n;else{let e=await c.getEncryptionKeyForRun?.(f);h=e?await s(e):void 0}r?.setAttributes({...y(i.token),..._(i.hookId),...p(i.runId)});let v=f.executionContext?.workflowCoreVersion,x=m(typeof v==`string`?v:void 0);x.supportedFormats.has(b.ENCRYPTED)||(h=void 0);let C=(f.specVersion??0)>=5&&x.supportedFormats.has(b.GZIP),T=[],E=S(i.specVersion),D=await l(t,i.runId,h,T,globalThis,E,x.framedByteStreams,C);g(Promise.all(T),e=>{e!==void 0&&o.warn(`Background flush of hook payload ops failed`,{workflowRunId:i.runId,hookId:i.hookId,error:e instanceof Error?e.message:String(e)})}),await c.events.create(i.runId,{eventType:`hook_received`,specVersion:5,correlationId:i.hookId,eventData:{...E?{}:{token:i.token},payload:D}},{v1Compat:E}),r?.setAttributes({...a(f.workflowName)});let O=await u(f.executionContext?.traceCarrier);return O&&r?.addLink?.(O),await c.queue(d(f.workflowName),{runId:i.runId,traceCarrier:f.executionContext?.traceCarrier??void 0},{deploymentId:f.deploymentId,specVersion:f.specVersion??1}),i}catch(t){throw r?.setAttributes({...y(typeof e==`string`?e:e.token),...f(!1)}),t}}))}async function D(e,i){let{hook:a,encryptionKey:o}=await w(e);if(a.isWebhook===!1)throw new r(e);let s,c;if(a.metadata&&typeof a.metadata==`object`&&`respondWith`in a.metadata)if(a.metadata.respondWith===`manual`){let{readable:e,writable:t}=new TransformStream;c=e,i[x]=t}else if(a.metadata.respondWith instanceof Response)s=a.metadata.respondWith;else throw new t(`Invalid \`respondWith\` value: ${a.metadata.respondWith}`,{slug:n.WEBHOOK_INVALID_RESPOND_WITH_VALUE});else s=new Response(null,{status:202});if(await E(a,i,o),c){let e=c.getReader(),t=await e.read();t.value&&(s=t.value),e.cancel()}if(!s)throw new t(`Workflow run did not send a response`,{slug:n.WEBHOOK_RESPONSE_NOT_SENT});return s}export{C as i,E as n,D as r,T as t};