"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 utils_1 = require("@postxl/utils");
const backend_core_1 = require("../backend-core");
const base_1 = require("../base");
const types_1 = require("../types");
const data_mocker_generator_1 = require("./generators/data-mocker.generator");
const id_tracker_generator_1 = require("./generators/id-tracker.generator");
const model_stub_generator_1 = require("./generators/model-stub.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('mock-data');
const moduleName = Generator.toBackendModuleName('mock-data');
exports.generator = {
    id: exports.generatorId,
    requires: [backend_core_1.backendGeneratorId, base_1.baseGeneratorId, types_1.typesGeneratorId],
    register: (context) => {
        // Register with backend module
        const typesModule = { name: moduleName, hasIndex: true };
        context.backend.modules.push(typesModule);
        const models = new Map();
        for (const [modelName, model] of context.models) {
            const modelWithTypes = registerModelStub(model);
            models.set(modelName, modelWithTypes);
        }
        const mockData = {
            dataMocker: {
                name: Generator.toClassName('DataMocker'),
                location: Generator.toBackendModuleLocation(`@mock-data/dataMocker.class`),
            },
            idTracker: {
                name: Generator.toClassName('IdTracker'),
                location: Generator.toBackendModuleLocation(`@mock-data/idTracker.class`),
            },
        };
        return {
            ...context,
            models,
            mockData,
        };
    },
    generate: async (context) => {
        const vfsSrc = new Generator.VirtualFileSystem();
        for (const model of context.models.values()) {
            vfsSrc.write(Generator.toLocalFile(model.mockData.stub), (0, model_stub_generator_1.generateModelStub)({ model, context }));
            for (const discriminatedUnion of model.mockData.discriminatedUnions.values()) {
                const location = discriminatedUnion.values().next().value.location;
                vfsSrc.write(Generator.toLocalFile({ location }), (0, model_stub_generator_1.generateDiscriminatedUnionStub)({ context, model, discriminatedUnion }));
            }
        }
        vfsSrc.write(Generator.toLocalFile(context.mockData.dataMocker), (0, data_mocker_generator_1.generateDataMocker)(context));
        vfsSrc.write(Generator.toLocalFile(context.mockData.idTracker), (0, id_tracker_generator_1.generateIdTracker)(context));
        const vfs = new Generator.VirtualFileSystem();
        vfs.insertFromVfs({ targetPath: '/src', vfs: vfsSrc });
        vfs.write('/tsconfig.lib.json', Generator.generateTsConfig('mock-data'));
        context.vfs.insertFromVfs({ targetPath: 'backend/libs/mock-data', vfs });
        return Promise.resolve(context); // NOSONAR
    },
};
function registerModelStub(model) {
    const discriminatedUnions = new Map();
    for (const field of model.fields.values()) {
        if (field.kind !== 'discriminatedUnion') {
            continue;
        }
        const stubs = new Map();
        const location = Generator.toBackendModuleLocation(`@mock-data/stubs/${(0, utils_1.toCamelCase)(field.name)}.discriminated-union.stub`);
        for (const discriminator of field.members.keys()) {
            const name = Generator.toFunctionName(`${(0, utils_1.toPascalCase)(field.name)}_${(0, utils_1.toPascalCase)(discriminator)}`);
            stubs.set(discriminator, { name, location });
        }
        discriminatedUnions.set(field.name, stubs);
    }
    return {
        ...model,
        mockData: {
            stub: {
                name: Generator.toConstantName(`stub${model._conjugated.PascalCase}`),
                location: Generator.toBackendModuleLocation(`@mock-data/stubs/${model._conjugated.camelCase}.stub`),
            },
            discriminatedUnions,
            dataMocker: {
                add: Generator.toFunctionName(`add${model._conjugated.PascalCase}`),
            },
        },
    };
}
//# sourceMappingURL=mock-data.generator.js.map