"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.generateModelController = generateModelController;
const Generator = __importStar(require("@postxl/generator"));
function generateModelController({ model, context, }) {
    const imports = Generator.ImportGenerator.from(model.restApi.controller.location)
        .add(model.decoder.decoder.zodDecoder)
        .add(model.types.id.decoder)
        .add(context.view.service);
    const { PascalCase, PascalCasePlural, camelCase } = model._conjugated;
    return /* ts */ `
import '@fastify/cookie'
import { Controller, Get, Logger, Param, Query } from '@nestjs/common'
import { createZodDto, ZodResponse } from 'nestjs-zod'
${imports.generate()}
import z from 'zod'

import { paginate, paginatedResultDecoder, paginationDecoderFromQuery } from '@utils/pagination'

const get${PascalCase}Decoder = z.object({
  id: ${model.types.id.decoder.name},
})

const get${PascalCase}ResponseDecoder = z.object({
  data: ${model.decoder.decoder.zodDecoder.name}.nullable(),
})

const get${PascalCasePlural}Decoder = paginationDecoderFromQuery

const get${PascalCasePlural}ResponseDecoder = paginatedResultDecoder(${model.decoder.decoder.zodDecoder.name})

class Get${PascalCase}Dto extends createZodDto(get${PascalCase}Decoder) {}
class Get${PascalCase}ResponseDto extends createZodDto(get${PascalCase}ResponseDecoder) {}
type Get${PascalCase}Response = z.infer<typeof get${PascalCase}ResponseDecoder>

class Get${PascalCasePlural}Dto extends createZodDto(get${PascalCasePlural}Decoder) {}
class Get${PascalCasePlural}ResponseDto extends createZodDto(get${PascalCasePlural}ResponseDecoder) {}
type Get${PascalCasePlural}Response = z.infer<typeof get${PascalCasePlural}ResponseDecoder>

@Controller('${camelCase}')
export class ${model.restApi.controller.name} {
  private readonly logger = new Logger(${model.restApi.controller.name}.name)

  constructor(private readonly viewService: ${context.view.service.name}) {}

  /**
   * Retrieve a ${model.userFriendlyName} by ID.
   */
  @ZodResponse({ type: Get${PascalCase}ResponseDto })
  @Get(':id')
  async get${PascalCase}(@Param() params: Get${PascalCase}Dto): Promise<Get${PascalCase}Response> {
    return { data: await this.viewService.${model.view.service.variableName}.get({ id: params.id, user: this.viewService.users.data.rootUser }) }
  }

  /**
   * Retrieve all ${model.userFriendlyNamePlural}.
   */
  @ZodResponse({ type: Get${PascalCasePlural}ResponseDto })
  @Get('')
  async get${PascalCasePlural}(@Query() query: Get${PascalCasePlural}Dto): Promise<Get${PascalCasePlural}Response> {
    const data = await this.viewService.${model.view.service.variableName}.getList(this.viewService.users.data.rootUser)
    return paginate(data, query)
  }
}
`;
}
//# sourceMappingURL=model-controller.generator.js.map