"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
exports.createApplicationStateManager = function (applicationState, hooks, notificationCenter) {
    var applicationStateSubscription = applicationState.onValueChange(function (key, value) {
        return notificationCenter.send("ApplicationState", "ApplicationStateValueChanged", {
            key: key,
            value: value,
        });
    });
    var resultProcessingSubscription = hooks.beforeOperationResultProcessing.tap("ApplicationState", function (results) {
        var e_1, _a;
        var oldSelection = applicationState.getValue("selection");
        if (oldSelection.length === 0) {
            return;
        }
        var newSelection = [];
        var _loop_1 = function (comId) {
            if (!results.find(function (item) { return item.comId === comId && item.type === "delete"; })) {
                newSelection.push(comId);
            }
        };
        try {
            for (var oldSelection_1 = tslib_1.__values(oldSelection), oldSelection_1_1 = oldSelection_1.next(); !oldSelection_1_1.done; oldSelection_1_1 = oldSelection_1.next()) {
                var comId = oldSelection_1_1.value;
                _loop_1(comId);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (oldSelection_1_1 && !oldSelection_1_1.done && (_a = oldSelection_1.return)) _a.call(oldSelection_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        if (oldSelection.length !== newSelection.length) {
            applicationState.setValue("selection", newSelection);
        }
    });
    var destroy = function () {
        applicationStateSubscription();
        resultProcessingSubscription();
    };
    return {
        destroy: destroy,
    };
};
