import { resolveAccountIdFromRequest } from "./account-identity.js";
import { getTranscriptStore } from "./transcript-store.js";
export function accountIdFromSessionKey(sessionKey) {
    const trimmed = sessionKey.trim();
    const rawMatch = trimmed.match(/^friday:account:([^:]+):/i);
    if (rawMatch?.[1])
        return rawMatch[1];
    const storeMatch = trimmed.match(/^agent:main:friday:account:([^:]+):/i);
    return storeMatch?.[1] ?? null;
}
export function resolveArchiveAccountId(req, sessionKey) {
    return (req ? resolveAccountIdFromRequest(req) : null) ?? accountIdFromSessionKey(sessionKey);
}
export function archiveTranscriptEvent(params) {
    const accountId = params.accountId?.trim();
    const sessionKey = params.sessionKey?.trim();
    if (!accountId || !sessionKey)
        return;
    try {
        getTranscriptStore().appendEvent(accountId, sessionKey, params.event);
    }
    catch (err) {
        console.error(`[Friday-SYNC] archive failed: ${String(err)}`);
    }
}
