import { URL } from 'url';
import { HostRule } from '../../../models/HostRule.js';
/**
 * Updates the `uri` property from current request URL
 */
export function readUrl(value, options = {}) {
    const { hosts = [] } = options;
    const instances = hosts.map((i) => new HostRule(i));
    value = HostRule.applyHosts(value, instances);
    try {
        return new URL(value);
    }
    catch {
        throw new Error(`Unable to parse the URL: ${value}`);
    }
}
/**
 * Get host header for the request
 */
export function getHostHeader(url, options = {}) {
    const { hosts = [] } = options;
    const instances = hosts.map((i) => new HostRule(i));
    const processedUrl = HostRule.applyHosts(url, instances);
    try {
        const parsed = new URL(processedUrl);
        return parsed.host;
    }
    catch {
        return undefined;
    }
}
/**
 * Get redirect location
 */
export function getRedirectLocation(location, currentUrl) {
    try {
        const url = new URL(location, currentUrl);
        return url.toString();
    }
    catch {
        return location;
    }
}
/**
 * Check if redirect is a loop
 */
export function isRedirectLoop(location, redirects) {
    return redirects.some((redirect) => redirect.url === location);
}
//# sourceMappingURL=UrlProcessor.js.map