import "../../../../assets/css/advancedAppLauncher.css";
// Import js modules
import React from "react";
import { createStore, getStore } from "./stores/LauncherStore";
import storeActions from "./stores/StoreActions";
// Import React components
import Content from "./components/Content";
import AddNewAppForm from "./components/AddNewAppForm";
import FoldersList from "./components/FoldersList";
import LeftNavBottomLinks from "./components/LeftNavBottomLinks";
import { FEAGlobals } from "../../../../../common/Globals";
/**
 * The "app" button that launches the Advanced App Launcher.
 * The Advanced App Launcher comes with search capabilities and allows users to organize their application components into folders.
 *
 * @param {boolean} [enableQuickComponents=false] DEPRECATED: Set to true to allow users to create their own components
 */
export class AdvancedAppLauncher extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isFormVisible: storeActions.getFormStatus(),
            advancedAppLauncherStore: null,
        };
        this.toggleAddNewAppForm = this.toggleAddNewAppForm.bind(this);
        this.openAppMarket = this.openAppMarket.bind(this);
        this.handleWindowBlur = this.handleWindowBlur.bind(this);
    }
    UNSAFE_componentWillMount() {
        createStore(async (error, store) => {
            storeActions.initialize(() => {
                store === null || store === void 0 ? void 0 : store.addListener({ field: "isFormVisible" }, this.toggleAddNewAppForm);
                this.forceUpdate();
            });
            // Delete the search text from the store as searches should not persist over restart.
            // The deletion is done on startup as a shutdown listener wouldn't trigger on crashes
            await (store === null || store === void 0 ? void 0 : store.set(["filterText"], ""));
            this.setState({
                advancedAppLauncherStore: store,
            });
        });
        window.addEventListener("blur", this.handleWindowBlur);
    }
    componentWillUnmount() {
        var _a;
        (_a = getStore()) === null || _a === void 0 ? void 0 : _a.removeListener({ field: "isFormVisible" }, this.toggleAddNewAppForm);
        window.removeEventListener("blur", this.handleWindowBlur);
    }
    handleWindowBlur() {
        this.setState({
            isFormVisible: false,
        });
    }
    /**
     * Sets isFormVisible to true or false in state
     * isFormVisible is used to conditionally show/hide the
     * add new component form.
     * @param {object} error
     * @param {object} data
     */
    toggleAddNewAppForm(error, data) {
        this.setState({
            isFormVisible: data.value,
        });
    }
    /**
     * Sets isFormVisible to false in store to remove the form
     */
    onAddNewAppFormAction() {
        var _a;
        (_a = getStore()) === null || _a === void 0 ? void 0 : _a.set(["isFormVisible"], false, (error) => {
            error && console.log("Failed to set isFormVisible to false");
        });
    }
    openAppMarket() {
        FEAGlobals.FSBL.Clients.LauncherClient.showWindow({
            componentType: "AppCatalog",
        }, {
            monitor: "mine",
            staggerPixels: 0,
            spawnIfNotFound: true,
            left: "center",
            top: "center",
        });
    }
    render() {
        if (!this.state.advancedAppLauncherStore)
            return null;
        return (React.createElement("div", { className: "advanced-app-launcher" },
            React.createElement("div", { className: "complex-menu-wrapper" },
                React.createElement("div", { className: "complex-menu-left-nav" },
                    React.createElement(FoldersList, null),
                    React.createElement(LeftNavBottomLinks, { openAppMarket: this.openAppMarket, enableQuickComponents: this.props.enableQuickComponents })),
                React.createElement(Content, null),
                this.state.isFormVisible && (React.createElement(AddNewAppForm, { onDone: this.onAddNewAppFormAction, customURLValidation: this.props.customURLValidation, customAddComponent: this.props.customAddComponent, beforeAddComponent: this.props.beforeAddComponent })))));
    }
}
//# sourceMappingURL=AdvancedAppLauncher.js.map