"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveCaptureSessionCookieDomain = resolveCaptureSessionCookieDomain;
exports.shouldUseSecureCaptureSessionCookie = shouldUseSecureCaptureSessionCookie;
exports.buildCaptureAccessTokenCookies = buildCaptureAccessTokenCookies;
const node_net_1 = require("node:net");
const ACCESS_TOKEN_COOKIE_NAME = 'playdrop_access_token';
const DOMAIN_PREFIXES = ['api.', 'www.', 'admin.', 'ai.', 'play.', 'assets.', 'engine.'];
function normalizeHostname(hostname) {
    return hostname.trim().toLowerCase();
}
function isLocalSessionHostname(hostname) {
    return hostname === 'localhost' || hostname.endsWith('.localhost') || hostname.endsWith('.local');
}
function stripKnownPrefix(hostname) {
    for (const prefix of DOMAIN_PREFIXES) {
        if (hostname.startsWith(prefix)) {
            return hostname.slice(prefix.length);
        }
    }
    return hostname;
}
function shouldUseSharedCookieDomain(hostname) {
    return hostname.length > 0 && !isLocalSessionHostname(hostname) && (0, node_net_1.isIP)(hostname) === 0;
}
function resolveCaptureSessionCookieDomain(hostname) {
    const override = process.env.PLAYDROP_SESSION_COOKIE_DOMAIN?.trim() || '';
    if (override) {
        return override;
    }
    const normalizedHostname = normalizeHostname(hostname);
    if (!shouldUseSharedCookieDomain(normalizedHostname)) {
        return null;
    }
    return stripKnownPrefix(normalizedHostname);
}
function shouldUseSecureCaptureSessionCookie(hostname) {
    const normalizedHostname = normalizeHostname(hostname);
    if (!normalizedHostname || isLocalSessionHostname(normalizedHostname) || (0, node_net_1.isIP)(normalizedHostname) !== 0) {
        return false;
    }
    return true;
}
function resolveLocalCaptureCookieHosts(hostname) {
    const normalizedHostname = normalizeHostname(hostname);
    if (normalizedHostname === 'localhost' || (0, node_net_1.isIP)(normalizedHostname) !== 0) {
        return [normalizedHostname];
    }
    const strippedHostname = stripKnownPrefix(normalizedHostname);
    if (!strippedHostname.endsWith('.localhost') && !strippedHostname.endsWith('.local') && strippedHostname !== 'localhost') {
        return [normalizedHostname];
    }
    return Array.from(new Set([
        normalizedHostname,
        `www.${strippedHostname}`,
        `api.${strippedHostname}`,
    ]));
}
function buildOrigin(protocol, hostname, port) {
    return `${protocol}//${hostname}${port ? `:${port}` : ''}`;
}
function resolveSharedCaptureCookieHosts(hostname, sharedDomain) {
    return Array.from(new Set([
        hostname,
        `www.${sharedDomain}`,
        `api.${sharedDomain}`,
    ]));
}
function buildCaptureAccessTokenCookies(targetUrl, accessToken) {
    const parsedUrl = new URL(targetUrl);
    const hostname = normalizeHostname(parsedUrl.hostname);
    const secure = shouldUseSecureCaptureSessionCookie(hostname);
    const sharedDomain = resolveCaptureSessionCookieDomain(hostname);
    if (sharedDomain) {
        return [
            {
                name: ACCESS_TOKEN_COOKIE_NAME,
                value: accessToken,
                domain: sharedDomain,
                path: '/',
                httpOnly: true,
                sameSite: 'Lax',
                secure,
            },
            ...resolveSharedCaptureCookieHosts(hostname, sharedDomain).map((cookieHostname) => ({
                name: ACCESS_TOKEN_COOKIE_NAME,
                value: accessToken,
                url: buildOrigin(parsedUrl.protocol, cookieHostname, parsedUrl.port),
                httpOnly: true,
                sameSite: 'Lax',
                secure,
            })),
        ];
    }
    return resolveLocalCaptureCookieHosts(hostname).map((cookieHostname) => ({
        name: ACCESS_TOKEN_COOKIE_NAME,
        value: accessToken,
        url: buildOrigin(parsedUrl.protocol, cookieHostname, parsedUrl.port),
        httpOnly: true,
        sameSite: 'Lax',
        secure,
    }));
}
