import "@js-temporal/polyfill";
import "urlpattern-polyfill";
import { getLogger } from "@logtape/logtape";
//#region src/x/sveltekit.ts
/**
* Fedify with SvelteKit
* =====================
*
* This module provides a [SvelteKit] hook to integrate with the Fedify.
*
* [SvelteKit]: https://kit.svelte.dev/
*
* @deprecated This module has been moved to a separate package.
*             Install and import from `@fedify/sveltekit` instead.
*             This module will be removed in Fedify v2.0.
*
* @module
* @since 1.3.0
*/
/**
* Create a SvelteKit hook handler to integrate with the {@link Federation}
* object.
*
* @deprecated This function has been moved to `@fedify/sveltekit` package.
*             Import `fedifyHook` from `@fedify/sveltekit` instead.
*             This function will be removed in Fedify v2.0.
*
* @example hooks.server.ts
* ``` typescript
* import { federation } from "./federation"; // Import the `Federation` object
*
* export const handle = fedifyHook(federation, () => undefined);
* ```
*
* @template TContextData A type of the context data for the {@link Federation}
*                         object.
* @param federation A {@link Federation} object to integrate with SvelteKit.
* @param createContextData A function to create a context data for the
*                          {@link Federation} object.
* @returns A SvelteKit hook handler.
* @since 1.3.0
*/
function fedifyHook(federation, createContextData) {
	getLogger([
		"fedify",
		"federation",
		"sveltekit"
	]).warn("The `@fedify/fedify/x/sveltekit` module is deprecated; use `fedifyHook` from `@fedify/sveltekit` package instead.");
	return async ({ event, resolve }) => {
		return await federation.fetch(event.request, {
			contextData: await createContextData(event),
			...integrateFetchOptions({
				event,
				resolve
			})
		});
	};
}
function integrateFetchOptions({ event, resolve }) {
	return {
		async onNotFound() {
			return await resolve(event);
		},
		async onNotAcceptable() {
			const res = await resolve(event);
			if (res.status !== 404) return res;
			return new Response("Not acceptable", {
				status: 406,
				headers: {
					"Content-Type": "text/plain",
					Vary: "Accept"
				}
			});
		}
	};
}
//#endregion
export { fedifyHook };
