import { randomUUID } from "node:crypto";
export function createPiboSessionId() {
    return randomUUID();
}
export function createSessionBinding(input, now = new Date().toISOString()) {
    return {
        sessionKey: input.sessionKey ?? createDefaultSessionKey(input),
        sessionId: input.sessionId ?? createPiboSessionId(),
        parentSessionKey: input.parentSessionKey,
        parentSessionId: input.parentSessionId,
        channel: input.channel,
        externalId: input.externalId,
        originalProfile: input.defaultProfile,
        workspace: input.workspace,
        createdAt: now,
        updatedAt: now,
    };
}
export function createDefaultSessionKey(input) {
    return `${input.channel}:${input.externalId}`;
}
export class InMemorySessionBindingStore {
    bySessionKey = new Map();
    byChannelExternalId = new Map();
    get(sessionKey) {
        return this.bySessionKey.get(sessionKey);
    }
    list() {
        return [...this.bySessionKey.values()];
    }
    resolve(input) {
        const channelExternalId = createDefaultSessionKey(input);
        const existing = this.byChannelExternalId.get(channelExternalId);
        if (existing)
            return existing;
        const binding = createSessionBinding(input);
        this.bySessionKey.set(binding.sessionKey, binding);
        this.byChannelExternalId.set(channelExternalId, binding);
        return binding;
    }
}
