"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessHistoryEntitySubscriber = exports.ProcessSubscriber = void 0;
const tslib_1 = require("tslib");
const typeorm_1 = require("typeorm");
const typeorm_history_1 = require("@operato/typeorm-history");
const process_1 = require("./process");
const process_history_1 = require("./process-history");
const analysis_1 = require("../analysis/analysis");
const sted_graph_generator_1 = require("../../controllers/sted-graph/sted-graph-generator");
const transformer_1 = require("../../controllers/transformer");
async function saveAnalysis(process, tx) {
    var analysis = {
        process: { id: process.id }
    };
    const repository = tx.getRepository(analysis_1.Analysis);
    analysis = (await repository.findOneBy(analysis)) || analysis;
    analysis.model = (0, transformer_1.transformGraphicModelToStedModel)(JSON.parse(process.model));
    const { stedGraph, availResult } = new sted_graph_generator_1.StedGraphGenerator(analysis.model).run();
    analysis.dag = stedGraph;
    analysis.availiability = availResult.result;
    analysis.availDetail = availResult.detail;
    try {
        repository.save(analysis);
    }
    catch (err) {
        console.error(err);
    }
}
let ProcessSubscriber = class ProcessSubscriber {
    listenTo() {
        return process_1.Process;
    }
    async afterInsert(event) {
        const process = event.entity;
        await saveAnalysis(process, event.manager);
        //   pubsub.publish('process', {
        //     process
        //   })
    }
    async afterUpdate(event) {
        var process = event.entity;
        await saveAnalysis(process, event.manager);
        // process = await getRepository(Process).findOne({
        //   where: { domain: { id: process.domainId }, id: process.id },
        //   relations: ['group']
        // })
        // if (process.group) {
        //   pubsub.publish('process-group', {
        //     processGroup: process.group
        //   })
        // }
        // pubsub.publish('process', {
        //   process
        // })
    }
};
exports.ProcessSubscriber = ProcessSubscriber;
exports.ProcessSubscriber = ProcessSubscriber = tslib_1.__decorate([
    (0, typeorm_1.EventSubscriber)()
], ProcessSubscriber);
let ProcessHistoryEntitySubscriber = class ProcessHistoryEntitySubscriber extends typeorm_history_1.HistoryEntitySubscriber {
    get entity() {
        return process_1.Process;
    }
    get historyEntity() {
        return process_history_1.ProcessHistory;
    }
    async afterInsert(event) {
        if (event.entity.state == 'released') {
            super.afterInsert(event);
        }
    }
    async afterUpdate(event) {
        if (event.entity.state == 'released') {
            super.afterUpdate(event);
        }
    }
};
exports.ProcessHistoryEntitySubscriber = ProcessHistoryEntitySubscriber;
exports.ProcessHistoryEntitySubscriber = ProcessHistoryEntitySubscriber = tslib_1.__decorate([
    (0, typeorm_1.EventSubscriber)()
], ProcessHistoryEntitySubscriber);
//# sourceMappingURL=event-subscriber.js.map