"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var SystemFieldsSeederService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemFieldsSeederService = void 0;
const common_1 = require("@nestjs/common");
const model_metadata_helper_service_1 = require("../helpers/model-metadata-helper.service");
const field_metadata_repository_1 = require("../repository/field-metadata.repository");
const model_metadata_repository_1 = require("../repository/model-metadata.repository");
let SystemFieldsSeederService = SystemFieldsSeederService_1 = class SystemFieldsSeederService {
    constructor(modelHelperService, modelRepository, fieldMetadataRepository) {
        this.modelHelperService = modelHelperService;
        this.modelRepository = modelRepository;
        this.fieldMetadataRepository = fieldMetadataRepository;
        this.logger = new common_1.Logger(SystemFieldsSeederService_1.name);
    }
    async seed() {
        const models = await this.modelRepository.find({
            relations: ['fields'],
        });
        for (const model of models) {
            await this.seedMissingSystemFields(model);
        }
    }
    async seedMissingSystemFields(model) {
        this.logger.debug(`Checking system fields for model: ${model.singularName}`);
        const existingSystemFields = model.fields.filter(field => field.isSystem);
        const systemFieldsMetadata = this.modelHelperService.getSystemFieldsMetadata(model.isLegacyTable, model.isLegacyTableWithId);
        this.logger.debug(`Model: ${model.singularName} has ${existingSystemFields.length} existing system fields.`);
        const missingFields = systemFieldsMetadata.filter(sysField => !existingSystemFields.some(field => field.name === sysField.name));
        if (missingFields.length > 0) {
            this.logger.debug(`Seeding ${missingFields.length} missing system fields for model: ${model.singularName}`);
            const newFields = missingFields.map(field => ({
                ...field,
                model: model,
            }));
            await this.fieldMetadataRepository.save(newFields);
        }
        else {
            this.logger.debug(`No missing system fields for model: ${model.singularName}`);
        }
    }
};
exports.SystemFieldsSeederService = SystemFieldsSeederService;
exports.SystemFieldsSeederService = SystemFieldsSeederService = SystemFieldsSeederService_1 = __decorate([
    (0, common_1.Injectable)(),
    __param(1, (0, common_1.Inject)((0, common_1.forwardRef)(() => model_metadata_repository_1.ModelMetadataRepository))),
    __metadata("design:paramtypes", [model_metadata_helper_service_1.ModelMetadataHelperService,
        model_metadata_repository_1.ModelMetadataRepository,
        field_metadata_repository_1.FieldMetadataRepository])
], SystemFieldsSeederService);
//# sourceMappingURL=system-fields-seeder.service.js.map