import { Federation, FederationFetchOptions } from "@fedify/fedify";

//#region src/index.d.ts

interface ContextDataFactory<TContextData> {
  (request: Request): TContextData | Promise<TContextData>;
}
type ErrorHandlers = Omit<FederationFetchOptions<unknown>, "contextData">;
/**
 * Wrapper function for Next.js middleware to integrate with the
 * {@link Federation} object.
 *
 * @template TContextData A type of the context data for the
 *                         {@link Federation} object.
 * @param federation A {@link Federation} object to integrate with Next.js.
 * @param contextDataFactory A function to create a context data for the
 *                         {@link Federation} object.
 * @param errorHandlers A set of error handlers to handle errors during
 *                      the federation fetch.
 * @returns A Next.js middleware function to integrate with the
 *          {@link Federation} object.
 *
 * @example
 * ```ts ignore
 * import { fedifyWith } from "@fedify/next";
 * import { federation } from "./federation";
 *
 * export default fedifyWith(federation)(
 *   function (request: Request) {
 *     // You can add custom logic here for other requests
 *     // except federation requests.  If there is no custom logic,
 *     // you can omit this function.
 *   }
 * )
 *
 * // This config makes middleware process only requests with the
 * // "Accept" header matching the federation accept regex.
 * // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
 * export const config = {
 *   runtime: "nodejs",
 *   matcher: [{
 *     source: "/:path*",
 *     has: [
 *       {
 *         type: "header",
 *         key: "Accept",
 *         value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
 *       },
 *     ],
 *   }],
 * };
 * ```
 */
declare const fedifyWith: <TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>) => (middleware?: (request: Request) => unknown) => (request: Request) => unknown;
/**
 * Check if the request has the "Accept" header matching the federation
 * accept regex.
 *
 * @param request The request to check.
 * @returns `true` if the request has the "Accept" header matching
 *                    the federation accept regex, `false` otherwise.
 */
declare const hasFederationAcceptHeader: (request: Request) => boolean;
/**
 * Create a Next.js handler to integrate with the {@link Federation} object.
 *
 * @template TContextData A type of the context data for the
 *                        {@link Federation} object.
 * @param federation A {@link Federation} object to integrate with Next.js.
 * @param contextDataFactory A function to create a context data for the
 *                           {@link Federation} object.
 * @param errorHandlers A set of error handlers to handle errors during
 *                      the federation fetch.
 * @returns A Next.js handler.
 */
declare function integrateFederation<TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>): (request: Request) => Promise<Response>;
//#endregion
export { fedifyWith, hasFederationAcceptHeader, integrateFederation };