import { resolveWorkspaceSafePath } from "../../gateway/workspace-editor-path.js";
import { readFile, readdir, stat } from "node:fs/promises";
//#region src/agent/context/expand-at-file-mentions.ts
/** Aligned with web `file-wire-pattern.ts`: unquoted path or quoted for spaces. */
const QUOTED_INNER = String.raw`((?:[^"\\]|\\.)*)`;
const UNQUOTED = String.raw`[a-zA-Z0-9_./\-\p{L}\p{N}]+`;
const FILE_RE = new RegExp(`@file:(?:"${QUOTED_INNER}"|(${UNQUOTED}))`, "gu");
const MAX_PATH_BYTES = 5e4;
const MAX_DIR_ENTRIES = 120;
function pathFromFileWireExec(m) {
	const unquoted = m[2];
	if (unquoted != null && unquoted !== "") return unquoted;
	const q = m[1];
	return q != null ? q.replace(/\\(.)/g, (_, ch) => ch) : "";
}
function escapeXmlAttr(s) {
	return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
}
function collectFileTokens(text) {
	const out = [];
	const r = new RegExp(FILE_RE.source, "gu");
	let m;
	while ((m = r.exec(text)) !== null) {
		const v = pathFromFileWireExec(m).trim();
		if (v) out.push({
			start: m.index,
			end: m.index + m[0].length,
			value: v
		});
	}
	return out;
}
async function workspacePathSnippet(rel, workspaceRoot) {
	const abs = resolveWorkspaceSafePath(workspaceRoot, rel);
	if (!abs) return "[invalid path]";
	try {
		const st = await stat(abs);
		if (st.isDirectory()) return `[directory, first ${MAX_DIR_ENTRIES} non-hidden entries]\n${(await readdir(abs, { withFileTypes: true })).filter((e) => !e.name.startsWith(".")).slice(0, MAX_DIR_ENTRIES).map((e) => `${e.isDirectory() ? "(dir)" : "(file)"} ${e.name}`).join("\n")}`;
		if (st.isFile()) {
			const s = (await readFile(abs)).toString("utf8");
			return s.length > MAX_PATH_BYTES ? `${s.slice(0, MAX_PATH_BYTES)}\n\n[... truncated ...]` : s;
		}
		return "[not a file or directory]";
	} catch {
		return "[unreadable or missing]";
	}
}
/**
* Prepend `<file path="…">` blocks for `@file:` references before the raw user text.
*/
async function expandAtFileMentionsInPlainText(text, workspaceRoot) {
	if (!text.includes("@file:")) return text;
	const tokens = collectFileTokens(text);
	if (tokens.length === 0) return text;
	const seen = /* @__PURE__ */ new Set();
	const blocks = [];
	for (const t of tokens) {
		if (seen.has(t.value)) continue;
		seen.add(t.value);
		const body = await workspacePathSnippet(t.value, workspaceRoot);
		blocks.push(`<file path="${escapeXmlAttr(t.value)}">\n${body}\n</file>`);
	}
	if (blocks.length === 0) return text;
	return `${blocks.join("\n\n")}\n\n${text}`;
}
//#endregion
export { expandAtFileMentionsInPlainText };

//# sourceMappingURL=expand-at-file-mentions.js.map