import React, { useEffect, useState } from "react";
import { Provider } from "react-redux";
import { useDispatch, useSelector } from "react-redux";
import styles from "../../assets/css/windowTitleBar.lazy.css";
import { Startup } from "../../../clients/Startup/Startup";
import { useInjectedTitleBar } from "../../hooks/useInjectedTitleBar";
import { useKeyboardNavigation } from "./useKeyboardNavigation";
import { actions, emptyStore } from "../../../platform/services/window/modules/actions";
import { getRemoteStore } from "../../../common/redux/getRemoteStore";
import { WindowNameContext } from "./WindowTitleBarContext";
import { loadCSSAsStyle } from "../common/loadCSSAsStyle";
export const getStore = () => getRemoteStore({ server: "@@finsemble/remoteRedux/WindowServiceStore", slice: "windowTitleBar", emptyStore });
export const WindowTitleBarContents = (props) => {
    var _a, _b;
    const dispatch = useDispatch();
    const [windowName, setWindowName] = useState(null);
    const state = useSelector((currentState) => currentState);
    useInjectedTitleBar(windowName ? (_a = state.windows[windowName]) === null || _a === void 0 ? void 0 : _a.bumpConfig : null);
    useKeyboardNavigation((_b = props.enableKeyboardNavigation) !== null && _b !== void 0 ? _b : true);
    // Retrieves the windowName asynchronously
    const getWindowName = async () => {
        const { windowName: myWindowName } = await Startup.getInstance().getStartupData();
        setWindowName(myWindowName);
        // This tells the redux store to create a windowTitleBar entry for this window
        if (myWindowName)
            dispatch(actions.register(myWindowName));
    };
    // Add styles lazily, and only once
    useEffect(() => {
        loadCSSAsStyle(styles);
        getWindowName();
    }, []);
    return (
    // Context provider for windowName
    React.createElement(WindowNameContext.Provider, { value: windowName },
        React.createElement("div", { className: "fsbl-header" }, props.children)));
};
/**
 *
 * @param props.enableKeyboardNavigation - Set to false to disable keyboard navigation
 * @returns
 */
export const WindowTitleBarShell = (props) => (
// Redux provider
React.createElement(Provider, { store: getStore() },
    React.createElement(WindowTitleBarContents, { ...props })));
//# sourceMappingURL=WindowTitleBarShell.js.map