import {
  createDispatchHook,
  createSelectorHook,
  createStoreHook,
  Provider as RawProvider,
  type ProviderProps,
  type ReactReduxContextValue,
} from "react-redux";
import type { Store, State } from "./store.js";
import { createContext } from "react";

const AbodeStoreContext = createContext<ReactReduxContextValue | null>(null);
AbodeStoreContext.displayName = "AbodeStoreContext";

export function Provider(
  props: Omit<ProviderProps, "context" | "store" | "serverState"> & {
    store: Store;
    serverState?: State;
  },
) {
  return <RawProvider context={AbodeStoreContext} {...props} />;
}
Object.assign(Provider, { displayName: "AbodeStoreProvider" });
export const useSelector =
  createSelectorHook(AbodeStoreContext).withTypes<State>();
export const useStore = createStoreHook(AbodeStoreContext).withTypes<Store>();
export const useDispatch =
  createDispatchHook(AbodeStoreContext).withTypes<Store["dispatch"]>();
