"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var SessionContext_1 = require("./SessionContext");
var defaultSelector = function (value) { return value; };
exports.useApplicationStateSubscription = function (selector) {
    if (selector === void 0) { selector = defaultSelector; }
    var session = SessionContext_1.useSessionContext().session;
    var _a = tslib_1.__read(React.useState(function () {
        return selector(session.getApplicationState());
    }), 2), value = _a[0], setValue = _a[1];
    React.useEffect(function () {
        var subscription = session.on("ApplicationStateValueChanged", function () {
            var newValue = selector(session.getApplicationState());
            if (newValue !== value) {
                setValue(newValue);
            }
        });
        return function () { return subscription.destroy(); };
    }, [session]);
    return [value, session.setApplicationState];
};
exports.useApplicationStateValueSubscription = function (key) {
    var session = SessionContext_1.useSessionContext().session;
    var _a = tslib_1.__read(React.useState(function () {
        return session.getApplicationStateValue(key);
    }), 2), value = _a[0], setValue = _a[1];
    React.useEffect(function () {
        var subscription = session.on("ApplicationStateValueChanged", function () {
            var newValue = session.getApplicationStateValue(key);
            if (newValue !== value) {
                setValue(newValue);
            }
        });
        return function () { return subscription.destroy(); };
    }, [session, key]);
    return [
        value,
        function (value) { return session.setApplicationStateValue(key, value); },
    ];
};
