"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const reactStore = require("@tanstack/react-store");
const React = require("react");
const routerCore = require("@tanstack/router-core");
const isServer = require("@tanstack/router-core/isServer");
const useRouter = require("./useRouter.cjs");
function useRouterState(opts) {
  const contextRouter = useRouter.useRouter({
    warn: opts?.router === void 0
  });
  const router = opts?.router || contextRouter;
  const _isServer = isServer.isServer ?? router.isServer;
  if (_isServer) {
    const state = router.state;
    return opts?.select ? opts.select(state) : state;
  }
  const previousResult = (
    // eslint-disable-next-line react-hooks/rules-of-hooks
    React.useRef(void 0)
  );
  return reactStore.useStore(router.__store, (state) => {
    if (opts?.select) {
      if (opts.structuralSharing ?? router.options.defaultStructuralSharing) {
        const newSlice = routerCore.replaceEqualDeep(
          previousResult.current,
          opts.select(state)
        );
        previousResult.current = newSlice;
        return newSlice;
      }
      return opts.select(state);
    }
    return state;
  });
}
exports.useRouterState = useRouterState;
//# sourceMappingURL=useRouterState.cjs.map
