import { init_paths_state, resolveStateDir } from "../config/paths-state.js";
import { init_agent_scope, resolveAgentProfileDir } from "../agent/agent-scope.js";
import { init_paths, resolveConfigPath, resolveSessionsDir, resolveSkillsDir } from "../config/paths.js";
import { extractProfileAgentId } from "../config/agent-profile.js";
import { isBareProfileMarkdownFileName, resolveProfileMarkdownPathIfBareName } from "../agent/tools/tool-paths.js";
import { isPathUnderWorkspace, resolveWorkspaceSafePath } from "./workspace-editor-path.js";
import { stat } from "node:fs/promises";
import { basename, isAbsolute, resolve } from "node:path";
//#region src/gateway/file-path-classifier.ts
init_agent_scope();
init_paths();
init_paths_state();
function buildFilePathClassifierContext(cfg, sessionKeyRaw) {
	const agentId = extractProfileAgentId(sessionKeyRaw, cfg);
	const stateDir = resolveStateDir();
	return {
		workspaceRoot: "",
		profileMarkdownRoot: resolveAgentProfileDir(cfg, agentId),
		stateDir,
		skillsDir: resolveSkillsDir(),
		configFilePath: resolveConfigPath(),
		agentsHomeDir: resolve(stateDir, "agents"),
		sessionsDir: resolveSessionsDir(cfg, agentId),
		agentId
	};
}
function looksLikeHostAbsolutePath(pathRaw) {
	const p = pathRaw.trim();
	if (!p) return false;
	if (/^[a-z][a-z0-9+.-]*:/i.test(p) && !/^[A-Za-z]:[\\/]/.test(p)) return false;
	return isAbsolute(p) || /^[A-Za-z]:[\\/]/.test(p) || p.startsWith("\\\\");
}
function fileRefSessionKeysMatch(registered, query) {
	return (registered ?? "").trim() === (query ?? "").trim();
}
function classifyFileLocation(absPath, ctx) {
	const p = resolve(absPath);
	if (isPathUnderWorkspace(resolve(ctx.workspaceRoot), p)) return { scope: "workspace" };
	if (ctx.profileMarkdownRoot) {
		if (isPathUnderWorkspace(resolve(ctx.profileMarkdownRoot), p)) return {
			scope: "agent-profile",
			locationKind: "agent-profile",
			manageRoute: "/settings/agents"
		};
	}
	if (isPathUnderWorkspace(resolve(ctx.skillsDir), p)) return {
		scope: "external",
		locationKind: "xopc-skills",
		manageRoute: "/settings/skills"
	};
	if (isPathUnderWorkspace(resolve(ctx.sessionsDir), p)) return {
		scope: "session-artifact",
		locationKind: "xopc-sessions",
		manageRoute: "/settings/sessions"
	};
	if (isPathUnderWorkspace(resolve(ctx.agentsHomeDir), p)) return {
		scope: "external",
		locationKind: "xopc-agents",
		manageRoute: "/settings/agents"
	};
	const stateRoot = resolve(ctx.stateDir);
	if (p === resolve(ctx.configFilePath) || isPathUnderWorkspace(stateRoot, p)) return {
		scope: "external",
		locationKind: "xopc-config",
		manageRoute: "/settings/gateway"
	};
	return {
		scope: "external",
		locationKind: "host"
	};
}
/**
* Resolve user path to an absolute candidate (workspace, absolute host, or profile fallback).
*/
async function resolveFileReferenceCandidate(rawPath, workspaceRoot, ctx) {
	const displayRoot = workspaceRoot;
	ctx = {
		...ctx,
		workspaceRoot: displayRoot
	};
	if (looksLikeHostAbsolutePath(rawPath)) return {
		candidate: resolve(rawPath),
		invalid: false
	};
	const wsPath = resolveWorkspaceSafePath(workspaceRoot, rawPath);
	if (!wsPath) return {
		candidate: null,
		invalid: true
	};
	try {
		await stat(wsPath);
		return {
			candidate: wsPath,
			invalid: false
		};
	} catch (e) {
		if (e?.code === "ENOENT" && ctx.profileMarkdownRoot && isBareProfileMarkdownFileName(rawPath)) return {
			candidate: resolveProfileMarkdownPathIfBareName(rawPath, ctx.profileMarkdownRoot),
			invalid: false
		};
		return {
			candidate: wsPath,
			invalid: false
		};
	}
}
function displayNameForPath(rawPath) {
	return basename(rawPath.replace(/\\/g, "/")) || rawPath;
}
//#endregion
export { buildFilePathClassifierContext, classifyFileLocation, displayNameForPath, fileRefSessionKeysMatch, looksLikeHostAbsolutePath, resolveFileReferenceCandidate };

//# sourceMappingURL=file-path-classifier.js.map