import { getSession } from "../lib/neo4j.js";
export async function memoryEdge(params) {
    const { action, fromId, toId, accountId } = params;
    const rawType = params.relationshipType.toUpperCase().replace(/[`\n\r]/g, "");
    if (rawType.startsWith("_")) {
        process.stderr.write(`[memory-edge] action=${action} from=${fromId.slice(0, 8)} to=${toId.slice(0, 8)} type=${rawType} accountId=${accountId?.slice(0, 8) ?? "none"} result=rejected reason=reserved-type\n`);
        return { error: "reserved-type", type: rawType };
    }
    const session = getSession();
    try {
        const fromCheck = await session.run(`MATCH (n) WHERE elementId(n) = $nodeId AND n.accountId = $accountId RETURN elementId(n) AS id`, { nodeId: fromId, accountId });
        if (fromCheck.records.length === 0) {
            process.stderr.write(`[memory-edge] action=${action} from=${fromId.slice(0, 8)} to=${toId.slice(0, 8)} type=${rawType} accountId=${accountId?.slice(0, 8) ?? "none"} result=rejected reason=foreign-node elementId=${fromId}\n`);
            return { error: "foreign-node", elementId: fromId };
        }
        const toCheck = await session.run(`MATCH (n) WHERE elementId(n) = $nodeId AND n.accountId = $accountId RETURN elementId(n) AS id`, { nodeId: toId, accountId });
        if (toCheck.records.length === 0) {
            process.stderr.write(`[memory-edge] action=${action} from=${fromId.slice(0, 8)} to=${toId.slice(0, 8)} type=${rawType} accountId=${accountId?.slice(0, 8) ?? "none"} result=rejected reason=foreign-node elementId=${toId}\n`);
            return { error: "foreign-node", elementId: toId };
        }
        if (action === "create") {
            const props = params.properties ?? {};
            const q = `
        MATCH (a) WHERE elementId(a) = $fromId
        MATCH (b) WHERE elementId(b) = $toId
        MERGE (a)-[r:\`${rawType}\`]->(b)
        ON CREATE SET r += $props, r.createdAt = datetime()
        RETURN r
      `;
            const result = await session.run(q, { fromId, toId, props });
            const created = result.summary.counters.updates().relationshipsCreated > 0;
            process.stderr.write(`[memory-edge] action=create from=${fromId.slice(0, 8)} to=${toId.slice(0, 8)} type=${rawType} accountId=${accountId?.slice(0, 8) ?? "none"} result=${created ? "created" : "idempotent"}\n`);
            return { created };
        }
        else {
            const q = `
        MATCH (a)-[r:\`${rawType}\`]->(b)
        WHERE elementId(a) = $fromId AND elementId(b) = $toId
        DELETE r
      `;
            const result = await session.run(q, { fromId, toId });
            const deleted = result.summary.counters.updates().relationshipsDeleted > 0;
            process.stderr.write(`[memory-edge] action=delete from=${fromId.slice(0, 8)} to=${toId.slice(0, 8)} type=${rawType} accountId=${accountId?.slice(0, 8) ?? "none"} result=${deleted ? "deleted" : "absent"}\n`);
            return { deleted };
        }
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=memory-edge.js.map