const require_rolldown_runtime = require('../../../../../../../../_virtual/rolldown_runtime.cjs');
let node_path = require("node:path");
let node_fs = require("node:fs");
let node_url = require("node:url");

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/_virtual/_utils_asset.mjs
const hereDirname = () => {
	try {
		return (0, node_path.dirname)((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
	} catch {
		return typeof __dirname !== "undefined" ? __dirname : process.cwd();
	}
};
const findDistRoot = (startDir) => {
	let dir = startDir;
	for (let i = 0; i < 12; i++) {
		if ((0, node_path.basename)(dir) === "dist") return dir;
		const parent = (0, node_path.resolve)(dir, "..");
		if (parent === dir) break;
		dir = parent;
	}
	return null;
};
const normalizeFrameFile = (file) => {
	if (!file) return null;
	try {
		if (file.startsWith("file://")) return (0, node_url.fileURLToPath)(file);
	} catch {}
	return file;
};
/**
* Returns the directory of the *caller* module that invoked readAsset.
* Prefers non-virtual frames; falls back to the first real frame.
*/
const getCallerDir = () => {
	const prev = Error.prepareStackTrace;
	try {
		Error.prepareStackTrace = (_, structured) => structured;
		const err = /* @__PURE__ */ new Error();
		Error.captureStackTrace(err, getCallerDir);
		/** @type {import('node:vm').CallSite[]} */
		const frames = err.stack || [];
		const isVirtualPath = (p) => p.includes(`${node_path.sep}_virtual${node_path.sep}`) || p.includes("/_virtual/");
		for (const frame of frames) {
			const file = normalizeFrameFile(typeof frame.getFileName === "function" ? frame.getFileName() : null);
			if (!file) continue;
			if (file.includes("node:internal") || file.includes(`${node_path.sep}internal${node_path.sep}modules${node_path.sep}`)) continue;
			if (!isVirtualPath(file)) return (0, node_path.dirname)(file);
		}
		for (const frame of frames) {
			const file = normalizeFrameFile(typeof frame.getFileName === "function" ? frame.getFileName() : null);
			if (file) return (0, node_path.dirname)(file);
		}
	} catch {} finally {
		Error.prepareStackTrace = prev;
	}
	return hereDirname();
};
/**
* Read an asset copied from src/** to dist/assets/**.
* - './' or '../' is resolved relative to the *caller module's* emitted directory.
* - otherwise, treat as src-relative.
*
* @param {string} relPath - e.g. './PROMPT.md' or 'utils/AI/askDocQuestion/embeddings/<fileKey>.json'
* @param {BufferEncoding} [encoding='utf8']
*/
const readAsset = (relPath, encoding = "utf8") => {
	const here = hereDirname();
	const distRoot = findDistRoot(here) ?? (0, node_path.resolve)(here, "..", "..", "dist");
	const assetsRoot = (0, node_path.join)(distRoot, "assets");
	const tried = [];
	/**
	* Transform dist/(esm|cjs)/... and _virtual/ prefix to clean subpath (Windows-safe)
	*/
	const callerSubpath = (0, node_path.relative)(distRoot, getCallerDir()).split("\\").join("/").replace(/^(?:dist\/)?(?:esm|cjs)\//, "").replace(/^_virtual\//, "");
	if (relPath.startsWith("./") || relPath.startsWith("../")) {
		const fromCallerAbs = (0, node_path.resolve)(assetsRoot, callerSubpath, relPath);
		tried.push(fromCallerAbs);
		if ((0, node_fs.existsSync)(fromCallerAbs)) return (0, node_fs.readFileSync)(fromCallerAbs, encoding);
	}
	const directPath = (0, node_path.join)(assetsRoot, relPath);
	tried.push(directPath);
	if ((0, node_fs.existsSync)(directPath)) return (0, node_fs.readFileSync)(directPath, encoding);
	if (callerSubpath) {
		const nested = (0, node_path.join)(assetsRoot, callerSubpath, relPath);
		tried.push(nested);
		if ((0, node_fs.existsSync)(nested)) return (0, node_fs.readFileSync)(nested, encoding);
	}
	const msg = [
		"readAsset: file not found.",
		"Searched:",
		...tried.map((p) => `- ${p}`)
	].join("\n");
	throw new Error(msg);
};

//#endregion
exports.readAsset = readAsset;
//# sourceMappingURL=_utils_asset.cjs.map