import { __read } from "tslib";
import * as React from "react";
import { useSessionContext } from "./SessionContext";
var defaultSelector = function (value) { return value; };
export var useApplicationStateSubscription = function (selector) {
    if (selector === void 0) { selector = defaultSelector; }
    var session = useSessionContext().session;
    var _a = __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];
};
export var useApplicationStateValueSubscription = function (key) {
    var session = useSessionContext().session;
    var _a = __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); },
    ];
};
