"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 ModelMetadataHelperService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelMetadataHelperService = void 0;
const common_1 = require("@nestjs/common");
const lodash_1 = require("lodash");
const legacy_common_entity_1 = require("../entities/legacy-common.entity");
const model_metadata_repository_1 = require("../repository/model-metadata.repository");
const solid_registry_1 = require("./solid-registry");
let ModelMetadataHelperService = ModelMetadataHelperService_1 = class ModelMetadataHelperService {
    constructor(registry, modelMetadataRepo) {
        this.registry = registry;
        this.modelMetadataRepo = modelMetadataRepo;
        this.logger = new common_1.Logger(ModelMetadataHelperService_1.name);
    }
    getSystemFieldsMetadata(isLegacyTable = false, isLegacyTableWithId = false) {
        let systemFieldsMetadata;
        if (isLegacyTableWithId) {
            systemFieldsMetadata = this.getSystemFieldsMetadataMappingForLegacyTable(true);
        }
        else if (isLegacyTable) {
            systemFieldsMetadata = this.getSystemFieldsMetadataMappingForLegacyTable(false);
        }
        else {
            systemFieldsMetadata = this.getSystemFieldsMetadataMapping();
        }
        this.checkWithRegistry(systemFieldsMetadata);
        return systemFieldsMetadata;
    }
    checkWithRegistry(systemFieldsMetadata) {
        const commonEntityKeys = this.registry.getCommonEntityKeys();
        const systemFieldNames = systemFieldsMetadata.map(field => field.name);
        const missingKeys = commonEntityKeys.filter(key => !systemFieldNames.includes(key));
        if (missingKeys.length > 0) {
            this.logger.warn(`Missing system fields metadata for common entity keys: ${missingKeys.join(', ')}`);
        }
    }
    getSystemFieldsMetadataMapping() {
        return [
            {
                name: "id",
                displayName: "Id",
                type: "int",
                isSystem: true,
            },
            {
                name: "createdAt",
                displayName: "Created At",
                type: "datetime",
                isSystem: true,
            },
            {
                name: "updatedAt",
                displayName: "Updated At",
                type: "datetime",
                isSystem: true,
            },
            {
                name: "deletedAt",
                displayName: "Deleted At",
                type: "datetime",
                isSystem: true,
            },
            {
                name: "deletedTracker",
                displayName: "Deleted Tracker",
                type: "shortText",
                isSystem: true,
            },
            {
                name: "publishedAt",
                displayName: "Published At",
                type: "datetime",
                isSystem: true,
                enableAuditTracking: true
            },
            {
                name: "localeName",
                displayName: "Locale",
                type: "shortText",
                isSystem: true,
            },
            {
                name: "defaultEntityLocaleId",
                displayName: "Default Entity Locale Id",
                type: "int",
                isSystem: true,
            },
            {
                name: "createdBy",
                displayName: "Created By",
                type: "int",
                isSystem: true,
                columnName: "created_by_id",
            },
            {
                name: "updatedBy",
                displayName: "Updated By",
                type: "int",
                isSystem: true,
                columnName: "updated_by_id",
            }
        ];
    }
    getSystemFieldsMetadataMappingForLegacyTable(withId = true) {
        const systemFieldsMetadata = this.getSystemFieldsMetadataMapping();
        if (!withId) {
            const index = systemFieldsMetadata.findIndex(field => field.name === 'id');
            if (index !== -1) {
                systemFieldsMetadata.splice(index, 1);
            }
        }
        systemFieldsMetadata.forEach(field => {
            if (field.name === 'createdBy' || field.name === 'updatedBy') {
                field['columnName'] = `${legacy_common_entity_1.LEGACY_TABLE_FIELDS_PREFIX}_${lodash_1._.snakeCase(field.name)}_id`;
            }
            else {
                field['columnName'] = `${legacy_common_entity_1.LEGACY_TABLE_FIELDS_PREFIX}_${lodash_1._.snakeCase(field.name)}`;
            }
        });
        return systemFieldsMetadata;
    }
    async loadFieldHierarchy(modelName) {
        const model = await this.modelMetadataRepo.findOne({
            where: {
                singularName: modelName,
            },
            relations: {
                fields: true,
                parentModel: {
                    fields: true,
                }
            }
        });
        const fields = [];
        if (model) {
            fields.push(...model.fields);
            if (model.parentModel) {
                fields.push(...model.parentModel.fields);
            }
        }
        return fields;
    }
};
exports.ModelMetadataHelperService = ModelMetadataHelperService;
exports.ModelMetadataHelperService = ModelMetadataHelperService = ModelMetadataHelperService_1 = __decorate([
    (0, common_1.Injectable)(),
    __param(1, (0, common_1.Inject)((0, common_1.forwardRef)(() => model_metadata_repository_1.ModelMetadataRepository))),
    __metadata("design:paramtypes", [solid_registry_1.SolidRegistry,
        model_metadata_repository_1.ModelMetadataRepository])
], ModelMetadataHelperService);
//# sourceMappingURL=model-metadata-helper.service.js.map