/** Parse Friday app session keys for transcript storage. */
const ACCOUNT_SESSION_RE = /^friday:account:([^:]+):([^:]+)$/i;
const DIRECT_SESSION_RE = /^friday:direct:[^:]+:([^:]+)$/i;
export function sessionFileIdFromKey(sessionKey) {
    const raw = sessionKey.trim();
    const mAccount = raw.match(ACCOUNT_SESSION_RE);
    if (mAccount?.[2])
        return sanitizeSessionFileId(mAccount[2]);
    const mDirect = raw.match(DIRECT_SESSION_RE);
    if (mDirect?.[1])
        return sanitizeSessionFileId(mDirect[1]);
    return sanitizeSessionFileId(raw);
}
export function parseFridaySessionKey(sessionKey) {
    const raw = sessionKey.trim();
    const mAccount = raw.match(ACCOUNT_SESSION_RE);
    if (mAccount?.[1] && mAccount[2]) {
        return {
            sessionKey: raw,
            accountIdFromKey: mAccount[1],
            sessionFileId: sanitizeSessionFileId(mAccount[2]),
        };
    }
    const mDirect = raw.match(DIRECT_SESSION_RE);
    if (mDirect?.[1]) {
        return {
            sessionKey: raw,
            sessionFileId: sanitizeSessionFileId(mDirect[1]),
        };
    }
    return {
        sessionKey: raw,
        sessionFileId: sanitizeSessionFileId(raw),
    };
}
function sanitizeSessionFileId(id) {
    const s = id.trim();
    if (!s)
        return "session";
    return s.replace(/[^a-zA-Z0-9._-]+/g, "-").slice(0, 128);
}
export function sessionBelongsToAccount(sessionKey, accountId) {
    const parsed = parseFridaySessionKey(sessionKey);
    if (!parsed.accountIdFromKey)
        return true;
    return parsed.accountIdFromKey.toLowerCase() === accountId.trim().toLowerCase();
}
