import assert from "assert";
import { BigNumber, isDefined } from "../utils";
/**
 * Utility for maintaining records of a token balance.
 */
export function Balances(balances = {}) {
    function create(id, amount = "0") {
        assert(!has(id), "balance already exists");
        return set(id, amount);
    }
    function has(id) {
        return isDefined(balances[id]);
    }
    function set(id, amount) {
        balances[id] = amount;
        return amount;
    }
    function add(id, amount) {
        return set(id, BigNumber.from(amount).add(getOrCreate(id)).toString());
    }
    function sub(id, amount) {
        return set(id, BigNumber.from(getOrCreate(id)).sub(amount).toString());
    }
    function get(id) {
        assert(has(id), "balance does not exist");
        return balances[id];
    }
    function getOrCreate(id) {
        if (has(id))
            return get(id);
        return create(id);
    }
    return { create, add, sub, get, balances, set, has, getOrCreate };
}
//# sourceMappingURL=utils.js.map