"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_core_1 = require("../backend-core");
const backend_repositories_1 = require("../backend-repositories");
const base_1 = require("../base");
const types_1 = require("../types");
const model_view_service_generator_1 = require("./model-view-service.generator");
const view_module_generator_1 = require("./view-module.generator");
const view_service_generator_1 = require("./view-service.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('backend-view');
exports.generator = {
    id: exports.generatorId,
    requires: [backend_core_1.backendGeneratorId, base_1.baseGeneratorId, types_1.typesGeneratorId, backend_repositories_1.backendRepositoriesGeneratorId],
    register: (context) => {
        const module = {
            name: Generator.toClassName('ViewModule'),
            location: Generator.toBackendModuleLocation(`@view/view.module`),
        };
        const viewModule = {
            name: Generator.toBackendModuleName('view'),
            moduleClass: module,
        };
        context.backend.modules.push(viewModule);
        const models = new Map();
        for (const [modelName, model] of context.models) {
            models.set(modelName, registerModel(model));
        }
        const view = {
            module,
            service: {
                name: Generator.toClassName('ViewService'),
                location: Generator.toBackendModuleLocation(`@view/view.service`),
            },
        };
        return {
            ...context,
            view,
            models,
        };
    },
    generate: async (context) => {
        const vfsSrc = new Generator.VirtualFileSystem();
        for (const model of context.models.values()) {
            vfsSrc.write(Generator.toLocalFile(model.view.service), (0, model_view_service_generator_1.generateModelViewService)({ model, context }));
        }
        vfsSrc.write(Generator.toLocalFile(context.view.service), (0, view_service_generator_1.generateViewService)(context));
        vfsSrc.write(Generator.toLocalFile(context.view.module), (0, view_module_generator_1.generateViewModule)(context));
        await vfsSrc.loadFolder({
            diskPath: (0, node_path_1.resolve)(__dirname, './template'),
        });
        const vfs = new Generator.VirtualFileSystem();
        vfs.insertFromVfs({ targetPath: 'src', vfs: vfsSrc });
        vfs.write('/tsconfig.lib.json', Generator.generateTsConfig('view'));
        context.vfs.insertFromVfs({ targetPath: 'backend/libs/view', vfs });
        return context;
    },
};
function registerModel(model) {
    const view = {
        service: {
            name: Generator.toClassName(`${model._conjugated.PascalCase}ViewService`),
            location: Generator.toBackendModuleLocation(`@view/${model._conjugated.camelCase}.view.service`),
            variableName: Generator.toVariableName(`${model._conjugated.camelCasePlural}`),
        },
    };
    return { ...model, view };
}
//# sourceMappingURL=view.generator.js.map