"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// modules/react/hooks/index.ts
var hooks_exports = {};
__export(hooks_exports, {
  useBroadcastChannel: () => useBroadcastChannel,
  useRouter: () => useRouter
});
module.exports = __toCommonJS(hooks_exports);

// modules/react/hooks/useBroadcastChannel/index.tsx
var import_react = require("react");
var useBroadcastChannel = (channelName) => {
  const [message, setMessage] = (0, import_react.useState)(null);
  const channelRef = (0, import_react.useRef)(null);
  (0, import_react.useEffect)(() => {
    if (!channelRef.current && typeof window !== "undefined") {
      channelRef.current = new BroadcastChannel(channelName);
    }
    const channel = channelRef.current;
    if (!channel) return;
    const handleMessage = (event) => {
      setMessage(event.data);
    };
    channel.onmessage = handleMessage;
    return () => {
      if (channelRef.current) {
        channelRef.current.close();
        channelRef.current = null;
      }
    };
  }, [channelName]);
  const sendMessage = (0, import_react.useCallback)((msg) => {
    if (channelRef.current) {
      channelRef.current.postMessage(msg);
    }
  }, []);
  return { message, sendMessage };
};

// modules/react/hooks/useRouter/index.ts
var import_react3 = require("react");

// modules/runtime/client/RouterContext.tsx
var import_react2 = require("react");
var RouterContext = (0, import_react2.createContext)(null);

// modules/runtime/client/constants.ts
var WINDOW_DATA_KEY = "__FW_DATA__";
var ROUTER_DATA_KEY = "__LOLY_ROUTER_DATA__";
var ROUTER_NAVIGATE_KEY = "__LOLY_ROUTER_NAVIGATE__";

// modules/runtime/client/window-data.ts
function getWindowData() {
  if (typeof window === "undefined") {
    return null;
  }
  return window[WINDOW_DATA_KEY] ?? null;
}
function getRouterData() {
  if (typeof window === "undefined") {
    return null;
  }
  return window[ROUTER_DATA_KEY] ?? null;
}

// modules/react/hooks/useRouter/index.ts
function useRouter() {
  const context = (0, import_react3.useContext)(RouterContext);
  const navigate = context?.navigate;
  const navigateRef = (0, import_react3.useRef)(navigate);
  (0, import_react3.useEffect)(() => {
    navigateRef.current = navigate;
  }, [navigate]);
  const [routeData, setRouteData] = (0, import_react3.useState)(() => {
    if (typeof window === "undefined") {
      return {
        pathname: "",
        query: {},
        searchParams: {},
        params: {}
      };
    }
    const data = getWindowData();
    const routerData = getRouterData();
    const searchParams = routerData?.searchParams || parseQueryString(window.location.search);
    return {
      pathname: routerData?.pathname || data?.pathname || window.location.pathname,
      query: searchParams,
      // For backward compatibility
      searchParams,
      params: routerData?.params || data?.params || {}
    };
  });
  (0, import_react3.useEffect)(() => {
    if (typeof window === "undefined") return;
    const handleDataRefresh = () => {
      const data = getWindowData();
      const routerData = getRouterData();
      const currentPathname = window.location.pathname;
      const currentSearch = window.location.search;
      const searchParams = routerData?.searchParams || parseQueryString(currentSearch);
      setRouteData({
        pathname: routerData?.pathname || data?.pathname || currentPathname,
        query: searchParams,
        // For backward compatibility
        searchParams,
        params: routerData?.params || data?.params || {}
      });
    };
    window.addEventListener("fw-data-refresh", handleDataRefresh);
    window.addEventListener("fw-router-data-refresh", handleDataRefresh);
    const handlePopState = () => {
      handleDataRefresh();
    };
    window.addEventListener("popstate", handlePopState);
    return () => {
      window.removeEventListener("fw-data-refresh", handleDataRefresh);
      window.removeEventListener("fw-router-data-refresh", handleDataRefresh);
      window.removeEventListener("popstate", handlePopState);
    };
  }, []);
  const push = (0, import_react3.useCallback)(
    async (url, options) => {
      const fullUrl = url.startsWith("/") ? url : `/${url}`;
      const getCurrentNavigate = () => {
        if (navigateRef.current) return navigateRef.current;
        if (navigate) return navigate;
        if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
          return window[ROUTER_NAVIGATE_KEY];
        }
        return null;
      };
      let currentNavigate = getCurrentNavigate();
      if (typeof window === "undefined") {
        return;
      }
      if (!currentNavigate) {
        for (let i = 0; i < 10; i++) {
          await new Promise((resolve) => setTimeout(resolve, 10));
          currentNavigate = getCurrentNavigate();
          if (currentNavigate) break;
        }
      }
      if (!currentNavigate) {
        window.location.href = fullUrl;
        return;
      }
      const currentUrl = window.location.pathname + window.location.search;
      if (fullUrl === currentUrl) {
        return;
      }
      window.history.pushState({}, "", fullUrl);
      await currentNavigate(fullUrl, options);
    },
    [navigate]
    // Include navigate in dependencies so it updates when context becomes available
  );
  const replace = (0, import_react3.useCallback)(
    async (url, options) => {
      const fullUrl = url.startsWith("/") ? url : `/${url}`;
      const getCurrentNavigate = () => {
        if (navigateRef.current) return navigateRef.current;
        if (navigate) return navigate;
        if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
          return window[ROUTER_NAVIGATE_KEY];
        }
        return null;
      };
      let currentNavigate = getCurrentNavigate();
      if (typeof window === "undefined") {
        return;
      }
      if (!currentNavigate) {
        for (let i = 0; i < 10; i++) {
          await new Promise((resolve) => setTimeout(resolve, 10));
          currentNavigate = getCurrentNavigate();
          if (currentNavigate) break;
        }
      }
      if (!currentNavigate) {
        window.location.replace(fullUrl);
        return;
      }
      window.history.replaceState({}, "", fullUrl);
      await currentNavigate(fullUrl, options);
    },
    [navigate]
  );
  const back = (0, import_react3.useCallback)(() => {
    if (typeof window !== "undefined") {
      window.history.back();
    }
  }, []);
  const refresh = (0, import_react3.useCallback)(async () => {
    const currentUrl = typeof window !== "undefined" ? window.location.pathname + window.location.search : routeData.pathname;
    const getCurrentNavigate = () => {
      if (navigateRef.current) return navigateRef.current;
      if (navigate) return navigate;
      if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
        return window[ROUTER_NAVIGATE_KEY];
      }
      return null;
    };
    let currentNavigate = getCurrentNavigate();
    if (typeof window === "undefined") {
      return;
    }
    if (!currentNavigate) {
      for (let i = 0; i < 10; i++) {
        await new Promise((resolve) => setTimeout(resolve, 10));
        currentNavigate = getCurrentNavigate();
        if (currentNavigate) break;
      }
    }
    if (!currentNavigate) {
      window.location.reload();
      return;
    }
    await currentNavigate(currentUrl, { revalidate: true });
  }, [navigate, routeData.pathname]);
  return {
    push,
    replace,
    back,
    refresh,
    pathname: routeData.pathname,
    query: routeData.query,
    searchParams: routeData.searchParams,
    params: routeData.params
  };
}
function parseQueryString(search) {
  const params = {};
  if (!search || search.length === 0) return params;
  const queryString = search.startsWith("?") ? search.slice(1) : search;
  const pairs = queryString.split("&");
  for (const pair of pairs) {
    const [key, value] = pair.split("=");
    if (key) {
      params[decodeURIComponent(key)] = value ? decodeURIComponent(value) : "";
    }
  }
  return params;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useBroadcastChannel,
  useRouter
});
//# sourceMappingURL=hooks.cjs.map