"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlertService = void 0;
const tslib_1 = require("tslib");
const IAlertService_1 = require("./Interface/IAlertService");
const Emitter_1 = tslib_1.__importDefault(require("../Emitter"));
const rxjs_1 = require("rxjs");
const ModuleConstants_1 = require("../Constants/ModuleConstants");
class AlertService {
    constructor(adaptableApi) {
        this.adaptableApi = adaptableApi;
        this.reactiveAlertsMap = new Map();
        this.onReactiveAlertTriggered = (callback) => {
            return this.emitter.on('ReactiveAlertTriggered', callback);
        };
        this.emitter = new Emitter_1.default();
    }
    destroy() {
        this.emitter.destroy();
        this.emitter = null;
        this.reactiveAlertsMap.forEach((alertsSubscriptionInfo) => alertsSubscriptionInfo.subscription.unsubscribe());
        this.reactiveAlertsMap.clear();
        this.reactiveAlertsMap = null;
    }
    createReactiveAlert(alertDefinition) {
        // if there is already a reactive alert for this definition, delete it (possible in case of editing definitions),
        this.deleteReactiveAlert(alertDefinition);
        if (!alertDefinition.Rule.ObservableExpression) {
            return;
        }
        const isValidExpression = this.adaptableApi.expressionApi.isValidObservableExpression(alertDefinition.Rule.ObservableExpression, ModuleConstants_1.AlertModuleId, `Invalid Alert rule with observable expression `);
        if (!isValidExpression) {
            this.adaptableApi.logWarn(`Could NOT create reactive alert due to invalid Rule definition!`, alertDefinition.Rule);
            return;
        }
        const reactiveExpressionObservable = this.evaluateReactiveExpression(alertDefinition.Rule);
        const reactiveAlertSubscription = (0, rxjs_1.combineLatest)([
            (0, rxjs_1.of)(alertDefinition),
            reactiveExpressionObservable,
        ]).subscribe(([alertDefinition, changeLogEntry]) => {
            const reactiveAlertInfo = (0, IAlertService_1.isRowDataChangedInfo)(changeLogEntry)
                ? {
                    alertDefinition,
                    type: 'rowDataChangedAlert',
                    gridChangeLogEntry: changeLogEntry,
                }
                : {
                    alertDefinition,
                    type: 'cellDataChangedAlert',
                    cellChangeLogEntry: changeLogEntry,
                };
            this.emitter.emitSync('ReactiveAlertTriggered', reactiveAlertInfo);
        });
        this.reactiveAlertsMap.set(alertDefinition.Uuid, {
            definition: alertDefinition,
            subscription: reactiveAlertSubscription,
        });
    }
    deleteReactiveAlert(alertDefinition) {
        if (this.reactiveAlertsMap.has(alertDefinition.Uuid)) {
            const obsoleteReactiveAlert = this.reactiveAlertsMap.get(alertDefinition.Uuid);
            obsoleteReactiveAlert?.subscription.unsubscribe();
            this.reactiveAlertsMap.delete(alertDefinition.Uuid);
        }
    }
    getReactiveActiveAlerts() {
        return [...this.reactiveAlertsMap.values()].map((item) => item.definition);
    }
    isReactiveAlertActive(alertDefinition) {
        return this.reactiveAlertsMap.has(alertDefinition.Uuid);
    }
    evaluateReactiveExpression(rule) {
        return this.adaptableApi.internalApi
            .getQueryLanguageService()
            .evaluateObservableExpression(rule.ObservableExpression, ModuleConstants_1.AlertModuleId);
    }
}
exports.AlertService = AlertService;
