import neo4j from "neo4j-driver";
import { getSession } from "../lib/neo4j.js";
import { notTrashed } from "../../../../../lib/graph-trash/dist/index.js";
export async function contactList(params) {
    const { status, source, since, accountId, limit = 50, } = params;
    const session = getSession();
    try {
        const conditions = ["p.accountId = $accountId", notTrashed("p")];
        const queryParams = { accountId, limit: neo4j.int(limit) };
        if (status) {
            conditions.push("p.status = $status");
            queryParams.status = status;
        }
        if (source) {
            conditions.push("p.source = $source");
            queryParams.source = source;
        }
        if (since) {
            conditions.push("p.createdOn >= $since");
            queryParams.since = since;
        }
        const whereClause = conditions.join(" AND ");
        const countResult = await session.run(`MATCH (p:Person) WHERE ${whereClause} RETURN count(p) AS total`, queryParams);
        const total = countResult.records[0].get("total").toNumber();
        const listResult = await session.run(`MATCH (p:Person)
       WHERE ${whereClause}
       RETURN p, elementId(p) AS nodeId
       ORDER BY p.createdOn DESC
       LIMIT $limit`, queryParams);
        const contacts = listResult.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,
                status: p.status,
                source: p.source,
                createdOn: p.createdOn,
            };
        });
        return { contacts, total };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=contact-list.js.map