import { V as isSubagentSessionKey } from "./agent-scope-u760_XKU.js";
import path from "node:path";

//#region ../../src/memory/access-policy.ts
function resolveMemoryActorType(sessionKey) {
	if (!sessionKey) return "other";
	return isSubagentSessionKey(sessionKey) ? "subagent" : "main";
}
function normalizeAllowedCollections(values) {
	if (!Array.isArray(values)) return;
	const normalized = Array.from(new Set(values.map((value) => typeof value === "string" ? value.trim().toLowerCase() : "").filter(Boolean)));
	return normalized.length > 0 ? normalized : void 0;
}
function isCollectionAllowed(collection, allowedCollections) {
	if (!allowedCollections || allowedCollections.length === 0) return true;
	return allowedCollections.includes(collection.trim().toLowerCase());
}
function normalizeRuntimeAttachmentPolicy(value) {
	if (typeof value !== "string") return;
	const normalized = value.trim().toLowerCase();
	if (normalized === "mounted-only" || normalized === "attach-on-demand") return normalized;
}
function resolveMemoryCapabilityTier(accessScope) {
	if (accessScope?.actorType === "subagent") {
		if (normalizeRuntimeAttachmentPolicy(accessScope.runtimeAttachmentPolicy) === "attach-on-demand") return "search-only";
	}
	return "search-and-read";
}
function isDirectMemoryReadAllowed(accessScope) {
	return resolveMemoryCapabilityTier(accessScope) === "search-and-read";
}
function resolveCollectionsForRelPath(params) {
	const relPath = params.relPath?.trim();
	if (!relPath) return;
	const explicitQmdCollection = extractQmdCollectionFromRelPath(relPath);
	if (explicitQmdCollection) return [explicitQmdCollection];
	const collections = params.collections ?? [];
	if (collections.length === 0) return;
	const absPath = path.isAbsolute(relPath) ? path.resolve(relPath) : path.resolve(params.workspaceDir, relPath);
	const matches = collections.filter((collection) => matchesCollection(absPath, collection)).map((collection) => collection.name.trim().toLowerCase()).filter(Boolean);
	return matches.length > 0 ? Array.from(new Set(matches)) : void 0;
}
function isMemoryPathAllowed(params) {
	const allowedCollections = normalizeAllowedCollections(params.allowedCollections);
	if (!allowedCollections || allowedCollections.length === 0) return true;
	const matchedCollections = resolveCollectionsForRelPath(params);
	if (!matchedCollections || matchedCollections.length === 0) return false;
	return matchedCollections.some((collection) => isCollectionAllowed(collection, allowedCollections));
}
function extractQmdCollectionFromRelPath(relPath) {
	if (!relPath.startsWith("qmd/")) return;
	const [, collection] = relPath.split("/");
	return collection?.trim().toLowerCase() || void 0;
}
function matchesCollection(absPath, collection) {
	const root = path.resolve(collection.path);
	if (!isWithinRoot(root, absPath)) return false;
	const globTarget = path.relative(root, absPath).replace(/\\/g, "/") || path.basename(absPath);
	return path.matchesGlob(globTarget, collection.pattern);
}
function isWithinRoot(root, candidate) {
	if (candidate === root) return true;
	const normalizedRoot = root.endsWith(path.sep) ? root : `${root}${path.sep}`;
	return (candidate.endsWith(path.sep) ? candidate : `${candidate}${path.sep}`).startsWith(normalizedRoot);
}

//#endregion
export { resolveMemoryActorType as a, normalizeRuntimeAttachmentPolicy as i, isMemoryPathAllowed as n, resolveMemoryCapabilityTier as o, normalizeAllowedCollections as r, isDirectMemoryReadAllowed as t };