"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.backendGenerator = exports.backendGeneratorId = void 0;
const path = __importStar(require("node:path"));
const Generator = __importStar(require("@postxl/generator"));
const api_config_generator_1 = require("./generators/api-config.generator");
const api_module_generator_1 = require("./generators/api-module.generator");
const dotenv_example_generator_1 = require("./generators/dotenv-example.generator");
const jest_config_generator_1 = require("./generators/jest.config.generator");
const main_generator_1 = require("./generators/main.generator");
const tsconfig_generator_1 = require("./generators/tsconfig.generator");
const baseDependencies = [
    { packageName: '@fastify/cookie', version: '11.2.0' },
    { packageName: '@fastify/cors', version: '11.2.0' },
    { packageName: '@fastify/helmet', version: '13.0.2' },
    { packageName: '@nestjs/common', version: '11.1.9' },
    { packageName: '@nestjs/core', version: '11.1.9' },
    { packageName: '@nestjs/platform-fastify', version: '11.1.9' },
    { packageName: '@nestjs/schematics', version: '11.0.9' },
    { packageName: Generator.toPostXlPackageName('@postxl/utils') },
    { packageName: 'colors', version: '1.4.0' },
    { packageName: 'jsonwebtoken', version: '9.0.3' },
    { packageName: 'jwks-rsa', version: '3.2.0' },
    // Do not upgrade to above 8.1 as 9.0 dropped CommonJS support and NestJS still uses CommonJS!
    { packageName: 'serialize-error', version: '8.1.0' },
    { packageName: 'zod', version: '4.3.5' },
    { packageName: 'zod-validation-error', version: '5.0.0' },
];
const baseDevDependencies = [
    { packageName: 'fastify', version: '5.6.2' },
    { packageName: 'express', version: '5.2.1' },
    { packageName: '@dotenvx/dotenvx', version: '1.51.2' },
    { packageName: '@jest/globals', version: '30.2.0' },
    { packageName: '@nestjs/cli', version: '11.0.14' },
    { packageName: '@types/express', version: '5.0.6' },
    { packageName: '@types/node', version: '25.0.3' },
    { packageName: 'jest', version: '30.2.0' },
    { packageName: 'ts-jest', version: '29.4.6' },
    { packageName: 'tsconfig-paths', version: '4.2.0' },
    { packageName: 'typescript', version: '5.9.3' },
    { packageName: 'webpack', version: '5.104.1' },
];
const baseScripts = [
    { name: 'build', command: 'nest build' },
    { name: 'start', command: 'node ./dist/apps/api/apps/api/src/main.js' },
    {
        name: 'dev',
        command: 'dotenvx run -f ./apps/api/.env --env STATEFUL=false --env AUTH=false -- nest start --debug --watch',
    },
    {
        name: 'dev:auth',
        command: 'dotenvx run -f ./apps/api/.env --env STATEFUL=false --env AUTH=true -- nest start --debug --watch',
    },
    {
        name: 'dev:stateful',
        command: 'dotenvx run -f ./apps/api/.env --env STATEFUL=true --env AUTH=false -- nest start --debug --watch',
    },
    {
        name: 'dev:stateful:auth',
        command: 'dotenvx run -f ./apps/api/.env --env STATEFUL=true --env AUTH=true -- nest start --debug --watch',
    },
    { name: 'lint', command: 'eslint .' },
    { name: 'test:jest', command: 'jest' },
    { name: 'test:types', command: 'tsc --noEmit' },
];
exports.backendGeneratorId = Generator.toGeneratorInterfaceId('backend-core');
exports.backendGenerator = {
    id: exports.backendGeneratorId,
    register: (context) => {
        const backend = {
            modules: [],
            packageJson: {
                name: Generator.toPostXlPackageName(`@postxl/${context.schema.slug}-api`),
                description: `API for the ${context.schema.name} project`,
                dependencies: baseDependencies,
                devDependencies: baseDevDependencies,
                scripts: baseScripts,
                isInPostXlWorkspace: context.schema.projectType === 'workspace',
            },
            tsConfigReferences: ['utils'],
        };
        return {
            ...context,
            backend,
        };
    },
    generate: async (ctx) => {
        const vfs = new Generator.VirtualFileSystem();
        // Load the template
        await vfs.loadFolder({
            diskPath: path.resolve(__dirname, './template'),
            // Libs are added separately by the modules' VFSs
            filter: (path) => !path.startsWith('/libs'),
        });
        // Load the non-module libs
        await vfs.loadFolder({
            diskPath: path.resolve(__dirname, './template/libs/utils'),
            targetPath: 'libs/utils',
        });
        // write dynamic files
        vfs.write('/package.json', Generator.generatePackageJson(ctx.backend.packageJson));
        vfs.write('/tsconfig.json', (0, tsconfig_generator_1.generateTsConfig)(ctx));
        vfs.write('/jest.config.ts', (0, jest_config_generator_1.generateJestConfig)(ctx));
        vfs.write('/apps/api/src/api.module.ts', (0, api_module_generator_1.generateApiModule)(ctx));
        vfs.write('/apps/api/src/main.ts', (0, main_generator_1.generateMain)(ctx));
        vfs.write('/apps/api/src/api.config.ts', (0, api_config_generator_1.generateApiConfig)(ctx));
        const env = (0, dotenv_example_generator_1.generateDotEnvExample)(ctx);
        vfs.write('/apps/api/.env.example', env);
        // We write the .env file so that the project can get started without the need for the developer to
        // copy the .env.example file. The .env file is not committed to the repository as it is part of the
        // .gitignore file.
        vfs.write('/apps/api/.env', env);
        //Insert the modules' VFSs
        for (const module of ctx.backend.modules) {
            if (module.vfs) {
                vfs.insertFromVfs({ vfs: module.vfs });
            }
        }
        ctx.vfs.insertFromVfs({ vfs, targetPath: '/backend' });
        return ctx;
    },
};
//# sourceMappingURL=backend.generator.js.map