"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyCustomDappUrl = exports.isSameDomain = exports.getProtocol = exports.getDomain = void 0;
/**
 * Extracts the domain from a URL string
 */
function getDomain(url) {
    try {
        const urlObj = new URL(url);
        return urlObj.hostname;
    }
    catch {
        return null;
    }
}
exports.getDomain = getDomain;
/**
 * Extracts the protocol from a URL string
 */
function getProtocol(url) {
    try {
        const urlObj = new URL(url);
        return urlObj.protocol;
    }
    catch {
        return null;
    }
}
exports.getProtocol = getProtocol;
/**
 * Checks if two URLs are on the same domain and protocol
 */
function isSameDomain(url1, url2) {
    if (!url1 || !url2)
        return false;
    const domain1 = getDomain(url1);
    const domain2 = getDomain(url2);
    const protocol1 = getProtocol(url1);
    const protocol2 = getProtocol(url2);
    return (domain1 !== null &&
        domain2 !== null &&
        protocol1 !== null &&
        protocol2 !== null &&
        domain1 === domain2 &&
        protocol1 === protocol2);
}
exports.isSameDomain = isSameDomain;
/**
 * Applies a custom dapp URL to a manifest if it's on the same domain as the original URL
 */
function applyCustomDappUrl(manifest, customDappUrl) {
    if (!customDappUrl || !manifest) {
        return manifest;
    }
    // Handle manifest with params.dappUrl
    if (manifest.params &&
        "dappUrl" in manifest.params &&
        isSameDomain(customDappUrl, manifest.params.dappUrl)) {
        return {
            ...manifest,
            params: {
                ...manifest.params,
                dappUrl: customDappUrl,
            },
        };
    }
    // Handle manifest.dapp case
    if (manifest.dapp && isSameDomain(customDappUrl, String(manifest.url))) {
        return {
            ...manifest,
            url: customDappUrl,
        };
    }
    return manifest;
}
exports.applyCustomDappUrl = applyCustomDappUrl;
//# sourceMappingURL=manifestDomainUtils.js.map