import { getSession } from "../lib/neo4j.js";
import { notTrashed } from "../../../../../lib/graph-trash/dist/index.js";
import { buildScopeAndSliceClause } from "../../../../../lib/graph-search/dist/index.js";
const DEFAULT_LIMIT = 25;
// Stripped from returned properties: the embedding is a large float vector that
// only bloats agent context, and accountId is the caller's own scope (mirrors
// memory-search's formatResult hygiene).
const STRIPPED_PROPERTY_KEYS = new Set(["embedding", "accountId"]);
export async function memoryLookupByName(params) {
    const { name, accountId, labels, allowedScopes, sliceToken, agentSlug } = params;
    const limit = params.limit ?? DEFAULT_LIMIT;
    if (!name.trim())
        throw new Error("name is required");
    const scope = buildScopeAndSliceClause(allowedScopes, sliceToken, "n");
    const labelClause = labels && labels.length > 0 ? "AND any(l IN labels(n) WHERE l IN $labels)" : "";
    const agentClause = agentSlug
        ? "AND n.agents IS NOT NULL AND $agentSlug IN n.agents"
        : "";
    const cypher = `MATCH (n)
     WHERE n.accountId = $accountId
       AND toLower(n.name) = toLower($name)
       AND ${notTrashed("n")}
       ${labelClause}
       ${scope.clause}
       ${agentClause}
     RETURN elementId(n) AS nodeId, labels(n) AS labels, properties(n) AS properties
     LIMIT toInteger($limit)`;
    const session = getSession();
    try {
        const result = await session.run(cypher, {
            accountId,
            name,
            limit,
            ...(labels && labels.length > 0 ? { labels } : {}),
            ...scope.params,
            ...(agentSlug ? { agentSlug } : {}),
        });
        return result.records.map((r) => {
            const rawProps = r.get("properties");
            const properties = {};
            for (const [k, v] of Object.entries(rawProps)) {
                if (!STRIPPED_PROPERTY_KEYS.has(k))
                    properties[k] = v;
            }
            return {
                nodeId: r.get("nodeId"),
                labels: r.get("labels"),
                properties,
            };
        });
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=memory-lookup-by-name.js.map