export const resolveSigningPrefix = (baseUrl) => {
    try {
        const parsed = new URL(baseUrl);
        const pathname = parsed.pathname;
        const path = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;
        if (path === '' || path === '/')
            return undefined;
        return path;
    }
    catch {
        return undefined;
    }
};
export const buildSigningUrl = (requestUrl, baseUrl) => {
    const prefix = resolveSigningPrefix(baseUrl);
    if (typeof prefix !== 'string' || prefix.trim() === '')
        return requestUrl;
    if (requestUrl.pathname === prefix || requestUrl.pathname.startsWith(`${prefix}/`)) {
        const signingUrl = new URL(requestUrl.toString());
        const stripped = requestUrl.pathname.slice(prefix.length);
        signingUrl.pathname = stripped.startsWith('/') ? stripped : `/${stripped}`;
        return signingUrl;
    }
    return requestUrl;
};
