"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTrpcShared = generateTrpcShared;
const Generator = __importStar(require("@postxl/generator"));
function generateTrpcShared(context) {
    const imports = Generator.ImportGenerator.from(context.trpcRouter._shared._filePath)
        .add(context.actions.dispatcher)
        .add(context.authentication.types.userInfo)
        .add(context.authentication.types.userRoles)
        .add(context.authentication.service)
        .add(context.types.user)
        .add(context.view.service);
    return /* ts */ `
// We import Fastify cookie here to ensure that the cookie plugin and its types are registered.
import '@fastify/cookie'

import { initTRPC } from '@trpc/server'
import superjson from 'superjson'
import type { Logger } from '@nestjs/common'

import { FastifyReply, FastifyRequest } from 'fastify'

${imports.generate()}

/**
* The shared context available to all routes before authentication.
*/
export type ${context.trpcRouter._shared.context.name} = {
 req: FastifyRequest
 res: FastifyReply

 logger: Logger

 /**
  * The viewer as derived from the current session.
  */
 viewer: {
   /**
    * Keycloak provided user information.
    */
   userInfo: ${context.authentication.types.userInfo.name}

   /**
    * The roles associated with the user.
    */
   userRoles: ${context.authentication.types.userRoles.name}

   /**
    * The user in the database.
    */
   user: ${context.types.user.name}

   /**
    * Whether the user is authorized to access the requested resource.
    */
   isAuthorized: boolean
 } | null

 /**
  * Authentication service to retrieve user information from the session
  */
 auth: ${context.authentication.service.name}
 
 /**
  * View service to retrieve data from the database
  */
 view: ${context.view.service.name}

 /**
  * Dispatcher service to dispatch actions
  */
 dispatcher: ${context.actions.dispatcher.name}
}

/**
 * Helper function that initializes the TRPC context. Use as the starting point for \`${context.trpcRouter._shared.router.name}\`,
 * \`${context.trpcRouter._shared.procedure.name}\` and \`${context.trpcRouter._shared.middleware.name}\`.
 */
const t = initTRPC.context<${context.trpcRouter._shared.context.name}>().create({
  transformer: superjson,
})

/**
 * The \`${context.trpcRouter._shared.router.name}\` function is function that defines a TRPC route.
 * It is used for the individual model routes as well as the app routes.
 */
export const ${context.trpcRouter._shared.router.name} = t.router

/**
 * The \`${context.trpcRouter._shared.procedure.name}\` function is used to defined an endpoint in a TRPC route.
 */
export const ${context.trpcRouter._shared.procedure.name} = t.procedure

/**
 * Function to define composable ${context.trpcRouter._shared.middleware.name}. Used for specific middleware definitions below
 */
export const ${context.trpcRouter._shared.middleware.name} = t.middleware

`;
}
//# sourceMappingURL=trpc-shared.generator.js.map