import{pathToFileURL as d}from"url";import{isDefined as l}from"../../../../utils/guards/is-defined.js";import{slash as m}from"../../../../utils/path/slash.js";import{isDevelopMode as s}from"../../../utils/envs/is-develop-mode.js";import{compose as w}from"./hono-compose.js";function h(r){return async(e,o)=>{await r.waitForPluginsLifecycle();const a=await c(r.serverOutDir),n=(await Promise.all(r.getAllMiddleware().map(({id:t})=>a[t]).map(async t=>typeof t=="function"?(await t()).default:void 0))).filter(l),i=await w(n)(e,o,r);if(!i.finalized)throw new Error("Context is not finalized. Did you forget to return a Response object or `await next()`?");return i.res}}async function c(r){try{let e;return process.env.REDOCLY_STATIC_BUNDLE?e=await import("@portal/middleware"):e=await import(d(`${m(r)}/middleware-entry.js`)+"?"+new Date),e.middleware}catch(e){if(s())return{};throw e}}export{h as dynamicMiddleware};
