"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 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 decoders_1 = require("../decoders");
const data_management_service_generator_1 = require("./generators/data-management-service.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('backend-data-management');
exports.generator = {
    id: exports.generatorId,
    requires: [backend_core_1.backendGeneratorId, backend_actions_1.backendActionsGeneratorId, backend_repositories_1.backendRepositoriesGeneratorId, decoders_1.decodersGeneratorId],
    register: (context) => {
        const module = {
            name: Generator.toClassName('DataManagementModule'),
            location: Generator.toBackendModuleLocation('@data-management/data-management.module'),
        };
        const dataManagementModule = {
            name: Generator.toBackendModuleName('data-management'),
            moduleClass: module,
            bootStrap: {
                import: (0, generator_1.ts)("import multipart from '@fastify/multipart'"),
                code: (0, generator_1.ts)(`await app.register(multipart, { limits: { fileSize: 100 * 1024 * 1024 }})`),
            },
            apiModuleRegistration: {
                code: (0, generator_1.ts)('...(config.admin.allowBackend ? [DataManagementModule] : [])'),
            },
        };
        context.backend.modules.push(dataManagementModule);
        context.backend.packageJson.dependencies.push({ packageName: '@fastify/multipart', version: '9.2.1' });
        const serviceLocation = Generator.toBackendModuleLocation('@data-management/data-management.service');
        const service = {
            name: Generator.toClassName('DataManagementService'),
            location: serviceLocation,
        };
        const controller = {
            name: Generator.toClassName('DataManagementController'),
            location: Generator.toBackendModuleLocation('@data-management/data-management.controller'),
        };
        const dataManagement = { module, service, controller };
        return { ...context, dataManagement };
    },
    generate: async (context) => {
        const vfsSrc = new Generator.VirtualFileSystem();
        // Load static template files
        await vfsSrc.loadFile({
            diskPath: path.resolve(__dirname, './template/data-management.controller.ts'),
            targetPath: Generator.toLocalFile(context.dataManagement.controller),
        });
        await vfsSrc.loadFile({
            diskPath: path.resolve(__dirname, './template/data-management.module.ts'),
            targetPath: Generator.toLocalFile(context.dataManagement.module),
        });
        // Generate dynamic service file
        vfsSrc.write(Generator.toLocalFile(context.dataManagement.service), (0, data_management_service_generator_1.generateDataManagementService)(context));
        // Add index.ts for the module
        vfsSrc.write('index.ts', `export { DataManagementModule } from './data-management.module'
export { DataManagementService } from './data-management.service'
export { DataManagementController } from './data-management.controller'
`);
        const vfs = new Generator.VirtualFileSystem();
        vfs.insertFromVfs({ targetPath: 'src', vfs: vfsSrc });
        vfs.write('/tsconfig.lib.json', Generator.generateTsConfig('data-management'));
        context.vfs.insertFromVfs({ targetPath: 'backend/libs/data-management', vfs });
        return context;
    },
};
//# sourceMappingURL=data-management.generator.js.map