import { generateUUID } from "./uuid";
import { BrowserStorage } from "~/utils/browser";
export function createNewSession() {
    const now = new Date().toISOString();
    let sessionId;
    try {
        sessionId = `$session:${generateUUID()}`;
    }
    catch (_error) {
        sessionId = `$session:default-${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
    }
    return {
        sessionId,
        startTime: now,
        endTime: now,
    };
}
export function getSessionId() {
    const storage = new BrowserStorage();
    const now = new Date();
    const sessionString = storage.getItem("localLogicSession");
    let session;
    if (!sessionString) {
        session = createNewSession();
    }
    else {
        session = JSON.parse(sessionString);
        // Check if the session has expired (30 minutes)
        if (session.endTime &&
            now.getTime() - new Date(session.endTime).getTime() >= 30 * 60 * 1000) {
            session = createNewSession();
        }
        else {
            session.endTime = now.toISOString();
        }
    }
    storage.setItem("localLogicSession", JSON.stringify(session));
    return session.sessionId;
}
//# sourceMappingURL=session.js.map