import { requestContext } from "@fastify/request-context";
import { ok } from "../is.js";
import { DEFAULT_ORGANISATION_ID } from "../config/index.js";
export const AUTHORIZED_PARTNER_ID_KEY = "authorizedForPartnerIds";
export const AUTHORIZED_ACCESS_TOKEN_KEY = "accessTokenKeyValue";
export const AUTHORIZED_PARTNER = "partner";
export const AUTHORIZED_USER = "user";
export const AUTHENTICATION_STATE = "authenticationState";
export function setAuthorizedForPartnerId(partnerId) {
    requestContext.set(AUTHORIZED_PARTNER_ID_KEY, partnerId);
}
export function getMaybeAuthorizedForPartnerId() {
    return requestContext.get(AUTHORIZED_PARTNER_ID_KEY);
}
/**
 * Where Authorized means is elevated membership and representative of given organisation
 */
export function getMaybeAuthorizedForOrganisationId() {
    const partner = getMaybePartner();
    return partner?.organisationId;
}
/**
 * Where Authenticated means is part of membership
 */
export function getMaybeAuthenticatedForOrganisationId() {
    const organisationId = getMaybeAuthorizedForOrganisationId();
    if (organisationId) {
        return organisationId;
    }
    return DEFAULT_ORGANISATION_ID;
}
export function isUnauthenticated() {
    return !isAuthenticated();
}
export function isAuthenticated() {
    // Later add more here if more authentication methods or types are available
    return !!(getMaybeAuthorizedForPartnerId() || getMaybeAuthenticationState());
}
export function getAuthorizedForPartnerId() {
    const partnerId = getMaybeAuthorizedForPartnerId();
    ok(partnerId, "Expected partner authorization");
    return partnerId;
}
export function validateAuthorizedForPartnerId(partnerId) {
    const authorizedPartnerId = getAuthorizedForPartnerId();
    ok(authorizedPartnerId === partnerId, "Expected partner authorization to match");
}
export function ensurePartnerMatchIfUnapproved(partnerId) {
    const { approved } = getPartner();
    if (process.env.VOUCH_REQUIRE_PARTNER_APPROVAL && !approved) {
        validateAuthorizedForPartnerId(partnerId);
    }
}
export function getMaybePartner() {
    return requestContext.get(AUTHORIZED_PARTNER);
}
export function getPartner() {
    const partner = getMaybePartner();
    ok(partner, "Expected authorized partner");
    return partner;
}
export function getMaybeUser() {
    return requestContext.get(AUTHORIZED_USER);
}
export function getUser() {
    const user = getMaybeUser();
    ok(user, "Expected authorized user");
    return user;
}
export function setUser(user) {
    requestContext.set(AUTHORIZED_USER, user);
}
export function getMaybeAccessToken() {
    return requestContext.get(AUTHORIZED_ACCESS_TOKEN_KEY);
}
export function getAccessToken() {
    const accessToken = getMaybeAccessToken();
    ok(accessToken, "Expected access token to be supplied");
    return accessToken;
}
export function setAuthenticationState(state) {
    requestContext.set(AUTHENTICATION_STATE, state);
}
export function getMaybeAuthenticationState() {
    const state = requestContext.get(AUTHENTICATION_STATE);
    if (state)
        return state;
    const partner = getMaybePartner();
    if (partner) {
        return {
            type: "partner",
            stateId: partner.partnerId,
            stateKey: partner.partnerId,
            partnerId: partner.partnerId,
            roles: ["partner"],
            createdAt: partner.createdAt,
            expiresAt: partner.createdAt,
        };
    }
    return undefined;
}
export function getAuthenticationState() {
    const state = getMaybeAuthenticationState();
    ok(state, "Expected to be authenticated");
    return state;
}
export function getAuthenticationRoles() {
    const state = getMaybeAuthenticationState();
    return state?.roles ?? [];
}
export function isRole(role) {
    const roles = getAuthenticationRoles();
    return roles.includes(role);
}
export function isMember() {
    return isRole("member");
}
export function isAdmin() {
    return isRole("admin");
}
export function isModerator() {
    return isAdmin() || isRole("moderator");
}
export function isIndustry() {
    return isRole("industry");
}
export function isAnonymous() {
    return isRole("anonymous") || isUnauthenticated();
}
export function getAuthenticatedContext() {
    const partner = getMaybePartner();
    const user = getMaybeUser();
    const organisationId = getMaybeAuthenticatedForOrganisationId() || "none";
    // no partner id but user indicates direct user as partner
    const partnerId = partner?.partnerId || "none";
    // no user id but partner indicates api
    const userId = user?.userId || "none";
    return {
        partner,
        user,
        partnerId,
        organisationId,
        userId
    };
}
//# sourceMappingURL=get-set.js.map