import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { createContext } from "react";
import { Provider, createDispatchHook, createSelectorHook, } from "react-redux";
import { DashboardEventsProvider } from "./DashboardEventsContext.js";
import { useInitializeDashboardStore } from "./useInitializeDashboardStore.js";
/**
 * @alpha
 */
export const ReactDashboardContext = createContext(null);
/**
 * @alpha
 */
export const useDashboardDispatch = createDispatchHook(ReactDashboardContext);
/**
 * Hook for retrieving data from the dashboard state.
 *
 * @example
 * Example how to obtain all insights stored on the dashboard:
 *
 * ```tsx
 * import { useDashboardSelector, selectInsights } from "@gooddata/sdk-ui-dashboard";
 *
 * const CustomDashboardWidget = () => {
 *   const insights = useDashboardSelector(selectInsights);
 *
 *   return (
 *      <pre>{JSON.stringify(insights, null, 2)}</pre>
 *   );
 * }
 * ```
 *
 * @public
 */
export const useDashboardSelector = createSelectorHook(ReactDashboardContext);
/**
 * @internal
 */
export function DashboardStoreProvider(props) {
    const dashboardStore = useInitializeDashboardStore(props);
    const { additionalReduxContext } = props;
    if (!dashboardStore) {
        return null;
    }
    if (additionalReduxContext != undefined && additionalReduxContext !== ReactDashboardContext) {
        /*
         * Setting store into multiple contexts is essential in environments where a dynamically loaded
         * dashboard engine is integrated into an application that adds additional embedded, local plugins
         * which use Redux hooks (useDashboardSelector, useDashboardDispatch)
         *
         * Such local code is typically built against a local version of the sdk-ui-dashboard and has
         * different redux context providers from those used by dynamically loaded engine bundle.
         *
         * When the local code uses redux hooks, it will explode because they look into a different context
         * where the store is unset.
         */
        return (_jsx(Provider, { store: dashboardStore.store, context: ReactDashboardContext, children: _jsx(Provider, { store: dashboardStore.store, context: additionalReduxContext, children: _jsx(DashboardEventsProvider, { registerHandler: dashboardStore.registerEventHandler, unregisterHandler: dashboardStore.unregisterEventHandler, children: props.children }) }) }));
    }
    return (_jsx(Provider, { store: dashboardStore.store, context: ReactDashboardContext, children: _jsx(DashboardEventsProvider, { registerHandler: dashboardStore.registerEventHandler, unregisterHandler: dashboardStore.unregisterEventHandler, children: props.children }) }));
}
//# sourceMappingURL=DashboardStoreProvider.js.map