export const createApplicationStateManager = (applicationState, hooks, notificationCenter) => {
    const applicationStateSubscription = applicationState.onValueChange((key, value) => notificationCenter.send("ApplicationState", "ApplicationStateValueChanged", {
        key,
        value,
    }));
    const resultProcessingSubscription = hooks.beforeOperationResultProcessing.tap("ApplicationState", results => {
        const oldSelection = applicationState.getValue("selection");
        if (oldSelection.length === 0) {
            return;
        }
        const newSelection = [];
        for (const comId of oldSelection) {
            if (!results.find(item => item.comId === comId && item.type === "delete")) {
                newSelection.push(comId);
            }
        }
        if (oldSelection.length !== newSelection.length) {
            applicationState.setValue("selection", newSelection);
        }
    });
    const destroy = () => {
        applicationStateSubscription();
        resultProcessingSubscription();
    };
    return {
        destroy,
    };
};
