import { applyWSSHandler } from '../ws.mjs';
import { fastifyRequestHandler } from './fastifyRequestHandler.mjs';

/**
 * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
 *
 * @example
 * ```ts
 * import type { AnyTRPCRouter } from '@trpc/server'
 * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
 * ```
 */ /// <reference types="@fastify/websocket" />
function fastifyTRPCPlugin(fastify, opts, done) {
    fastify.removeContentTypeParser('application/json');
    fastify.addContentTypeParser('application/json', {
        parseAs: 'string'
    }, function(_, body, _done) {
        _done(null, body);
    });
    let prefix = opts.prefix ?? '';
    // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
    // @ts-expect-error property 'default' does not exists on type ...
    if (typeof fastifyTRPCPlugin.default !== 'function') {
        prefix = ''; // handled by fastify internally
    }
    fastify.all(`${prefix}/:path`, async (req, res)=>{
        const path = req.params.path;
        await fastifyRequestHandler({
            ...opts.trpcOptions,
            req,
            res,
            path
        });
    });
    if (opts.useWSS) {
        applyWSSHandler({
            ...opts.trpcOptions,
            wss: fastify.websocketServer
        });
        // eslint-disable-next-line @typescript-eslint/no-empty-function
        fastify.get(prefix ?? '/', {
            websocket: true
        }, ()=>{});
    }
    done();
}

export { fastifyTRPCPlugin };
