import { BrowserSessionDataSchema } from './schema.js';
/**
 * Parse browser session credential (base64 JSON) to contextId + cookies
 */
export function parseBrowserSessionData(credential) {
    if (!credential)
        return null;
    try {
        const jsonString = Buffer.from(credential, 'base64').toString('utf-8');
        const parsed = JSON.parse(jsonString);
        const validated = BrowserSessionDataSchema.safeParse(parsed);
        if (validated.success) {
            const cookies = validated.data.cookies.map((c) => ({
                name: c.name,
                value: c.value,
                domain: c.domain,
                path: c.path || '/',
                expires: c.expires ?? -1,
                httpOnly: c.httpOnly ?? false,
                secure: c.secure ?? false,
            }));
            return {
                contextId: validated.data.contextId,
                cookies,
            };
        }
        return null;
    }
    catch {
        return null;
    }
}
/**
 * Build proxy configuration for BrowserBase from ProxyChoice
 */
export function buildProxyConfig(proxy) {
    if (!proxy || proxy.type === 'none') {
        return {};
    }
    if (proxy.type === 'browserbase') {
        return { proxies: true };
    }
    if (proxy.type === 'custom') {
        return {
            proxies: [
                {
                    type: 'external',
                    server: proxy.proxy.server,
                    ...(proxy.proxy.username && { username: proxy.proxy.username }),
                    ...(proxy.proxy.password && { password: proxy.proxy.password }),
                },
            ],
        };
    }
    return {};
}
//# sourceMappingURL=utils.js.map