"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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListOfValuesSubscriber = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const list_of_values_entity_1 = require("../entities/list-of-values.entity");
const module_metadata_helper_service_1 = require("../helpers/module-metadata-helper.service");
const list_of_values_service_1 = require("../services/list-of-values.service");
const typeorm_2 = require("typeorm");
let ListOfValuesSubscriber = class ListOfValuesSubscriber {
    constructor(dataSource, moduleMetadataHelperService, listOfValuesService) {
        this.dataSource = dataSource;
        this.moduleMetadataHelperService = moduleMetadataHelperService;
        this.listOfValuesService = listOfValuesService;
        this.logger = new common_1.Logger(this.constructor.name);
        this.dataSource.subscribers.push(this);
    }
    listenTo() {
        return list_of_values_entity_1.ListOfValues;
    }
    async afterInsert(event) {
        if (!event.entity) {
            this.logger.debug('No listofvalue entity found in the ListofValueSubscriber afterInsert method');
            return;
        }
        await this.saveListOfValuesToConfig(event.entity, event.queryRunner.manager);
    }
    async afterUpdate(event) {
        if (!event.entity) {
            this.logger.debug('No listofvalue entity found in the ListofValueSubscriber afterInsert method');
            return;
        }
        await this.updateListOfValuesToConfig(event.databaseEntity, event.entity, event.queryRunner.manager);
    }
    async saveListOfValuesToConfig(listOfValues, entityManager) {
        if (!listOfValues || !listOfValues.id) {
            this.logger.debug('on save Listofvalue id is undefined');
            return;
        }
        const populatedLov = await entityManager.findOne(list_of_values_entity_1.ListOfValues, {
            where: { id: listOfValues.id },
            relations: ['module'],
        });
        if (!populatedLov) {
            this.logger.error(`Listofvalue not found for id ${listOfValues.id}`);
            return;
        }
        await this.listOfValuesService.saveListofValuesToConfig(populatedLov);
    }
    async updateListOfValuesToConfig(oldlistOfValues, listOfValues, entityManager) {
        if (!listOfValues || !listOfValues.id) {
            this.logger.debug('on update Listofvalue id is undefined');
            return;
        }
        const populatedLov = await entityManager.findOne(list_of_values_entity_1.ListOfValues, {
            where: { id: listOfValues.id },
            relations: ['module'],
        });
        if (!populatedLov) {
            this.logger.error(`Listofvalue not found for id ${listOfValues.id}`);
            return;
        }
        await this.listOfValuesService.updateListofValuesToConfig(oldlistOfValues, populatedLov);
    }
    async deleteListOfValuesFromConfig(listOfValues, entityManager) {
        if (!listOfValues || !listOfValues.id) {
            this.logger.debug('on delete Listofvalue id is undefined');
            return;
        }
        const populatedLov = await entityManager.findOne(list_of_values_entity_1.ListOfValues, {
            where: { id: listOfValues.id },
            relations: ['module'],
        });
        if (!populatedLov) {
            this.logger.error(`Listofvalue not found for id ${listOfValues.id}`);
            return;
        }
        await this.listOfValuesService.deleteListOfValuesFromConfig(populatedLov);
    }
};
exports.ListOfValuesSubscriber = ListOfValuesSubscriber;
exports.ListOfValuesSubscriber = ListOfValuesSubscriber = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectDataSource)()),
    __metadata("design:paramtypes", [typeorm_2.DataSource,
        module_metadata_helper_service_1.ModuleMetadataHelperService,
        list_of_values_service_1.ListOfValuesService])
], ListOfValuesSubscriber);
//# sourceMappingURL=list-of-values.subscriber.js.map