"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPageSearchParams = exports.EXTERNAL_LINK_PROPS = void 0;
exports.getLinkProps = getLinkProps;
exports.isAbsoluteUrl = isAbsoluteUrl;
exports.isLinkExternal = isLinkExternal;
exports.getNonLocaleHostName = getNonLocaleHostName;
exports.setUrlTld = setUrlTld;
exports.getAbsolutePath = getAbsolutePath;
const url_1 = require("url");
const EXAMPLE_URL = 'https://example.org';
exports.EXTERNAL_LINK_PROPS = { target: '_blank', rel: 'noopener noreferrer' };
function getLinkProps(url, hostname, target) {
    let linkProps = { target };
    if (isLinkExternal(url, hostname)) {
        linkProps = { ...linkProps, ...exports.EXTERNAL_LINK_PROPS };
    }
    return linkProps;
}
function isAbsoluteUrl(url) {
    // Using example URL as base for relative links
    const urlObj = new URL(url, EXAMPLE_URL);
    return (
    // Compare url origin with example and check that original url was not example one
    urlObj.origin !== EXAMPLE_URL || (typeof url === 'string' && url.startsWith(EXAMPLE_URL)));
}
function isLinkExternal(url, routerHostname) {
    return (isAbsoluteUrl(url) &&
        getNonLocaleHostName(new URL(url).hostname) !== getNonLocaleHostName(routerHostname ?? ''));
}
function getNonLocaleHostName(hostname) {
    return hostname.replace(/\.(ru|com)$/, '');
}
function setUrlTld(url, tld = 'ru') {
    return typeof url === 'string' ? url?.replace(/\${tld}/g, tld) : url;
}
const getPageSearchParams = (query = {}) => {
    const searchParams = new URLSearchParams();
    Object.entries(query).forEach(([key, value]) => {
        searchParams.set(key, String(value));
    });
    return searchParams;
};
exports.getPageSearchParams = getPageSearchParams;
function getAbsolutePath(hostname, pathname, url) {
    if (!pathname) {
        return url ?? '';
    }
    const parsed = (0, url_1.parse)(url || '');
    return (0, url_1.format)({
        ...parsed,
        protocol: parsed.protocol || 'https',
        hostname: parsed.hostname || hostname,
        pathname: parsed.pathname || pathname,
    });
}
//# sourceMappingURL=url.js.map
