"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.generator = exports.generatorId = void 0;
const Generator = __importStar(require("@postxl/generator"));
const generator_1 = require("@postxl/generator");
const backend_actions_1 = require("../backend-actions");
const backend_core_1 = require("../backend-core");
const backend_repositories_1 = require("../backend-repositories");
const backend_seed_1 = require("../backend-seed");
const base_1 = require("../base");
const e2e_controller_generator_1 = require("./generators/e2e-controller.generator");
const e2e_module_generator_1 = require("./generators/e2e-module.generator");
const e2e_service_generator_1 = require("./generators/e2e-service.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('backend-e2e');
exports.generator = {
    id: exports.generatorId,
    requires: [
        backend_core_1.backendGeneratorId,
        base_1.baseGeneratorId,
        backend_actions_1.backendActionsGeneratorId,
        backend_repositories_1.backendRepositoriesGeneratorId,
        backend_seed_1.backendSeedGeneratorId,
    ],
    register: (context) => {
        const module = {
            name: Generator.toClassName('E2eModule'),
            location: Generator.toBackendModuleLocation(`@e2e/e2e.module`),
        };
        const e2eModule = {
            name: Generator.toBackendModuleName('e2e'),
            moduleClass: module,
            apiModuleRegistration: {
                code: (0, generator_1.ts)(`...(config.test.isTest ? [E2eModule.forRoot(config.test)] : [])`),
            },
            envConfig: {
                decoder: (0, generator_1.ts)(`NODE_ENV: z.string().optional(),`),
                transformer: (0, generator_1.ts)(`
              test: val.NODE_ENV === 'test'
              ? {
                isTest: true as const,
                isStateful: val.STATEFUL,
              }
              : {
                isTest: false as const,
              }`),
                type: (0, generator_1.ts)(`
          export type TestConfig = TestConfig_NoTest | TestConfig_Test
                
          type TestConfig_NoTest = {
          isTest: false
          }
          
          export type TestConfig_Test = {
          isTest: true
          isStateful: boolean
          }`),
                dotEnvExample: ``,
            },
            bootStrap: {
                import: (0, generator_1.ts)(``),
                code: (0, generator_1.ts)(``),
            },
        };
        context.backend.modules.push(e2eModule);
        context.backend.packageJson.scripts.push({
            name: 'e2e:stateless',
            command: 'dotenvx run -f ./apps/api/.env --env STATEFUL=false --env AUTH=false NODE_ENV=test -- nest start --watch --debug --entryFile apps/api/src/e2e.js',
        }, {
            name: 'e2e:stateful',
            command: 'dotenvx run -f ./apps/api/.env --env STATEFUL=true --env AUTH=false NODE_ENV=test -- nest start --watch --debug --entryFile apps/api/src/e2e.js',
        }, {
            name: 'e2e:coverage:stateless',
            command: 'dotenvx run -f ./apps/api/.env --env STATEFUL=false --env AUTH=false NODE_ENV=test -- c8 nest start --entryFile apps/api/src/e2e.js',
        }, {
            name: 'e2e:coverage:stateful',
            command: 'dotenvx run -f ./apps/api/.env --env STATEFUL=true --env AUTH=false NODE_ENV=test -- c8 nest start --entryFile apps/api/src/e2e.js',
        });
        const serviceLocation = Generator.toBackendModuleLocation(`@e2e/e2e.service`);
        const service = {
            name: Generator.toClassName('E2eService'),
            location: serviceLocation,
            factory: {
                name: Generator.toFunctionName('provideTestDataService'),
                location: serviceLocation,
            },
        };
        const controller = {
            name: Generator.toClassName('E2eController'),
            location: Generator.toBackendModuleLocation(`@e2e/e2e.controller`),
        };
        const e2eBackend = { module, service, controller };
        return { ...context, e2eBackend };
    },
    generate: (context) => {
        const vfsSrc = new Generator.VirtualFileSystem();
        vfsSrc.write(Generator.toLocalFile(context.e2eBackend.controller), (0, e2e_controller_generator_1.generateBackendE2eController)(context));
        vfsSrc.write(Generator.toLocalFile(context.e2eBackend.service), (0, e2e_service_generator_1.generateBackendE2eService)(context));
        vfsSrc.write(Generator.toLocalFile(context.e2eBackend.module), (0, e2e_module_generator_1.generateBackendE2eModule)(context));
        const vfs = new Generator.VirtualFileSystem();
        vfs.insertFromVfs({ targetPath: 'src', vfs: vfsSrc });
        vfs.write('/tsconfig.lib.json', Generator.generateTsConfig('e2e'));
        context.vfs.insertFromVfs({ targetPath: 'backend/libs/e2e', vfs });
        return Promise.resolve(context);
    },
};
//# sourceMappingURL=backend-e2e.generator.js.map