import { configureStore } from "@reduxjs/toolkit";
import { remoteReduxClientEnhancer } from "./remoteReduxEnhancer";
import { singletonFactory } from "../Singleton";
import { RouterClient } from "../../clients/routerClient";
// The local store doesn't have any reducers, but redux requires at least one, so we provide this reducer that does nothing
const noopReducer = (state) => state;
/**
 * Creates a redux store and attaches it to a remote store.
 *
 * @param server The name of the remote redux server to attach.
 * @param slice The slice from the remote redux server to subscribe.
 * @param emptyStore An empty state for the local store, to handle the initial display prior to the remote side updating the local side.
 * @param dispatchOnly - Set to true to opt out of inbound state changes on the store. This can minimize network traffic for write-only situations.
 * @returns
 */
export const getRemoteStore = ({ server, slice, emptyStore, dispatchOnly = false, }) => configureStore({
    preloadedState: emptyStore,
    reducer: noopReducer,
    enhancers: [
        remoteReduxClientEnhancer({
            server,
            slice,
            dispatchOnly,
        }),
    ],
});
/**
 * Creates a redux store and attaches it to a remote store and then returns it as a singleton. This lazily creates
 * the store and allows re-use without connection overhead.
 *
 * @param singletonName The unique identifier to use for the singleton
 * @param server The name of the remote redux server to attach.
 * @param slice The slice from the remote redux server to subscribe.
 * @param emptyStore An empty state for the local store, to handle the initial display prior to the remote side updating the local side.
 * @param dispatchOnly - Set to true to opt out of inbound state changes on the store. This can minimize network traffic for write-only situations.
 * @returns
 */
export const getRemoteStoreAsSingleton = ({ singletonName, server, slice, emptyStore, dispatchOnly = false, }) => singletonFactory(singletonName, () => getRemoteStore({ server, slice, emptyStore, dispatchOnly }));
/**
 * Resolves to a point in time snapshot of the requested remote redux store's state. If `slice` is specified
 * then only that slice will be returned, otherwise the entire store will be returned.
 */
export const getRemoteState = async ({ server, slice, slices, }) => {
    const { response } = await RouterClient.query(server, {
        "@@finsemble/action": "snapshot",
        "@@finsemble/slice": slice,
        "@@finsemble/slices": slices,
    }, () => { });
    return response.data;
};
//# sourceMappingURL=getRemoteStore.js.map