"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleep = void 0;
exports.exists = exists;
exports.Balances = Balances;
exports.loop = loop;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var utils_1 = require("../../utils");
var utils_2 = require("../../utils");
Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return utils_2.delay; } });
function exists(value) {
    return value !== null && value !== undefined;
}
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 exists(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 };
}
function loop(fn, delay) {
    var args = [];
    for (var _i = 2; _i < arguments.length; _i++) {
        args[_i - 2] = arguments[_i];
    }
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, fn.apply(void 0, args)];
                case 1:
                    _a.sent();
                    return [4, (0, utils_1.delay)(delay)];
                case 2:
                    _a.sent();
                    _a.label = 3;
                case 3:
                    if (true) return [3, 0];
                    _a.label = 4;
                case 4: return [2];
            }
        });
    });
}
//# sourceMappingURL=utils.js.map