import { v4 } from "uuid";
import { getExpiresAt } from "../expiring-kv.js";
import { DEFAULT_USER_EXPIRES_IN_MS, getExternalReferenceKey, getExternalUserReferenceStore, getUserStore } from "./store.js";
export async function setUser(data) {
    const store = getUserStore();
    const userId = data.userId || v4();
    const createdAt = data.createdAt || new Date().toISOString();
    const user = {
        ...data,
        userId,
        createdAt,
        updatedAt: createdAt,
        expiresAt: getExpiresAt(DEFAULT_USER_EXPIRES_IN_MS, data.expiresAt),
    };
    await store.set(userId, user);
    return user;
}
export async function setExternalReference(data) {
    const { externalId, externalType, ...rest } = data;
    const key = getExternalReferenceKey(externalType, externalId);
    const reference = {
        ...rest,
        externalType,
    };
    const store = getExternalUserReferenceStore();
    await store.set(key, reference);
    return reference;
}
/**
 * Used in the case where a user was anonymous, and is being converted to a specific user type
 * @param user
 * @param externalType
 */
export async function resetUserExpiryWithType(user, externalType) {
    return await setUser({
        ...user,
        // No longer anonymous
        externalType: externalType,
        // Reset expiry to default
        expiresAt: getExpiresAt(DEFAULT_USER_EXPIRES_IN_MS),
    });
}
//# sourceMappingURL=set-user.js.map