"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Balances = void 0;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var utils_1 = require("../utils");
function Balances(balances) {
    if (balances === void 0) { balances = {}; }
    function create(id, amount) {
        if (amount === void 0) { amount = "0"; }
        (0, assert_1.default)(!has(id), "balance already exists");
        return set(id, amount);
    }
    function has(id) {
        return (0, utils_1.isDefined)(balances[id]);
    }
    function set(id, amount) {
        balances[id] = amount;
        return amount;
    }
    function add(id, amount) {
        return set(id, utils_1.BigNumber.from(amount).add(getOrCreate(id)).toString());
    }
    function sub(id, amount) {
        return set(id, utils_1.BigNumber.from(getOrCreate(id)).sub(amount).toString());
    }
    function get(id) {
        (0, assert_1.default)(has(id), "balance does not exist");
        return balances[id];
    }
    function getOrCreate(id) {
        if (has(id))
            return get(id);
        return create(id);
    }
    return { create: create, add: add, sub: sub, get: get, balances: balances, set: set, has: has, getOrCreate: getOrCreate };
}
exports.Balances = Balances;
//# sourceMappingURL=utils.js.map