/*!
 * Copyright 2017 by ChartIQ, Inc.
 * All rights reserved.
 */
/**
 * This is the list of all components in the appLauncher.
 */
import React, { useEffect, useState } from "react";
import { getStore, Actions as appLauncherActions } from "../stores/appLauncherStore";
import { MenuItem } from "../../../menu/MenuItem";
import { FavoriteMaker } from "../../../favorites/FavoriteMaker";
import { useFavorites } from "../../../../hooks/useFavorites";
import { FEAGlobals } from "../../../../../common/Globals";
let appLauncherStore;
/**
 * Handles deleting a component and removing it from favorites if necessary
 *
 * @param {object} props
 * @param {string} props.name
 */
const DeleteComponent = ({ name }) => {
    const { isFavorited, removeFavorite } = useFavorites();
    const doDeleteComponent = (e) => {
        e.preventDefault();
        e.stopPropagation();
        appLauncherActions.handleRemoveCustomComponent(name, (wasDeleted) => {
            if (wasDeleted && isFavorited(name)) {
                removeFavorite(name);
            }
        });
    };
    return (React.createElement("span", { onClick: doDeleteComponent, className: "menu-item-icon" },
        React.createElement("i", { className: "ff-adp-trash-outline", title: "Delete" })));
};
const AppLauncherContainer = (props) => {
    const [componentList, setComponentList] = useState({});
    const [filteredListOfComponents, setFilteredListOfComponents] = useState([]);
    useEffect(() => {
        var _a;
        if (props.componentFilter !== filteredListOfComponents) {
            setFilteredListOfComponents((_a = props.componentFilter) !== null && _a !== void 0 ? _a : []);
        }
    }, [props]);
    const onComponentListUpdate = (err, data) => {
        FEAGlobals.FSBL.Clients.Logger.system.debug("appLauncher onComponentListUpdate");
        setComponentList(data.value);
    };
    const setInitialState = () => {
        appLauncherStore === null || appLauncherStore === void 0 ? void 0 : appLauncherStore.get(["componentList"], (err, data) => {
            if (typeof data !== "undefined") {
                setComponentList(data);
            }
        });
    };
    useEffect(() => {
        appLauncherStore = getStore();
        setInitialState();
        appLauncherStore === null || appLauncherStore === void 0 ? void 0 : appLauncherStore.addListener({ field: "componentList" }, onComponentListUpdate);
        return () => {
            appLauncherStore === null || appLauncherStore === void 0 ? void 0 : appLauncherStore.removeListener({ field: "componentList" }, onComponentListUpdate);
        };
    }, []);
    const launchComponent = (component, params, cb) => {
        // If we are launching a group
        if (component.group) {
            for (let i of Object.keys(component.list)) {
                const cloneParams = Object.assign({}, params);
                const c = component.list[i];
                if (c.component.windowGroup) {
                    cloneParams.groupName = c.component.windowGroup;
                }
                appLauncherActions.launchComponent(component.list[i], params, cb);
            }
        }
        else {
            if (component.component.windowGroup) {
                params.groupName = component.component.windowGroup;
            }
            appLauncherActions.launchComponent(component, params, cb);
        }
    };
    const buildComponentItem = (params) => {
        var _a, _b, _c, _d;
        const { key, isUserDefined } = params;
        const config = componentList === null || componentList === void 0 ? void 0 : componentList[key];
        let displayName = key;
        // If the app isn't launchable by the user, don't show it
        if ((!config ||
            !config.window ||
            !config.foreign ||
            !config.foreign.components ||
            !config.foreign.components["App Launcher"] ||
            !config.foreign.components["App Launcher"].launchableByUser) &&
            !(config === null || config === void 0 ? void 0 : config.group)) {
            return;
        }
        // If the app is temporary (actively being configured in the Smart Desktop Designer), don't show it
        if ((_b = (_a = config.appConfig) === null || _a === void 0 ? void 0 : _a.hostManifest) === null || _b === void 0 ? void 0 : _b.Finsemble.temporary) {
            return;
        }
        // Component developers can define a display name that will show instead of the component's type.
        if (config.component && config.component.displayName) {
            ({ displayName } = config.component);
        }
        const iconDescriptor = config.icons.toolbarIcon;
        return (React.createElement(MenuItem, { key: key, onClick: () => launchComponent(config, {}), lang: (_c = config.appConfig) === null || _c === void 0 ? void 0 : _c.lang },
            React.createElement("div", { className: "menu-item-row" },
                React.createElement("span", { className: "app-name", title: displayName }, displayName),
                React.createElement("span", { className: "menu-item-row-push-right" },
                    isUserDefined && React.createElement(DeleteComponent, { name: displayName }),
                    !props.suppressFavorites && (React.createElement(FavoriteMaker, { id: (_d = config === null || config === void 0 ? void 0 : config.component) === null || _d === void 0 ? void 0 : _d.type, category: "Application", name: displayName, icon: iconDescriptor, className: "menu-item-icon" }))))));
    };
    if (!componentList) {
        return React.createElement("div", null);
    }
    const components = Object.keys(componentList !== null && componentList !== void 0 ? componentList : {}).filter((componentType) => {
        if (filteredListOfComponents.length) {
            return filteredListOfComponents.includes(componentType);
        }
        return true;
    });
    // if this is greater than 0, we don't show a note telling the user to check their configs.
    // iterate through componentList and render a componentItem for each one.
    const list = components.map((key, i) => {
        const { component } = componentList[key];
        const isUserDefined = Boolean(component && component.isUserDefined);
        return buildComponentItem({ i, key, isUserDefined });
    });
    return (React.createElement("div", { className: "app-launcher-list" }, list.length ? (list) : (React.createElement("p", null,
        " ",
        "No components loaded. Make sure to check ./src/components.json to make sure you've set everything up correctly."))));
};
export default AppLauncherContainer;
//# sourceMappingURL=componentList.js.map