import neo4j from "neo4j-driver";
import { getSession } from "../lib/neo4j.js";
import { notTrashed } from "../../../../../lib/graph-trash/dist/index.js";
export async function contactLookup(params) {
    const { email, telephone, name, accountId, limit = 10 } = params;
    const session = getSession();
    try {
        let query;
        let queryParams;
        const trashFilter = notTrashed("p");
        if (email) {
            query = `
        MATCH (p:Person {accountId: $accountId})
        WHERE p.email = $email AND ${trashFilter}
        RETURN p, elementId(p) AS nodeId
        LIMIT $limit
      `;
            queryParams = { email: email.toLowerCase(), accountId, limit: neo4j.int(limit) };
        }
        else if (telephone) {
            query = `
        MATCH (p:Person {accountId: $accountId})
        WHERE p.telephone = $telephone AND ${trashFilter}
        RETURN p, elementId(p) AS nodeId
        LIMIT $limit
      `;
            queryParams = { telephone: telephone.trim(), accountId, limit: neo4j.int(limit) };
        }
        else if (name) {
            query = `
        MATCH (p:Person {accountId: $accountId})
        WHERE (toLower(p.givenName) CONTAINS toLower($name)
           OR toLower(p.familyName) CONTAINS toLower($name))
           AND ${trashFilter}
        RETURN p, elementId(p) AS nodeId
        LIMIT $limit
      `;
            queryParams = { name, accountId, limit: neo4j.int(limit) };
        }
        else {
            query = `
        MATCH (p:Person {accountId: $accountId})
        WHERE ${trashFilter}
        RETURN p, elementId(p) AS nodeId
        ORDER BY p.createdOn DESC
        LIMIT $limit
      `;
            queryParams = { accountId, limit: neo4j.int(limit) };
        }
        const result = await session.run(query, queryParams);
        return result.records.map((record) => {
            const p = record.get("p").properties;
            return {
                nodeId: record.get("nodeId"),
                givenName: p.givenName,
                familyName: p.familyName,
                email: p.email,
                telephone: p.telephone,
                jobTitle: p.jobTitle,
                source: p.source,
                status: p.status,
                createdOn: p.createdOn,
            };
        });
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=contact-lookup.js.map