import { getSession } from "../lib/neo4j.js";
export async function contactDelete(params) {
    const { nodeId, email, telephone, accountId } = params;
    if (!nodeId && !email && !telephone) {
        throw new Error("Either nodeId, email, or telephone must be provided");
    }
    const session = getSession();
    try {
        let matchClause;
        const queryParams = { accountId };
        if (nodeId) {
            matchClause = `MATCH (p:Person) WHERE elementId(p) = $nodeId AND p.accountId = $accountId`;
            queryParams.nodeId = nodeId;
        }
        else if (email) {
            matchClause = `MATCH (p:Person {email: $email, accountId: $accountId})`;
            queryParams.email = email.toLowerCase();
        }
        else {
            matchClause = `MATCH (p:Person {telephone: $telephone, accountId: $accountId})`;
            queryParams.telephone = telephone.trim();
        }
        const result = await session.run(`${matchClause}
       WITH p, p.email AS deletedEmail, p.telephone AS deletedPhone
       DETACH DELETE p
       RETURN deletedEmail, deletedPhone`, queryParams);
        if (result.records.length === 0) {
            throw new Error("Contact not found");
        }
        const deletedEmail = result.records[0].get("deletedEmail");
        const deletedPhone = result.records[0].get("deletedPhone");
        return {
            deleted: true,
            identifier: deletedEmail ?? deletedPhone ?? "unknown",
        };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=contact-delete.js.map