import { createHash } from "node:crypto";
// ---------------------------------------------------------------------------
// derive-keys — natural-key derivation for conversation-archive ingestion
//
// Pure functions. No I/O. The whole point is that re-imports of the same
// archive collapse to the same identity regardless of release-level drift in
// chunk indices, hash widths, or arbitrary tiebreakers.
//
//   conversationIdentity = sha256(accountId + ":" + sortedParticipantElementIds.join(",") + ":" + source)
//   messageContentHash   = sha256(dateSent + "|" + body)
//
// `conversationIdentity` is stable across re-exports — same operator + same
// participant set + same source → same identity, regardless of file bytes.
// Source is part of the hash because a 1:1 conversation with the same person
// on WhatsApp and by email is two conversations, not one — their `:Person`
// elementIds match, so without source-scoping the parent rows collide.
//
// `messageContentHash`: hashes `dateSent + body`. Per-line
// senderName is gone (the scanner is body-opaque); the body string still
// carries any `Sender:` prefix verbatim, so two senders posting different
// content at the same instant stay distinguishable. The cursor survives a
// fresh re-export as long as the source preserves the prefix in the body.
// ---------------------------------------------------------------------------
export function sha256Hex(input) {
    return createHash("sha256").update(input).digest("hex");
}
export function deriveConversationIdentity(input) {
    if (!input.accountId || !input.accountId.trim()) {
        throw new Error("deriveConversationIdentity: accountId is required");
    }
    if (input.participantElementIds.length === 0) {
        throw new Error("deriveConversationIdentity: participantElementIds must be non-empty");
    }
    if (!input.source || !input.source.trim()) {
        throw new Error("deriveConversationIdentity: source is required");
    }
    const sorted = [...input.participantElementIds].sort();
    return sha256Hex(`${input.accountId}:${sorted.join(",")}:${input.source}`);
}
export function deriveMessageContentHash(input) {
    return sha256Hex(`${input.dateSent}|${input.body}`);
}
//# sourceMappingURL=derive-keys.js.map