import "@js-temporal/polyfill";
import "urlpattern-polyfill";
import { getLogger } from "@logtape/logtape";
//#region src/x/hono.ts
/**
* Fedify with Hono
* ================
*
* This module provides a [Hono] middleware to integrate with the Fedify.
*
* [Hono]: https://hono.dev/
*
* @deprecated This module has been moved to a separate package.
*             Install and import from `@fedify/hono` instead.
*             This module will be removed in Fedify v2.0.
*
* @module
* @since 0.6.0
*/
/**
* Create a Hono middleware to integrate with the {@link Federation} object.
*
* @deprecated This function has been moved to `@fedify/hono` package.
*             Import `federation` from `@fedify/hono` instead.
*             This function will be removed in Fedify v2.0.
*
* @template TContextData A type of the context data for the {@link Federation}
*                         object.
* @template THonoContext A type of the Hono context.
* @param federation A {@link Federation} object to integrate with Hono.
* @param contextDataFactory A function to create a context data for the
*                           {@link Federation} object.
* @returns A Hono middleware.
*/
function federation(federation, contextDataFactory) {
	getLogger([
		"fedify",
		"federation",
		"hono"
	]).warn("The `@fedify/fedify/x/hono` module is deprecated; use `federation` from `@fedify/hono` package instead.");
	return async (ctx, next) => {
		let contextData = contextDataFactory(ctx);
		if (contextData instanceof Promise) contextData = await contextData;
		return await federation.fetch(ctx.req.raw, {
			contextData,
			...integrateFetchOptions(ctx, next)
		});
	};
}
function integrateFetchOptions(ctx, next) {
	return {
		async onNotFound(_req) {
			await next();
			return ctx.res;
		},
		async onNotAcceptable(_req) {
			await next();
			if (ctx.res.status !== 404) return ctx.res;
			return new Response("Not acceptable", {
				status: 406,
				headers: {
					"Content-Type": "text/plain",
					Vary: "Accept"
				}
			});
		}
	};
}
//#endregion
export { federation };
