/**
 * Note Selection
 *
 * Greedy algorithm to select unspent notes for a target amount.
 * Port of select_notes_for_amount() from operations/common.rs.
 */
export function selectNotesForAmount(notes, targetAmount) {
    const sorted = [...notes]
        .filter((n) => BigInt(n.value) > 0n)
        .sort((a, b) => {
        const aVal = BigInt(a.value);
        const bVal = BigInt(b.value);
        if (bVal > aVal)
            return 1;
        if (bVal < aVal)
            return -1;
        return 0;
    });
    const selected = [];
    let totalValue = 0n;
    for (const note of sorted) {
        if (totalValue >= targetAmount)
            break;
        selected.push(note);
        totalValue += BigInt(note.value);
    }
    if (totalValue < targetAmount) {
        throw new Error(`Insufficient notes: have ${totalValue.toString()}, need ${targetAmount.toString()}`);
    }
    return {
        selected,
        totalValue,
        changeValue: totalValue - targetAmount,
    };
}
//# sourceMappingURL=note-selection.js.map