"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 node_path_1 = require("node:path");
const Generator = __importStar(require("@postxl/generator"));
const backend_actions_1 = require("../backend-actions");
const backend_core_1 = require("../backend-core");
const backend_import_1 = require("../backend-import");
const backend_repositories_1 = require("../backend-repositories");
const base_1 = require("../base");
const seed_module_generator_1 = require("./generators/seed-module.generator");
const seed_service_generator_1 = require("./generators/seed-service.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('backend-seed');
exports.generator = {
    id: exports.generatorId,
    requires: [
        backend_core_1.backendGeneratorId,
        base_1.baseGeneratorId,
        backend_actions_1.backendActionsGeneratorId,
        backend_core_1.backendModuleXlPortGeneratorId,
        backend_repositories_1.backendRepositoriesGeneratorId,
        backend_import_1.backendImportGeneratorId,
    ],
    register: (context) => {
        const module = {
            name: Generator.toClassName('SeedModule'),
            location: Generator.toBackendModuleLocation(`@seed/seed.module`),
        };
        const seedModule = {
            name: Generator.toBackendModuleName('seed'),
            moduleClass: module,
            bootStrap: {
                import: Generator.ts(`
          import { SeedService } from '@seed/seed.service'`),
                code: Generator.ts(`
          // Executing seeding and initialize data
            const seedService = app.get(SeedService)
            await seedService.seed()
          `),
            },
        };
        context.backend.modules.push(seedModule);
        const service = {
            name: Generator.toClassName('SeedService'),
            location: Generator.toBackendModuleLocation(`@seed/seed.service`),
        };
        const seed = { module, service };
        return { ...context, seed };
    },
    generate: async (context) => {
        const vfsSrc = new Generator.VirtualFileSystem();
        vfsSrc.write(Generator.toLocalFile(context.seed.service), (0, seed_service_generator_1.generateSeedService)(context));
        vfsSrc.write(Generator.toLocalFile(context.seed.module), (0, seed_module_generator_1.generateSeedModule)(context));
        const vfs = new Generator.VirtualFileSystem();
        vfs.insertFromVfs({ targetPath: 'src', vfs: vfsSrc });
        await vfs.loadFolder({ diskPath: (0, node_path_1.resolve)(__dirname, './template') });
        vfs.write('/tsconfig.lib.json', Generator.generateTsConfig('seed'));
        context.vfs.insertFromVfs({ targetPath: 'backend/libs/seed', vfs });
        return context;
    },
};
//# sourceMappingURL=seed.generator.js.map