import { Headers } from '../../lib/headers/Headers.js';
import { CookieParser } from '../../cookies/CookieParser.js';
export class RequestCookiesProcessor {
    /**
     * Applies cookies to the request object.
     *
     * Note, this mutates the request object.
     *
     * @param request The request to which apply the cookies.
     * @param cookies The cookies to apply
     */
    static request(request, cookies) {
        if (!Array.isArray(cookies) || !cookies.length) {
            return;
        }
        const header = cookies.map((c) => `${c.name}=${c.value}`).join('; ');
        const headers = new Headers(request.headers);
        headers.append('cookie', header);
        request.headers = headers.toString();
    }
    /**
     * Computes a list of cookies in the response. This includes the redirect cookies and the final response.
     *
     * @param log The HTTP log.
     * @returns A map where keys are the URL of the request and the values are cookies.
     *          It may be empty map when no cookies.
     */
    static response(log) {
        if (!log.response || !log.request) {
            return;
        }
        const typedError = log.response;
        if (typedError.error) {
            return;
        }
        const typedResponse = log.response;
        return this.extract(typedResponse, log.request.url, log.redirects);
    }
    static extract(response, url, redirects) {
        const result = {};
        let redirectUrl = url;
        if (redirects && redirects.length) {
            for (const redirect of redirects) {
                const headers = new Headers(redirect.response.headers);
                const sc = headers.get('set-cookie');
                if (sc) {
                    const cookies = CookieParser.parse(redirectUrl, sc);
                    if (cookies.length) {
                        result[redirectUrl] = cookies;
                    }
                }
                redirectUrl = redirect.url;
            }
        }
        const headers = new Headers(response.headers);
        const sc = headers.get('set-cookie');
        if (sc) {
            const cookies = CookieParser.parse(redirectUrl, sc);
            if (cookies.length) {
                result[redirectUrl] = cookies;
            }
        }
        return result;
    }
}
//# sourceMappingURL=RequestCookiesProcessor.js.map