"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkConnectivityValidator = void 0;
const dns = require("dns");
const yok_1 = require("../common/yok");
class NetworkConnectivityValidator {
    constructor($errors, $logger) {
        this.$errors = $errors;
        this.$logger = $logger;
    }
    async validate() {
        const isConnected = await this.isConnected();
        if (!isConnected) {
            this.$errors.fail(NetworkConnectivityValidator.NO_INTERNET_ERROR_MESSAGE);
        }
    }
    isConnected() {
        return new Promise((resolve, reject) => {
            dns.lookup(NetworkConnectivityValidator.DNS_LOOKUP_URL, (err) => {
                this.$logger.trace(`Error from dns.lookup is ${err}.`);
                if (err &&
                    err.code === NetworkConnectivityValidator.NO_INTERNET_ERROR_CODE) {
                    resolve(false);
                }
                else {
                    resolve(true);
                }
            });
        });
    }
}
exports.NetworkConnectivityValidator = NetworkConnectivityValidator;
NetworkConnectivityValidator.DNS_LOOKUP_URL = "play.nativescript.org";
NetworkConnectivityValidator.NO_INTERNET_ERROR_CODE = "ENOTFOUND";
NetworkConnectivityValidator.NO_INTERNET_ERROR_MESSAGE = "No internet connection. Check your internet settings and try again.";
yok_1.injector.register("networkConnectivityValidator", NetworkConnectivityValidator);
