/**
 * Shared Person resolution helper.
 *
 * Builds a Cypher MATCH clause and query params to find a Person node
 * by one of three identifiers: nodeId (elementId), email, or telephone.
 * Priority: nodeId > email > telephone.
 *
 * Used by: contact-delete, contact-update, contact-export, contact-erase.
 * NOT used by: contact-create (dedup OR logic), contact-lookup (name search).
 *
 * trashed Person nodes are excluded by default. `contact-erase`
 * passes `includeTrashed: true` because GDPR Article 17 must cascade-delete
 * trashed persons too (they were marked but not yet emptied). Trashed
 * persons also have nulled email/telephone (UNIQUE_KEYS_BY_LABEL snapshot),
 * so identifier-by-email/telephone already fails — the filter is primarily
 * relevant to the nodeId path and guards against the legacy deletedAt
 * soft-delete state surfaced by `notTrashed()`.
 */
import { notTrashed } from "../../../../../lib/graph-trash/dist/index.js";
export function resolvePersonMatch(identifier, accountId, options = {}) {
    const { nodeId, email, telephone } = identifier;
    const { includeTrashed = false } = options;
    if (!nodeId && !email && !telephone) {
        throw new Error("Either nodeId, email, or telephone must be provided");
    }
    const queryParams = { accountId };
    const trashSuffix = includeTrashed ? "" : ` AND ${notTrashed("p")}`;
    let matchClause;
    if (nodeId) {
        if (nodeId.includes(":")) {
            matchClause = `MATCH (p:Person) WHERE elementId(p) = $nodeId AND p.accountId = $accountId${trashSuffix}`;
            queryParams.nodeId = nodeId;
        }
        else {
            matchClause = `MATCH (p:Person) WHERE id(p) = $nodeId AND p.accountId = $accountId${trashSuffix}`;
            queryParams.nodeId = parseInt(nodeId, 10);
        }
    }
    else if (email) {
        matchClause = includeTrashed
            ? `MATCH (p:Person {email: $email, accountId: $accountId})`
            : `MATCH (p:Person {email: $email, accountId: $accountId}) WHERE ${notTrashed("p")}`;
        queryParams.email = email.toLowerCase();
    }
    else {
        matchClause = includeTrashed
            ? `MATCH (p:Person {telephone: $telephone, accountId: $accountId})`
            : `MATCH (p:Person {telephone: $telephone, accountId: $accountId}) WHERE ${notTrashed("p")}`;
        queryParams.telephone = telephone.trim();
    }
    return { matchClause, queryParams };
}
//# sourceMappingURL=resolve-person.js.map