import { APP_LAZY_ROUTE_BUNDLES, AUTHENTICATED_LAZY_ROUTE_BUNDLES, findAuthenticatedLazyRouteBundle } from "./lazy-bundles.js";
import { Hono } from "hono";
//#region src/gateway/hono/routes/lazy-fallback.ts
const authenticatedSubApps = /* @__PURE__ */ new Map();
const appSubApps = /* @__PURE__ */ new Map();
const authenticatedLoadPromises = /* @__PURE__ */ new Map();
const appLoadPromises = /* @__PURE__ */ new Map();
function getLoadedLazyRouteBundleIdsForTests() {
	return {
		authenticated: [...authenticatedSubApps.keys()],
		app: [...appSubApps.keys()]
	};
}
function resetLazyRouteBundlesForTests() {
	authenticatedSubApps.clear();
	appSubApps.clear();
	authenticatedLoadPromises.clear();
	appLoadPromises.clear();
}
async function ensureAuthenticatedLazyBundle(bundleId, deps) {
	const existing = authenticatedSubApps.get(bundleId);
	if (existing) return existing;
	let pending = authenticatedLoadPromises.get(bundleId);
	if (!pending) {
		const bundle = AUTHENTICATED_LAZY_ROUTE_BUNDLES.find((entry) => entry.id === bundleId);
		if (!bundle) return null;
		pending = (async () => {
			const mod = await bundle.load();
			const sub = new Hono();
			mod.register(sub, deps);
			authenticatedSubApps.set(bundleId, sub);
			authenticatedLoadPromises.delete(bundleId);
			return sub;
		})();
		authenticatedLoadPromises.set(bundleId, pending);
	}
	return pending;
}
async function ensureAppLazyBundle(bundleId, params) {
	const existing = appSubApps.get(bundleId);
	if (existing) return existing;
	let pending = appLoadPromises.get(bundleId);
	if (!pending) {
		const bundle = APP_LAZY_ROUTE_BUNDLES.find((entry) => entry.id === bundleId);
		if (!bundle) return null;
		pending = (async () => {
			const mod = await bundle.load();
			const sub = new Hono();
			if (mod.registerOnApp) mod.registerOnApp(sub, params.service);
			appSubApps.set(bundleId, sub);
			appLoadPromises.delete(bundleId);
			return sub;
		})();
		appLoadPromises.set(bundleId, pending);
	}
	return pending;
}
async function forwardToSubApp(c, sub) {
	try {
		return await sub.fetch(c.req.raw, c.env, c.executionCtx);
	} catch (error) {
		if (error instanceof Error && error.message.includes("ExecutionContext")) return sub.fetch(c.req.raw, c.env);
		throw error;
	}
}
function registerAuthenticatedLazyRouteFallback(authenticated, deps) {
	authenticated.all("*", async (c) => {
		const bundle = findAuthenticatedLazyRouteBundle(c.req.path);
		if (!bundle) return c.json({ error: "Not found" }, 404);
		const sub = await ensureAuthenticatedLazyBundle(bundle.id, deps);
		if (!sub) return c.json({ error: "Not found" }, 404);
		return forwardToSubApp(c, sub);
	});
}
function mountAppLazyRoutePrefixes(app, params) {
	for (const bundle of APP_LAZY_ROUTE_BUNDLES) {
		const handler = async (c) => {
			const sub = await ensureAppLazyBundle(bundle.id, params);
			if (!sub) return c.json({ error: "Not found" }, 404);
			return forwardToSubApp(c, sub);
		};
		for (const prefix of bundle.prefixes) {
			app.all(prefix, handler);
			app.all(`${prefix}/*`, handler);
		}
	}
}
//#endregion
export { getLoadedLazyRouteBundleIdsForTests, mountAppLazyRoutePrefixes, registerAuthenticatedLazyRouteFallback, resetLazyRouteBundlesForTests };

//# sourceMappingURL=lazy-fallback.js.map