"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 base_1 = require("../base");
const frontend_tables_1 = require("../frontend-tables");
const types_1 = require("../types");
const admin_overview_page_generator_1 = require("./generators/admin-overview-page.generator");
const admin_sidebar_generator_1 = require("./generators/admin-sidebar.generator");
const audit_log_sidebar_generator_1 = require("./generators/audit-log-sidebar.generator");
const data_management_page_generator_1 = require("./generators/data-management-page.generator");
const model_admin_page_generator_1 = require("./generators/model-admin-page.generator");
const utils_1 = require("./utils");
const paths = {
    pages: 'frontend/src/pages',
    routes: 'frontend/src/routes/_auth-routes',
    components: 'frontend/src/ui',
};
exports.generatorId = Generator.toGeneratorInterfaceId('frontend-admin');
// TODO Peter: Refactor Generator to so it is simply `ModuleName`
const moduleName = Generator.toBackendModuleName('admin');
exports.generator = {
    id: exports.generatorId,
    requires: [base_1.baseGeneratorId, types_1.typesGeneratorId, frontend_tables_1.frontendTablesGeneratorId],
    register: (context) => {
        const admin = {
            components: {
                sidebar: {
                    name: Generator.toFunctionName('AdminSidebar'),
                    location: Generator.toBackendModuleLocation('@admin/admin-sidebar'),
                },
                auditLogSidebar: {
                    name: Generator.toFunctionName('AuditLogSidebar'),
                    location: Generator.toBackendModuleLocation('@admin/audit-log-sidebar'),
                },
            },
            pages: {
                overview: {
                    pageComponent: {
                        name: Generator.toFunctionName('AdminOverviewPage'),
                        location: Generator.toBackendModuleLocation('@pages/admin/index.page'),
                    },
                    route: '/admin',
                    isOutlet: true,
                },
                dataManagement: {
                    pageComponent: {
                        name: Generator.toFunctionName('DataManagementPage'),
                        location: Generator.toBackendModuleLocation('@pages/admin/data-management.page'),
                    },
                    route: '/admin/data-management',
                },
            },
        };
        const models = new Map();
        for (const [modelName, model] of context.models) {
            models.set(modelName, registerModelAdmin(model));
        }
        context.frontend.tsConfigReferences.set(moduleName, {
            name: moduleName,
            filePath: Generator.toFilePath('ui/admin'),
            hasIndex: false,
            isAbsolute: true,
        });
        return { ...context, admin, models };
    },
    generate: async (context) => {
        const { vfs } = context;
        // Generate individual model admin pages
        for (const model of context.models.values()) {
            (0, utils_1.generatePage)({ page: model.admin.page, paths, vfs, content: (0, model_admin_page_generator_1.generateModelAdminPage)({ model, context }) });
        }
        (0, utils_1.generatePage)({ page: context.admin.pages.overview, paths, vfs, content: (0, admin_overview_page_generator_1.generateAdminOverviewPage)({ context }) });
        (0, utils_1.generatePage)({
            page: context.admin.pages.dataManagement,
            paths,
            vfs,
            content: (0, data_management_page_generator_1.generateDataManagementPage)({ context }),
        });
        context.vfs.write((0, node_path_1.join)(paths.components, 'admin', Generator.toLocalReactComponent(context.admin.components.sidebar)), (0, admin_sidebar_generator_1.generateAdminSidebar)({ context }));
        context.vfs.write((0, node_path_1.join)(paths.components, 'admin', Generator.toLocalReactComponent(context.admin.components.auditLogSidebar)), (0, audit_log_sidebar_generator_1.generateAuditLogSidebar)({ context }));
        return Promise.resolve(context); // NOSONAR
    },
};
function registerModelAdmin(model) {
    const admin = {
        page: {
            pageComponent: {
                name: Generator.toFunctionName(`${model._conjugated.PascalCase}AdminPage`),
                location: Generator.toBackendModuleLocation(`@pages/admin/${model._conjugated.camelCase}.page`),
            },
            route: `/admin/${model._conjugated.camelCase}`,
            aliases: [`/admin/${model._conjugated.camelCasePlural}`],
        },
    };
    return { ...model, admin };
}
//# sourceMappingURL=admin.generator.js.map