"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 __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 __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;
    };
})();
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 ViewMetadataSubsciber_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewMetadataSubsciber = void 0;
const typeorm_1 = require("@nestjs/typeorm");
const view_metadata_entity_1 = require("../entities/view-metadata.entity");
const module_metadata_helper_service_1 = require("../helpers/module-metadata-helper.service");
const typeorm_2 = require("typeorm");
const common_1 = require("@nestjs/common");
const fs = __importStar(require("fs/promises"));
let ViewMetadataSubsciber = ViewMetadataSubsciber_1 = class ViewMetadataSubsciber {
    constructor(dataSource, moduleMetadataHelperService) {
        this.dataSource = dataSource;
        this.moduleMetadataHelperService = moduleMetadataHelperService;
        this.logger = new common_1.Logger(ViewMetadataSubsciber_1.name);
        this.dataSource.subscribers.push(this);
    }
    listenTo() {
        return view_metadata_entity_1.ViewMetadata;
    }
    async afterUpdate(event) {
        const viewMetadataRepo = event.manager.getRepository(view_metadata_entity_1.ViewMetadata);
        const viewMetadata = await viewMetadataRepo.findOne({
            where: {
                id: event.entity.id
            },
            relations: {
                model: {
                    module: true
                }
            }
        });
        if (!viewMetadata) {
            throw new Error(`View metadata not found for id ${event.entity.id}`);
        }
        if (viewMetadata.model.module.name != "solid-core") {
            const filePath = await this.moduleMetadataHelperService.getModuleMetadataFilePath(viewMetadata.model.module.name);
            try {
                await fs.access(filePath);
            }
            catch (error) {
                this.logger.error(`File not found at path: ${filePath}`);
                return;
            }
            const metaData = await this.moduleMetadataHelperService.getModuleMetadataConfiguration(filePath);
            const viewMetadataIndex = metaData.views.findIndex((view) => view.name === event.entity.name);
            if (viewMetadataIndex !== -1) {
                metaData.views[viewMetadataIndex].layout = JSON.parse(event.entity.layout);
            }
            const updatedContent = JSON.stringify(metaData, null, 2);
            await fs.writeFile(filePath, updatedContent);
        }
    }
};
exports.ViewMetadataSubsciber = ViewMetadataSubsciber;
exports.ViewMetadataSubsciber = ViewMetadataSubsciber = ViewMetadataSubsciber_1 = __decorate([
    __param(0, (0, typeorm_1.InjectDataSource)()),
    __metadata("design:paramtypes", [typeorm_2.DataSource,
        module_metadata_helper_service_1.ModuleMetadataHelperService])
], ViewMetadataSubsciber);
//# sourceMappingURL=view-metadata.subscriber.js.map