'use strict';

var https = require('https');

function _interopNamespaceDefault(e) {
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () { return e[k]; }
                });
            }
        });
    }
    n.default = e;
    return Object.freeze(n);
}

var https__namespace = /*#__PURE__*/_interopNamespaceDefault(https);

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

var __assign = function() {
    __assign = Object.assign || function __assign(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};

var checkPort = function (port) {
    return !isNaN(parseFloat(port)) && Math.sign(port) === 1;
};
var getDaysBetween = function (validFrom, validTo) {
    return Math.round(Math.abs(+validFrom - +validTo) / 8.64e7);
};
var getDaysRemaining = function (validFrom, validTo) {
    var daysRemaining = getDaysBetween(validFrom, validTo);
    if (new Date(validTo).getTime() < new Date().getTime()) {
        return -daysRemaining;
    }
    return daysRemaining;
};
var DEFAULT_OPTIONS = {
    agent: new https__namespace.Agent({
        maxCachedSessions: 0
    }),
    method: "HEAD",
    port: 443,
    rejectUnauthorized: false
};
var sslChecker = function (host, options) {
    if (options === void 0) { options = {}; }
    return new Promise(function (resolve, reject) {
        options = Object.assign({}, DEFAULT_OPTIONS, options);
        if (!checkPort(options.port)) {
            reject(Error("Invalid port"));
            return;
        }
        try {
            var req_1 = https__namespace.request(__assign({ host: host }, options), function (res) {
                var _a = res.socket.getPeerCertificate(), valid_from = _a.valid_from, valid_to = _a.valid_to, subjectaltname = _a.subjectaltname;
                if (!valid_from || !valid_to || !subjectaltname) {
                    reject(new Error("No certificate"));
                    return;
                }
                var validTo = new Date(valid_to);
                var validFor = subjectaltname
                    .replace(/DNS:|IP Address:/g, "")
                    .split(", ");
                resolve({
                    daysRemaining: getDaysRemaining(new Date(), validTo),
                    valid: res.socket
                        .authorized || false,
                    validFrom: new Date(valid_from).toISOString(),
                    validTo: validTo.toISOString(),
                    validFor: validFor
                });
            });
            req_1.on("error", reject);
            req_1.on("timeout", function () {
                req_1.destroy();
                reject(new Error("Timed Out"));
            });
            req_1.end();
        }
        catch (e) {
            reject(e);
        }
    });
};

module.exports = sslChecker;
