import { DEFAULT_AUTHENTICATION_STATE_EXPIRES_MS, getAuthenticationStateStore, } from "./store.js";
import { getExpiresAt } from "../expiring-kv.js";
import { v4 } from "uuid";
import { getMaybePartner, getMaybeUser } from "../../authentication/index.js";
export async function setAuthenticationState(data) {
    const stateId = data.stateId || v4();
    const stateKey = stateId;
    const createdAt = data.createdAt || new Date().toISOString();
    let createdBy = {};
    if (!(data.createdAt || data.createdByUserId || data.createdByOrganisationId)) {
        createdBy = {
            createdByUserId: getMaybeUser()?.userId,
            createdByOrganisationId: getMaybePartner()?.organisationId
        };
    }
    const state = {
        createdAt,
        ...data,
        ...createdBy,
        stateId,
        stateKey,
        expiresAt: getExpiresAt(DEFAULT_AUTHENTICATION_STATE_EXPIRES_MS, data.expiresAt),
    };
    const store = getAuthenticationStateStore();
    await store.set(stateId, state);
    return state;
}
//# sourceMappingURL=set-authentication-state.js.map