import { verifyAccessToken } from '../auth/jwt.js';
export function extractTokenFromUrl(url) {
    if (!url)
        return null;
    try {
        const searchParams = new URLSearchParams(url.split('?')[1] || '');
        return searchParams.get('token') || null;
    }
    catch {
        return null;
    }
}
export function authenticateWs(url, cookieHeader, jwtSecret) {
    // Try query param first (for WebSocket connections from browser)
    const queryToken = extractTokenFromUrl(url);
    if (queryToken) {
        try {
            return verifyAccessToken(queryToken, jwtSecret);
        }
        catch {
            return null;
        }
    }
    // Try cookie
    if (cookieHeader) {
        const cookies = Object.fromEntries(cookieHeader.split(';').map((c) => {
            const [key, ...vals] = c.trim().split('=');
            return [key, vals.join('=')];
        }));
        const token = cookies['access_token'];
        if (token) {
            try {
                return verifyAccessToken(token, jwtSecret);
            }
            catch {
                return null;
            }
        }
    }
    return null;
}
//# sourceMappingURL=ws-auth.js.map