import { createGenesisAuth } from "../auth/index.js";
import { isAnonymousAuthResult } from "../auth/types.js";
/**
 * TanStack Start server middleware handler. Wrap with `createMiddleware` from `@tanstack/react-start`:
 *
 * ```ts
 * import { createMiddleware } from '@tanstack/react-start';
 * import { genesisAuthTanStackStart } from '@agentuity/genesis/tanstack-start';
 *
 * export const genesisAuth = createMiddleware().server(genesisAuthTanStackStart({ projectId }));
 * ```
 */
export function genesisAuthTanStackStart(config) {
    const auth = createGenesisAuth(config);
    return async ({ request, next, context }) => {
        const result = await auth.authenticate(request);
        if (!result.ok) {
            if (isAnonymousAuthResult(result)) {
                return next({ context });
            }
            return new Response(result.message, { status: result.status });
        }
        return next({
            context: {
                ...context,
                genesisIdentity: result.identity,
            },
        });
    };
}
//# sourceMappingURL=tanstack-start.js.map