"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCookieJar = getCookieJar;
exports.clearCookieJar = clearCookieJar;
exports.mergeCookieJar = mergeCookieJar;
const jars = new Map();
function cookieNameFromSetCookie(setCookie) {
    const pair = String(setCookie ?? "").split(";")[0]?.trim();
    if (!pair)
        return null;
    const idx = pair.indexOf("=");
    if (idx <= 0)
        return null;
    return pair.slice(0, idx).trim().toLowerCase();
}
function mergeCookieString(oldCookie, setCookies) {
    const next = new Map();
    for (const token of String(oldCookie ?? "").split(";")) {
        const pair = token.trim();
        if (!pair)
            continue;
        const idx = pair.indexOf("=");
        if (idx <= 0)
            continue;
        const name = pair.slice(0, idx).trim().toLowerCase();
        next.set(name, pair);
    }
    for (const sc of setCookies ?? []) {
        const pair = String(sc ?? "").split(";")[0]?.trim();
        if (!pair)
            continue;
        const name = cookieNameFromSetCookie(sc);
        if (!name)
            continue;
        next.set(name, pair);
    }
    return Array.from(next.values()).join("; ");
}
function getCookieJar(sessionKey) {
    return jars.get(sessionKey)?.value;
}
function clearCookieJar(sessionKey) {
    jars.delete(sessionKey);
}
function mergeCookieJar(sessionKey, setCookies) {
    if (!Array.isArray(setCookies) || setCookies.length === 0)
        return jars.get(sessionKey)?.value;
    const oldCookie = jars.get(sessionKey)?.value ?? "";
    const merged = mergeCookieString(oldCookie, setCookies);
    if (!merged)
        return undefined;
    jars.set(sessionKey, { value: merged, updatedAt: Date.now() });
    return merged;
}
