/**
 * 会话上下文：合并请求 cookies 与磁盘 session，供各 API 统一使用
 */
const {
    loadSession,
    sessionCookiesForPage,
    mergeCookies,
    touchSession,
} = require('./session-store');
const { SESSION_TTL_MS } = require('./constants');

/**
 * @param {{ sessionKey?: string }} antiBot
 * @param {string} pageUrl
 * @param {object[]} [requestCookies]
 */
function resolveSessionContext(antiBot, pageUrl, requestCookies = []) {
    const sessionKey = antiBot?.sessionKey || '';
    const storedSession = sessionKey ? loadSession(sessionKey) : null;
    const sessionCookies = storedSession ? sessionCookiesForPage(storedSession, pageUrl) : [];
    const mergedCookies = mergeCookies(requestCookies, sessionCookies);
    return { sessionKey, storedSession, mergedCookies };
}

/**
 * 成功过盾后滑动续期 session TTL
 * @param {string} sessionKey
 */
function touchSessionIfPresent(sessionKey) {
    if (sessionKey) {
        touchSession(sessionKey, SESSION_TTL_MS);
    }
}

module.exports = {
    resolveSessionContext,
    touchSessionIfPresent,
};
