"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/components/index.ts
var components_exports = {};
__export(components_exports, {
  Image: () => Image,
  Link: () => Link
});
module.exports = __toCommonJS(components_exports);

// modules/react/components/Image/index.tsx
var import_jsx_runtime = require("react/jsx-runtime");
function Image({
  src,
  alt,
  width,
  height,
  className,
  ...rest
}) {
  const style = {};
  if (width) style.width = width;
  if (height) style.height = height;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    "img",
    {
      src,
      alt,
      width,
      height,
      className,
      style,
      ...rest
    }
  );
}

// modules/react/cache/client-data-cache/index.ts
var CACHE_KEY = "__FW_DATA_CACHE__";
var MAX_CACHE_SIZE = 100;
function getCacheStore() {
  if (typeof window !== "undefined") {
    if (!window[CACHE_KEY]) {
      window[CACHE_KEY] = {
        data: /* @__PURE__ */ new Map(),
        index: /* @__PURE__ */ new Map(),
        lru: []
      };
    }
    return window[CACHE_KEY];
  }
  return {
    data: /* @__PURE__ */ new Map(),
    index: /* @__PURE__ */ new Map(),
    lru: []
  };
}
var cacheStore = getCacheStore();
var dataCache = cacheStore.data;
var pathIndex = cacheStore.index;
var lru = cacheStore.lru;
function extractPathBase(key) {
  return key.split("?")[0];
}
function addToIndex(key) {
  const pathBase = extractPathBase(key);
  if (!pathIndex.has(pathBase)) {
    pathIndex.set(pathBase, /* @__PURE__ */ new Set());
  }
  pathIndex.get(pathBase).add(key);
}
function removeFromIndex(key) {
  const pathBase = extractPathBase(key);
  const keys = pathIndex.get(pathBase);
  if (keys) {
    keys.delete(key);
    if (keys.size === 0) {
      pathIndex.delete(pathBase);
    }
  }
}
function updateLRU(key) {
  const index = lru.indexOf(key);
  if (index !== -1) {
    lru.splice(index, 1);
  }
  lru.push(key);
}
function evictOldest() {
  while (lru.length >= MAX_CACHE_SIZE && lru.length > 0) {
    const oldestKey = lru.shift();
    dataCache.delete(oldestKey);
    removeFromIndex(oldestKey);
  }
}
function setCacheEntry(key, entry) {
  const existingEntry = dataCache.get(key);
  const wasFulfilled = existingEntry?.status === "fulfilled";
  dataCache.set(key, entry);
  if (entry.status === "fulfilled") {
    if (!wasFulfilled) {
      addToIndex(key);
    }
    updateLRU(key);
    evictOldest();
  } else if (wasFulfilled) {
    removeFromIndex(key);
  }
}
function buildDataUrl(url) {
  return url + (url.includes("?") ? "&" : "?") + "__fw_data=1";
}
async function fetchRouteDataOnce(url, skipLayoutHooks = true) {
  const dataUrl = buildDataUrl(url);
  const headers = {
    "x-fw-data": "1",
    Accept: "application/json"
  };
  if (skipLayoutHooks) {
    headers["x-skip-layout-hooks"] = "true";
  }
  const res = await fetch(dataUrl, { headers });
  let json = {};
  try {
    const text = await res.text();
    if (text) {
      json = JSON.parse(text);
    }
  } catch (parseError) {
    console.error(
      "[client][cache] Failed to parse response as JSON:",
      parseError
    );
  }
  const result = {
    ok: res.ok,
    status: res.status,
    json
  };
  return result;
}
function prefetchRouteData(url) {
  const key = buildDataUrl(url);
  const cached = dataCache.get(key);
  if (cached && cached.status !== "rejected") {
    if (cached.status === "fulfilled") {
      updateLRU(key);
    }
    return;
  }
  const promise = fetchRouteDataOnce(url, true).then((value) => {
    setCacheEntry(key, { status: "fulfilled", value });
    return value;
  }).catch((error) => {
    console.error("[client][cache] Error prefetching route data:", error);
    dataCache.set(key, { status: "rejected", error });
    throw error;
  });
  dataCache.set(key, { status: "pending", promise });
}

// modules/react/components/Link/index.tsx
var import_react = require("react");
var import_jsx_runtime2 = require("react/jsx-runtime");
function isExternal(href) {
  try {
    const url = new URL(href, window.location.href);
    return url.origin !== window.location.origin;
  } catch {
    return false;
  }
}
function Link({
  href,
  prefetch = true,
  children,
  ...rest
}) {
  const ref = (0, import_react.useRef)(null);
  (0, import_react.useEffect)(() => {
    if (!prefetch || !ref.current || isExternal(href)) return;
    const el = ref.current;
    let prefetched = false;
    const io = new IntersectionObserver(([entry]) => {
      if (entry.isIntersecting && !prefetched) {
        prefetched = true;
        prefetchRouteData(href);
      }
    });
    io.observe(el);
    return () => io.disconnect();
  }, [href, prefetch]);
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", { ref, href, ...rest, children });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Image,
  Link
});
//# sourceMappingURL=components.cjs.map