"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 path = __importStar(require("node:path"));
const Generator = __importStar(require("@postxl/generator"));
const model_test_id_generator_1 = require("./generators/model-test-id.generator");
const DOCKER_SH_FILENAME = 'docker.sh';
const PACKAGE_JSON_FILENAME = 'package.json';
exports.generatorId = Generator.toGeneratorInterfaceId('e2e');
exports.generator = {
    id: exports.generatorId,
    requires: [exports.generatorId],
    register: (ctx) => {
        ctx.backend.packageJson.devDependencies.push({ packageName: 'c8', version: '^10.1.3' });
        return {
            ...ctx,
            e2e: {
                testIds: [],
            },
        };
    },
    generate: async (context) => {
        const vfs = new Generator.VirtualFileSystem();
        const templateContext = { schema: context.schema };
        // Load e2e folder, excluding files that need template substitution
        await vfs.loadFolder({
            diskPath: path.resolve(__dirname, './template/e2e'),
            targetPath: '/e2e',
            filter: (filePath) => !filePath.endsWith(PACKAGE_JSON_FILENAME),
        });
        await vfs.loadFolder({
            diskPath: path.resolve(__dirname, './template/scripts'),
            targetPath: '/scripts',
            filter: (filePath) => !filePath.endsWith(DOCKER_SH_FILENAME),
        });
        // Load files with template substitution
        const dockerShContent = await Generator.generateFromTemplate({
            file: path.resolve(__dirname, './template/scripts', DOCKER_SH_FILENAME),
            context: templateContext,
        });
        if (dockerShContent.isErr()) {
            throw new Error(`Failed to generate docker.sh: ${dockerShContent.unwrapErr().message}`);
        }
        vfs.write(`/scripts/${DOCKER_SH_FILENAME}`, dockerShContent.unwrap());
        const packageJsonContent = await Generator.generateFromTemplate({
            file: path.resolve(__dirname, './template/e2e', PACKAGE_JSON_FILENAME),
            context: templateContext,
        });
        if (packageJsonContent.isErr()) {
            throw new Error(`Failed to generate package.json: ${packageJsonContent.unwrapErr().message}`);
        }
        vfs.write(`/e2e/${PACKAGE_JSON_FILENAME}`, packageJsonContent.unwrap());
        // write dynamic files
        vfs.write('/e2e/support/model-test-ids.ts', (0, model_test_id_generator_1.generateModelTestIds)(context.e2e));
        context.vfs.insertFromVfs({ vfs, targetPath: '/' });
        return context;
    },
};
//# sourceMappingURL=e2e.generator.js.map