/*!
 * Copyright 2017 by ChartIQ, Inc.
 * All rights reserved.
 * This is a single workspace. When clicked, it requests that the app switch to the clicked workspace.
 */
import React, { useEffect, useState } from "react";
import { MenuItem } from "../../../menu/MenuItem";
import { Actions as WorkspaceManagementMenuActions } from "../stores/workspaceManagementMenuStore";
import { FavoriteMaker } from "../../../favorites/FavoriteMaker";
import { useFavorites } from "../../../../hooks/useFavorites";
import { FEAGlobals } from "../../../../../common/Globals";
const WORKSPACE_PREFERENCES = {
    PERMANENT_WORKSPACES: "finsemble.servicesConfig.workspace.permanentWorkspaces",
};
const RemoveWorkspace = (props) => {
    const { workspace } = props;
    const { isFavorited, removeFavorite } = useFavorites();
    const actuallyRemoveWorkspace = (e) => {
        e.preventDefault();
        e.stopPropagation();
        // Trigger the remove. This will throw a dialog. It will return a boolean
        // for whether it was or was not deleted.
        // If the thing was deleted, remove it from favorites.
        WorkspaceManagementMenuActions.removeWorkspace(workspace, (wasDeleted) => {
            if (wasDeleted && isFavorited(workspace.name)) {
                removeFavorite(workspace.name);
            }
        });
    };
    return (React.createElement("span", { role: "button", tabIndex: 0, onClick: actuallyRemoveWorkspace, className: "menu-item-icon" },
        React.createElement("i", { className: "ff-adp-trash-outline", title: "Delete" })));
};
const Workspace = ({ isSwitchingWorkspaces, isActiveWorkspace, dragHandle, workspace, mainAction }) => {
    const [permanentWorkspaces, setPermanentWorkspaces] = useState([]);
    /**
     * When the main body of the component is clicked, we fire off the mainAction. By default this is to switch workspaces.
     */
    const onClick = () => {
        mainAction({
            name: workspace.name,
        });
    };
    const doSetPermanentWorkspaces = (err, config) => {
        FEAGlobals.FSBL.Clients.Logger.system.log("Workspaces new permanentWorkspaces config", config);
        const { value: pw } = config;
        if (pw && Array.isArray(pw)) {
            setPermanentWorkspaces(pw);
        }
    };
    useEffect(() => {
        FEAGlobals.FSBL.Clients.ConfigClient.addListener({ field: WORKSPACE_PREFERENCES.PERMANENT_WORKSPACES }, doSetPermanentWorkspaces);
        FEAGlobals.FSBL.Clients.ConfigClient.get(WORKSPACE_PREFERENCES.PERMANENT_WORKSPACES.split("."), (err, value) => {
            if (err) {
                FEAGlobals.FSBL.Clients.Logger.system.error("render.getValues error", err);
            }
            if (value) {
                setPermanentWorkspaces(value);
            }
        });
        return () => {
            FEAGlobals.FSBL.Clients.ConfigClient.removeListener({ field: WORKSPACE_PREFERENCES.PERMANENT_WORKSPACES }, doSetPermanentWorkspaces);
        };
    }, []);
    // Actions are "remove" and "pin workspace".
    const showSpinner = isSwitchingWorkspaces && isActiveWorkspace;
    const classes = isActiveWorkspace ? "menu-item active-workspace workspace-name" : "menu-item workspace-name";
    return (React.createElement(MenuItem, { onClick: onClick, className: classes },
        React.createElement("div", { className: "menu-item-row" },
            showSpinner && React.createElement("span", { className: "fsbl-loader" }, "Loading..."),
            !showSpinner && dragHandle && dragHandle.iconClass && React.createElement("span", { className: dragHandle.iconClass }),
            React.createElement("span", { className: "workspace-name", title: workspace.name }, workspace.name),
            React.createElement("div", { className: "menu-item-row-push-right" },
                !isActiveWorkspace && !workspace.permanent && !permanentWorkspaces.includes(workspace.name) && (React.createElement(RemoveWorkspace, { workspace: workspace })),
                React.createElement(FavoriteMaker, { id: workspace.name, name: workspace.name, category: "Workspace", className: "menu-item-icon" })))));
};
export default Workspace;
//# sourceMappingURL=Workspace.js.map