"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setAlarm = exports.getTime = exports.sleep = void 0;
exports.inRangeOfCurrentTime = inRangeOfCurrentTime;
const network_1 = require("../../network");
const sleep = (ms) => {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
};
exports.sleep = sleep;
/** Be very careful this uses shardusGetTime() the timetamp passed in must repect this */
const getTime = (format = 'ms') => {
    let time;
    switch (format) {
        case 'ms':
            time = (0, network_1.shardusGetTime)();
            break;
        case 's':
            time = Math.floor((0, network_1.shardusGetTime)() / 1000);
            break;
        default:
            throw Error('Error: Invalid format given.');
    }
    return time;
};
exports.getTime = getTime;
/** Be very careful this uses shardusGetTime() the timetamp passed in must repect this */
const setAlarm = (callback, timestamp) => {
    const now = (0, network_1.shardusGetTime)();
    if (timestamp <= now) {
        callback();
        return;
    }
    const toWait = timestamp - now;
    setTimeout(callback, toWait);
};
exports.setAlarm = setAlarm;
function inRangeOfCurrentTime(timestamp, before, after) {
    const currentTime = (0, network_1.shardusGetTime)();
    if (timestamp - currentTime <= after) {
        if (currentTime - timestamp <= before) {
            return true;
        }
    }
    return false;
}
//# sourceMappingURL=time.js.map