"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isOnline = void 0;
const tslib_1 = require("tslib");
/**
 * Checks if the device has an active internet connection by making a fetch request to
 * 'https://ipv4.icanhazip.com/' and resolving the result.
 * The request will time out if the specified timeout duration is exceeded.
 *
 * @param {Object} options - The options object for the function.
 * @param {number} options.timeout - The timeout duration in milliseconds. If the request
 *                                    takes longer than this, it will be aborted.
 *
 * @returns {Promise<boolean>} A Promise that resolves to `true` if the device is online
 *                             (i.e., the fetch request is successful), or `false` if the device
 *                             is offline or the request fails or times out.
 */
function isOnline(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        return new Promise((resolve) => {
            // Use AbortController to set a timeout for the fetch request
            const controller = new AbortController();
            const timeoutId = setTimeout(() => {
                controller.abort(); // Abort the request if the timeout is exceeded
            }, options.timeout);
            fetch('https://ipv4.icanhazip.com/', {
                method: 'GET',
                signal: controller.signal,
            })
                .then((response) => {
                clearTimeout(timeoutId); // Clear the timeout once the request is done
                resolve(response.ok);
            })
                .catch(() => {
                clearTimeout(timeoutId);
                resolve(false);
            });
        });
    });
}
exports.isOnline = isOnline;
//# sourceMappingURL=is-online.js.map