"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.generateModelRoutes = generateModelRoutes;
const Generator = __importStar(require("@postxl/generator"));
/**
 * Generates TRPC route for a given model.
 */
function generateModelRoutes({ model, context }) {
    const imports = Generator.ImportGenerator.from(model.trpcRoute._filePath)
        .add(model.types.id.decoder)
        .add(model.types.filter.decoder)
        .add(model.types.filter.field)
        .add({ name: context.trpcRouter._middleware.auth.name, location: context.trpcRouter._middleware._filePath })
        .add({ name: context.trpcRouter._shared.procedure.name, location: context.trpcRouter._shared._filePath })
        .add({ name: context.trpcRouter._shared.router.name, location: context.trpcRouter._shared._filePath });
    const mutations = [];
    if (model.update.kind !== 'skipped') {
        for (const action of Object.values(model.update.action.standardActions)) {
            if (action === undefined) {
                continue;
            }
            imports.add(action.payload.decoder);
            mutations.push(`
        ${action.name}: ${context.trpcRouter._shared.procedure.name}
          .use(${context.trpcRouter._middleware.auth.name})
          .input(${action.payload.decoder.name})
          .mutation(({ input, ctx }) => ctx.dispatch({scope: "${model.update.action.scope}", type: "${action.name}", payload: input})),
      `);
        }
    }
    const defaultValueMethod = `getDefault: ${context.trpcRouter._shared.procedure.name}.query(({ ctx }) => ctx.view.${model.view.service.variableName}.data.defaultValue),`;
    const variableName = model.view.service.variableName;
    return /* ts */ `

import { z } from 'zod'
  ${imports.generate()}

  export const ${model.trpcRoute.name} = router({
    ${model.defaultField === undefined ? '' : defaultValueMethod}

    ${model.trpcRoute.get.name}: procedure
      .use(authMiddleware)
      .input(${model.types.id.decoder.name})
      .query(({ input, ctx }) => ctx.view.${variableName}.get({ id: input, user: ctx.user})),
    
    ${model.trpcRoute.getMap.name}: procedure
      .use(authMiddleware)
      .query(({ ctx }) => ctx.view.${variableName}.getAll(ctx.user)),
    
    ${model.trpcRoute.getList.name}: procedure
      .use(authMiddleware)
      .query(({ ctx }) => ctx.view.${variableName}.getList(ctx.user)),

    ${model.trpcRoute.getFiltered.name}: procedure
      .use(authMiddleware)
      .input(${model.types.filter.decoder.name})
      .query(({ input, ctx }) => ctx.view.${variableName}.getFiltered({ filters: input, user: ctx.user })),

    ${model.trpcRoute.getFilterOptions.name}: procedure
      .use(authMiddleware)
      .input(
        z.object({
          field: z.enum(${model.types.filter.field.name}),
          filters: ${model.types.filter.decoder.name},
        }),
      )
      .query(({ input, ctx }) =>
        ctx.view.${variableName}.getFilterOptions({ field: input.field, filters: input.filters, user: ctx.user }),
      ),

    ${mutations.join('\n')}

  })
  `;
}
//# sourceMappingURL=model-routes.generator.js.map