"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 base_1 = require("../base");
const frontend_core_1 = require("../frontend-core");
const frontend_forms_1 = require("../frontend-forms");
const types_1 = require("../types");
const model_context_generator_1 = require("./generators/model-context.generator");
const model_table_generator_1 = require("./generators/model-table.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('frontend-tables');
// TODO Peter: Refactor Generator to so it is simply `ModuleName`
const moduleName = Generator.toBackendModuleName('tables');
exports.generator = {
    id: exports.generatorId,
    requires: [base_1.baseGeneratorId, types_1.typesGeneratorId, frontend_core_1.frontendGeneratorId, frontend_forms_1.frontendFormsGeneratorId],
    register: (context) => {
        context.frontend.tsConfigReferences.set(moduleName, {
            name: moduleName,
            filePath: Generator.toFilePath('ui/tables'),
            hasIndex: false,
            isAbsolute: true,
        });
        const tables = {};
        const models = new Map();
        for (const [modelName, model] of context.models) {
            models.set(modelName, registerModelTables(model));
        }
        return {
            ...context,
            tables,
            models,
        };
    },
    generate: async (context) => {
        const tablesVfs = new Generator.VirtualFileSystem();
        const contextVfs = new Generator.VirtualFileSystem();
        for (const model of context.models.values()) {
            tablesVfs.write(Generator.toLocalReactComponent(model.table.componentWithoutFilters), (0, model_table_generator_1.generateModelTableComponent)({ model, context }));
            contextVfs.write(Generator.toLocalReactComponent(model.table.allModelsHook), (0, model_context_generator_1.generateModelContext)({ model, context }));
        }
        context.vfs.insertFromVfs({ targetPath: 'frontend/src/ui/tables', vfs: tablesVfs });
        context.vfs.insertFromVfs({ targetPath: 'frontend/src/context-providers', vfs: contextVfs });
        return Promise.resolve(context); // NOSONAR
    },
};
function registerModelTables(model) {
    const location = Generator.toBackendModuleLocation(`@tables/${model._conjugated.camelCase}-table`);
    const contextLocation = Generator.toBackendModuleLocation(`@context-providers/${model._conjugated.PascalCase}-context-provider`);
    const table = {
        componentWithoutFilters: {
            name: Generator.toFunctionName(`${model._conjugated.PascalCase}TableRaw`),
            location,
        },
        component: {
            name: Generator.toFunctionName(`${model._conjugated.PascalCase}Table`),
            location,
        },
        allModelsHook: {
            name: Generator.toConstantName(`use${model._conjugated.PascalCasePlural}Context`),
            location: contextLocation,
        },
        modelHook: { name: Generator.toConstantName(`use${model._conjugated.PascalCase}`), location: contextLocation },
        propName: `${model.internalSingularName}Map`,
    };
    return {
        ...model,
        table,
    };
}
//# sourceMappingURL=tables.generator.js.map