/*!
 * Copyright 2017 by ChartIQ, Inc.
 * All rights reserved.
 */
import React, { useEffect, useState } from "react";
import "./workspaceManagementMenu.css";
import { Actions as WorkspaceManagementMenuActions } from "./stores/workspaceManagementMenuStore";
import WorkspaceActions from "./components/WorkspaceActions";
import WorkspaceList from "./components/WorkspaceList";
import { Menu } from "../../menu/Menu";
import { storeManager } from "./stores/workspaceManagementMenuStore";
/**
 * A button that opens the workspace management menu when clicked.
 */
export const WorkspaceManagementMenu = (props) => {
    var _a, _b, _c;
    const [store, setStore] = useState(null);
    const [workspaceList, setWorkspaceList] = useState([]);
    const [activeWorkspace, setActiveWorkspace] = useState(null);
    const [initialized, setInitialized] = useState(false);
    const [isSwitchingWorkspaces, setIsSwitchingWorkspaces] = useState(false);
    const onStoreChange = async () => {
        setWorkspaceList(await WorkspaceManagementMenuActions.getWorkspaceList());
        setActiveWorkspace(await WorkspaceManagementMenuActions.getActiveWorkspace());
        setIsSwitchingWorkspaces(await WorkspaceManagementMenuActions.getIsSwitchingWorkspaces());
    };
    const onInitialized = (initializedStore) => {
        setStore(initializedStore);
        setInitialized(true);
    };
    useEffect(() => {
        if (store === null) {
            return;
        }
        store.addListener({ field: "WorkspaceList" }, onStoreChange);
        store.addListener({ field: "activeWorkspace" }, onStoreChange);
        store.addListener({ field: "isSwitchingWorkspaces" }, onStoreChange);
        onStoreChange();
    }, [store]);
    useEffect(() => {
        storeManager.addInitializedListener(onInitialized);
        storeManager.initialize();
        return () => {
            storeManager.removeInitializedListener(onInitialized);
            if (initialized) {
                store === null || store === void 0 ? void 0 : store.removeListener({ field: "WorkspaceList" }, onStoreChange);
                store === null || store === void 0 ? void 0 : store.removeListener({ field: "activeWorkspace" }, onStoreChange);
                store === null || store === void 0 ? void 0 : store.removeListener({ field: "isSwitchingWorkspaces" }, onStoreChange);
            }
        };
    }, []);
    let completeProps = {
        ...props,
        width: (_a = props.width) !== null && _a !== void 0 ? _a : 330,
        className: (_b = props.className) !== null && _b !== void 0 ? _b : "workspace-management-menu",
        id: (_c = props.id) !== null && _c !== void 0 ? _c : "WorkspaceMenu",
        title: activeWorkspace === null || activeWorkspace === void 0 ? void 0 : activeWorkspace.name,
    };
    return (React.createElement(Menu, { ...completeProps }, initialized && activeWorkspace ? (React.createElement(React.Fragment, null,
        React.createElement(WorkspaceActions, null),
        React.createElement(WorkspaceList, { isSwitchingWorkspaces: isSwitchingWorkspaces, activeWorkspace: activeWorkspace, workspaces: workspaceList }))) : null));
};
//# sourceMappingURL=WorkspaceManagementMenu.js.map