// src/react-host/loader.ts
import { createRequire } from "node:module";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
function findPackageRoot(start) {
  let current = path.resolve(start);
  while (true) {
    const packageJson = path.join(current, "package.json");
    if (fs.existsSync(packageJson)) {
      return current;
    }
    const parent = path.dirname(current);
    if (parent === current) {
      return null;
    }
    current = parent;
  }
}
function createPackageRequire(packageRoot) {
  return createRequire(path.join(packageRoot, "package.json"));
}
function candidatePackageRoots() {
  const roots = /* @__PURE__ */ new Set();
  const addCandidate = (candidate) => {
    if (!candidate) {
      return;
    }
    const root = findPackageRoot(candidate);
    if (root) {
      roots.add(root);
    }
  };
  const argv1 = process.argv[1];
  let argv1Real;
  if (argv1) {
    try {
      argv1Real = fs.realpathSync(argv1);
    } catch {
      argv1Real = argv1;
    }
  }
  const packageRoot = fileURLToPath(new URL("../../..", import.meta.url));
  addCandidate(process.env.INDUSAGI_REACT_HOST_ROOT);
  addCandidate(argv1Real ? path.dirname(argv1Real) : void 0);
  addCandidate(argv1 ? path.dirname(argv1) : void 0);
  addCandidate(process.env.INIT_CWD);
  addCandidate(process.cwd());
  addCandidate(packageRoot);
  return [...roots];
}
function resolveHostModule(specifier) {
  const failures = [];
  for (const packageRoot of candidatePackageRoots()) {
    try {
      return createPackageRequire(packageRoot).resolve(specifier);
    } catch (error) {
      failures.push(`${packageRoot}: ${String(error.message ?? error)}`);
    }
  }
  throw new Error(
    `Unable to resolve host module "${specifier}" from any detected package root.
${failures.join("\n")}`
  );
}
function normalizeModuleNamespace(moduleNamespace) {
  const defaultExport = moduleNamespace.default;
  if (defaultExport && typeof defaultExport === "object") {
    return { ...defaultExport, ...moduleNamespace };
  }
  return moduleNamespace;
}
async function importHostModule(specifier) {
  const resolved = resolveHostModule(specifier);
  const moduleNamespace = await import(pathToFileURL(resolved).href);
  return normalizeModuleNamespace(moduleNamespace);
}
var jsxRuntimeInstance = null;
function loadHostJsxRuntime() {
  if (!jsxRuntimeInstance) {
    jsxRuntimeInstance = importHostModule("react/jsx-runtime");
  }
  return jsxRuntimeInstance;
}

// src/react-host/jsx-runtime.ts
var jsxRuntime = await loadHostJsxRuntime();
var Fragment = jsxRuntime.Fragment;
var jsx = jsxRuntime.jsx;
var jsxs = jsxRuntime.jsxs;
export {
  Fragment,
  jsx,
  jsxs
};
