import { getSession } from "../lib/neo4j.js";
import { resolvePersonMatch } from "../lib/resolve-person.js";
export async function contactUpdate(params) {
    const { nodeId, email, telephone, updates, accountId } = params;
    const { matchClause, queryParams } = resolvePersonMatch({ nodeId, email, telephone }, accountId);
    const session = getSession();
    try {
        // Build SET clauses from updates (excluding protected fields)
        const protectedFields = new Set(["accountId", "email", "createdOn", "embedding"]);
        const setClauses = [];
        let paramIndex = 0;
        for (const [key, value] of Object.entries(updates)) {
            if (protectedFields.has(key))
                continue;
            const paramName = `val_${paramIndex}`;
            setClauses.push(`p.${key} = $${paramName}`);
            queryParams[paramName] = value;
            paramIndex++;
        }
        if (setClauses.length === 0) {
            throw new Error("No valid fields to update");
        }
        setClauses.push(`p.updatedAt = $updatedAt`);
        queryParams.updatedAt = new Date().toISOString();
        const query = `
      ${matchClause}
      SET ${setClauses.join(", ")}
      RETURN elementId(p) AS nodeId
    `;
        const result = await session.run(query, queryParams);
        if (result.records.length === 0) {
            throw new Error("Contact not found");
        }
        return {
            nodeId: result.records[0].get("nodeId"),
            updated: true,
        };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=contact-update.js.map