import * as React from "react";
import { MenuItem } from "../menu/MenuItem";
import { Icon } from "../icon/Icon";
import { Highlight } from "./Highlight";
import { FEAGlobals } from "../../../common/Globals";
export const SearchResult = (props) => {
    var _a, _b, _c, _d;
    const { data, searchQuery, resetSearch } = props;
    const displayName = (_a = data.displayName) !== null && _a !== void 0 ? _a : data.name;
    // Will trigger the action associated with the item
    // (e.g., loading a workspace, spawning a component)
    const onSearchResultClicked = () => {
        var _a;
        FEAGlobals.FSBL.Clients.SearchClient.invokeItemAction(data, (_a = data.actions) === null || _a === void 0 ? void 0 : _a[0]);
        resetSearch();
    };
    const onActionClicked = (action) => {
        FEAGlobals.FSBL.Clients.SearchClient.invokeItemAction(data, action);
        resetSearch();
    };
    // Default icon to be based on initials
    const defaultIcon = {
        imageType: "initials",
        name: displayName,
        category: data.type,
    };
    // Use iconography if provided in the search results
    const maybeProviderIcon = () => {
        if (!data.icon)
            return undefined;
        // Default conversion which can be overridden if the provider explicitly
        // provides `imageType`, `name` or `category` fields.
        return {
            imageType: data.icon.type,
            name: displayName,
            category: data.type,
            ...data.icon,
        };
    };
    const icon = (_b = maybeProviderIcon()) !== null && _b !== void 0 ? _b : defaultIcon;
    return (React.createElement(MenuItem, { onClick: onSearchResultClicked },
        React.createElement("div", { className: "menu-item-row" },
            React.createElement(Icon, { ...icon }),
            React.createElement(Highlight, { text: displayName, matchAgainst: searchQuery || "" }),
            ((_c = data.actions) === null || _c === void 0 ? void 0 : _c.length) > 1 && (React.createElement("div", { className: "search-actions" }, (_d = data.actions) === null || _d === void 0 ? void 0 : _d.map((action, i) => (React.createElement("div", { key: `actionbtn${i}`, className: "search-actions actionButton", onClick: (e) => {
                    e.preventDefault();
                    e.stopPropagation();
                    onActionClicked(action);
                    resetSearch();
                    FEAGlobals.finsembleWindow.blur();
                } }, action.name))))))));
};
//# sourceMappingURL=SearchResult.js.map