"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getThrottleIntervalMs = exports.getConcurrentRequestsLimit = exports.applyHostRules = exports.findMatchingRules = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const constants_1 = require("../../constants");
const logger_1 = require("../../logger");
const proxy_1 = require("../../proxy");
const hostRules = tslib_1.__importStar(require("../host-rules"));
const dns_1 = require("./dns");
const keepalive_1 = require("./keepalive");
function findMatchingRules(options, url) {
    const { hostType } = options;
    let res = hostRules.find({ hostType, url });
    if (is_1.default.nonEmptyString(res.token) ||
        is_1.default.nonEmptyString(res.username) ||
        is_1.default.nonEmptyString(res.password)) {
        // do not fallback if we already have auth infos
        return res;
    }
    // Fallback to `github` hostType
    if (hostType &&
        constants_1.GITHUB_API_USING_HOST_TYPES.includes(hostType) &&
        hostType !== 'github') {
        res = {
            ...hostRules.find({
                hostType: 'github',
                url,
            }),
            ...res,
        };
    }
    // Fallback to `gitlab` hostType
    if (hostType &&
        constants_1.GITLAB_API_USING_HOST_TYPES.includes(hostType) &&
        hostType !== 'gitlab') {
        res = {
            ...hostRules.find({
                hostType: 'gitlab',
                url,
            }),
            ...res,
        };
    }
    // Fallback to `bitbucket` hostType
    if (hostType &&
        constants_1.BITBUCKET_API_USING_HOST_TYPES.includes(hostType) &&
        hostType !== 'bitbucket') {
        res = {
            ...hostRules.find({
                hostType: 'bitbucket',
                url,
            }),
            ...res,
        };
    }
    return res;
}
exports.findMatchingRules = findMatchingRules;
// Apply host rules to requests
function applyHostRules(url, inOptions) {
    const options = { ...inOptions };
    const foundRules = findMatchingRules(options, url);
    const { username, password, token, enabled, authType } = foundRules;
    if (options.noAuth) {
        logger_1.logger.trace({ url }, `Authorization disabled`);
    }
    else if (is_1.default.nonEmptyString(options.headers?.authorization) ||
        is_1.default.nonEmptyString(options.password) ||
        is_1.default.nonEmptyString(options.token)) {
        logger_1.logger.trace({ url }, `Authorization already set`);
    }
    else if (password !== undefined) {
        logger_1.logger.trace({ url }, `Applying Basic authentication`);
        options.username = username;
        options.password = password;
    }
    else if (token) {
        logger_1.logger.trace({ url }, `Applying Bearer authentication`);
        options.token = token;
        options.context = { ...options.context, authType };
    }
    else if (enabled === false) {
        options.enabled = false;
    }
    // Apply optional params
    if (foundRules.abortOnError) {
        options.abortOnError = foundRules.abortOnError;
    }
    if (foundRules.abortIgnoreStatusCodes) {
        options.abortIgnoreStatusCodes = foundRules.abortIgnoreStatusCodes;
    }
    if (foundRules.timeout) {
        options.timeout = foundRules.timeout;
    }
    if (foundRules.dnsCache) {
        options.lookup = dns_1.dnsLookup;
    }
    if (foundRules.keepalive) {
        options.agent = keepalive_1.keepaliveAgents;
    }
    if (!(0, proxy_1.hasProxy)() && foundRules.enableHttp2 === true) {
        options.http2 = true;
    }
    return options;
}
exports.applyHostRules = applyHostRules;
function getConcurrentRequestsLimit(url) {
    const { concurrentRequestLimit } = hostRules.find({ url });
    return is_1.default.number(concurrentRequestLimit) && concurrentRequestLimit > 0
        ? concurrentRequestLimit
        : null;
}
exports.getConcurrentRequestsLimit = getConcurrentRequestsLimit;
function getThrottleIntervalMs(url) {
    const { maxRequestsPerSecond } = hostRules.find({ url });
    return is_1.default.number(maxRequestsPerSecond) && maxRequestsPerSecond > 0
        ? Math.ceil(1000 / maxRequestsPerSecond)
        : null;
}
exports.getThrottleIntervalMs = getThrottleIntervalMs;
//# sourceMappingURL=host-rules.js.map