"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 backend_actions_1 = require("../backend-actions");
const backend_core_1 = require("../backend-core");
const base_1 = require("../base");
const types_1 = require("../types");
const excel_template_generator_1 = require("./generator/excel-template.generator");
const model_seed_data_generator_1 = require("./generator/model-seed-data.generator");
const seed_migration_types_generator_1 = require("./generator/seed-migration-types.generator");
const seed_migrations_generator_1 = require("./generator/seed-migrations.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('seed-data');
const moduleName = Generator.toBackendModuleName('seedData');
exports.generator = {
    id: exports.generatorId,
    requires: [backend_core_1.backendGeneratorId, base_1.baseGeneratorId, types_1.typesGeneratorId, backend_actions_1.backendActionsGeneratorId],
    register: (context) => {
        const seedDataModule = { name: moduleName };
        context.backend.modules.push(seedDataModule);
        const typesLocation = Generator.toBackendModuleLocation(`@seedData/seed-migration.types`);
        const seedData = {
            excelTemplate: {
                projectRootPath: Generator.toFilePath(`./backend/seed-data.xlsx`),
            },
            migrationTypes: {
                summary: {
                    name: Generator.toTypeName(`SeedMigration`),
                    location: typesLocation,
                },
                base: {
                    name: Generator.toTypeName(`SeedMigration_Base`),
                    location: typesLocation,
                },
                excel: {
                    name: Generator.toTypeName(`SeedMigration_Excel`),
                    location: typesLocation,
                    discriminant: Generator.toDiscriminantValue(`excel`),
                },
                excelSeed: {
                    name: Generator.toTypeName(`SeedMigration_ExcelSeed`),
                    location: typesLocation,
                    discriminant: Generator.toDiscriminantValue(`excelSeed`),
                },
                customAction: {
                    name: Generator.toTypeName(`SeedMigration_CustomAction`),
                    location: typesLocation,
                    discriminant: Generator.toDiscriminantValue(`customAction`),
                },
            },
            seedMigrations: {
                name: Generator.toConstantName(`migrations`),
                location: Generator.toBackendModuleLocation(`@${moduleName}/seed-migrations`),
            },
        };
        const modelData = (0, model_seed_data_generator_1.generateSeedDataForModels)(context);
        const models = new Map();
        for (const [modelName, model] of context.models) {
            const modelWithSeedData = registerModelSeedData({ context, modelData, model });
            models.set(modelName, modelWithSeedData);
        }
        return { ...context, seedData, models };
    },
    generate: async (context) => {
        const vfsSrc = new Generator.VirtualFileSystem();
        for (const model of context.models.values()) {
            if (model.seedData.data.length === 0) {
                continue;
            }
            vfsSrc.write(Generator.toLocalFile({ location: model.seedData.dataVariable.location, extension: 'json' }), (0, model_seed_data_generator_1.generateModelSeedDataJson)({ model, context }));
            vfsSrc.write(Generator.toLocalFile(model.seedData.dataVariable), (0, model_seed_data_generator_1.generateModelSeedDataTypescript)({ model, context }));
        }
        vfsSrc.write(Generator.toLocalFile(context.seedData.migrationTypes.summary), (0, seed_migration_types_generator_1.generateSeedMigrationTypes)(context));
        vfsSrc.write(Generator.toLocalFile(context.seedData.seedMigrations), (0, seed_migrations_generator_1.generateSeedMigrations)(context));
        const vfs = new Generator.VirtualFileSystem();
        vfs.insertFromVfs({ targetPath: 'src', vfs: vfsSrc });
        vfs.write('/tsconfig.lib.json', Generator.generateTsConfig('seedData'));
        context.vfs.write(context.seedData.excelTemplate.projectRootPath, await (0, excel_template_generator_1.generateSeedExcelTemplate)({ context }));
        context.vfs.insertFromVfs({ targetPath: 'backend/libs/seedData', vfs });
        return context;
    },
};
function registerModelSeedData({ modelData, model, }) {
    const data = modelData.get(model.name);
    const seedData = {
        dataVariable: {
            name: Generator.toConstantName(`${model._conjugated.camelCasePlural}`),
            location: Generator.toBackendModuleLocation(`@seedData/json/${model._conjugated.camelCase}.seed-data`),
        },
        data,
        excel: {
            tableName: model.excelName,
            sheetName: model.name.substring(0, 30),
        },
    };
    return { ...model, seedData };
}
//# sourceMappingURL=seed-data.generator.js.map