/*!
 * Copyright 2017 by ChartIQ, Inc.
 * All rights reserved.
 */
import React from "react";
// data
import storeActions from "../../stores/storeActions";
import { FEAGlobals } from "../../../../../common/Globals";
let pendingSpawn = false;
/**
 * The header of the AppShowcase. Contains the actionable buttons (to add/remove and app) and the app title and icon
 * @param {object} props Component props
 * @param {string} props.iconUrl The image url for the app's icon
 * @param {string} props.name The app name
 * @param {func} props.appAction The function to call when a button is clicked.
 */
const Header = (props) => {
    const addApp = () => {
        if (props.installed) {
            if (pendingSpawn)
                return;
            pendingSpawn = true;
            const name = props.title || props.name;
            // If the app has a URL property
            // For now, this means it was manually added
            // So lets spawn from URL
            if (props.url) {
                FEAGlobals.FSBL.Clients.LauncherClient.spawn("", {
                    url: props.url,
                    addToWorkspace: true,
                }, () => {
                    pendingSpawn = false;
                });
                return;
            }
            // Otherwise launch application by name
            FEAGlobals.FSBL.Clients.LauncherClient.spawn((name !== null && name !== void 0 ? name : "unknown-name").trim(), { addToWorkspace: true }, () => {
                pendingSpawn = false;
            });
        }
        else {
            storeActions.addApp(props.appId);
        }
    };
    const removeApp = () => {
        storeActions.removeApp(props.appId);
    };
    return (React.createElement("div", { className: "header" },
        React.createElement("div", { className: "icon-title-container" },
            React.createElement("img", { className: "header-icon", src: props.iconUrl }),
            React.createElement("span", { className: "appName" }, props.name)),
        React.createElement("div", { className: "action-button-container" },
            React.createElement("button", { className: props.entitled ? "action-button open" : "action-button disabled", disabled: !props.entitled, onClick: addApp }, props.installed ? (React.createElement("div", null,
                React.createElement("span", { className: "action-button-label" }, "Open"))) : (React.createElement("span", { className: "action-button-label" }, "Add"))),
            props.installed && (React.createElement("button", { className: props.entitled ? "action-button remove" : "action-button remove disabled", disabled: !props.entitled, onClick: removeApp },
                React.createElement("div", { className: "ff-adp-trash-outline", title: "Remove App" }))))));
};
export default Header;
//# sourceMappingURL=Header.js.map