import React, { useEffect, useState } from "react";
import LeftNav from "./components/LeftNav";
import ContentSection from "./components/ContentSection";
import "../../assets/css/userPreferences.css";
import { FEAGlobals } from "../../../common/Globals";
import { clone } from "lodash";
export const UserPreferencesBase = (props) => {
    // The active section is the section that is currently selected and displayed in the preference menu.
    const [activeSection, setActiveSection] = useState("General");
    const [entries, setEntries] = useState(Object.keys(props.sections));
    useEffect(() => {
        function listenForPreference(error, response) {
            if (error) {
                console.error("FinsembleUserPreferencesChannel: ", error);
            }
            else {
                const { data } = response;
                if (data.preferencesTab) {
                    if (entries.includes(data.preferencesTab)) {
                        setActiveSection(data.preferencesTab);
                    }
                    else {
                        console.warn(`Unknown ${data === null || data === void 0 ? void 0 : data.preferencesTab} value for User Preferences sections`);
                    }
                }
            }
        }
        FEAGlobals.FSBL.Clients.RouterClient.addListener("FinsembleUserPreferencesChannel", listenForPreference);
        return () => {
            FEAGlobals.FSBL.Clients.RouterClient.removeListener("FinsembleUserPreferencesChannel", listenForPreference);
        };
    }, []);
    useEffect(() => {
        props.addListener({ field: "activeSection" }, (newValue) => {
            if (newValue === null) {
                return;
            }
            setActiveSection(newValue);
        });
        return function cleanup() {
            props.removeListener({ field: "activeSection" }, setActiveSection);
        };
    }, []);
    // adds and removes preferences sections at runtime
    const updateEntries = (entryName, enable) => {
        let newEntries = clone(entries);
        if (newEntries.includes(entryName)) {
            // remove section
            if (!enable) {
                newEntries = newEntries.filter((entry) => entry !== entryName);
                setEntries(newEntries);
                setActiveSection("General");
            }
        }
        else {
            // add section
            if (enable) {
                newEntries.push(entryName);
                setEntries(newEntries);
                setActiveSection(entryName);
            }
        }
    };
    useEffect(() => {
        const bloombergField = "finsemble.custom.bloomberg";
        const bloombergEntriesName = "Bloomberg";
        // get the initial state
        FEAGlobals.FSBL.Clients.ConfigClient.get(bloombergField.split("."), (err, data) => {
            updateEntries(bloombergEntriesName, !!(data === null || data === void 0 ? void 0 : data.enabled));
        });
        // updates state when it has been updated at runtime
        const bloombergListener = (error, response) => {
            var _a;
            updateEntries(bloombergEntriesName, !!((_a = response === null || response === void 0 ? void 0 : response.value) === null || _a === void 0 ? void 0 : _a.enabled));
        };
        FEAGlobals.FSBL.Clients.ConfigClient.addListener({ field: bloombergField }, bloombergListener);
        return () => {
            FEAGlobals.FSBL.Clients.ConfigClient.removeListener({ field: bloombergField }, bloombergListener);
        };
    });
    useEffect(() => {
        const dashbarField = "finsemble.components.Toolbar.window.data.dashbar";
        const dashbarEntriesName = "Dashbar";
        // get the initial state
        FEAGlobals.FSBL.Clients.ConfigClient.get(dashbarField.split("."), (err, data) => {
            var _a;
            updateEntries(dashbarEntriesName, ((_a = data === null || data === void 0 ? void 0 : data.items) === null || _a === void 0 ? void 0 : _a.length) > 0);
        });
        // updates state when it has been updated at runtime
        const dashbarListener = (error, response) => {
            updateEntries(dashbarEntriesName, !!(response === null || response === void 0 ? void 0 : response.value));
        };
        FEAGlobals.FSBL.Clients.ConfigClient.addListener({ field: dashbarField }, dashbarListener);
        return () => {
            FEAGlobals.FSBL.Clients.ConfigClient.removeListener({ field: dashbarField }, dashbarListener);
        };
    });
    return (React.createElement("div", { className: "complex-menu-wrapper" },
        React.createElement(LeftNav, { activeSection: activeSection, setValue: props.setValue, getValue: props.getValue, entries: entries, setActiveEntry: setActiveSection }),
        React.createElement(ContentSection, { activeSection: activeSection, ...props })));
};
//# sourceMappingURL=UserPreferencesBase.js.map