import { isFeatureKey, type FeatureKey } from "./features.js"; export const entitlementLimitTypes = Object.freeze({ notIncluded: 0, included: 1, monthly: 2, max: 3, unlimited: 4, } as const); export type EntitlementLimitType = (typeof entitlementLimitTypes)[keyof typeof entitlementLimitTypes]; export type EntitlementDecisionReason = | "included" | "not-included" | "max-remaining" | "max-exhausted" | "monthly-remaining" | "monthly-exhausted" | "unlimited" | "trial-active" | "trial-expired" | "missing-subscription" | "unknown-feature"; export interface EvaluateEntitlementInput { readonly featureKey: string; readonly hasSubscription?: boolean; readonly trialActive?: boolean; readonly trialPeriod?: string; readonly trialMessage?: string; readonly limitType?: EntitlementLimitType; readonly limit?: number | null; readonly used?: number | null; readonly period?: string; readonly message?: string; } export interface EntitlementDecision { readonly allowed: boolean; readonly reason: EntitlementDecisionReason; readonly featureKey: FeatureKey | string; readonly limitType?: EntitlementLimitType; readonly limit?: number; readonly used?: number; readonly remaining?: number; readonly period?: string; readonly trialPeriod?: string; readonly message?: string; } function normalizeCount(value: number | null | undefined): number | undefined { return typeof value === "number" && Number.isFinite(value) ? Math.max(0, value) : undefined; } function usageDecision({ featureKey, limitType, limit, used, period, message, allowedReason, deniedReason, }: { readonly featureKey: FeatureKey; readonly limitType: EntitlementLimitType; readonly limit: number | null | undefined; readonly used: number | null | undefined; readonly period?: string; readonly message?: string; readonly allowedReason: EntitlementDecisionReason; readonly deniedReason: EntitlementDecisionReason; }): EntitlementDecision { const normalizedLimit = normalizeCount(limit) ?? 0; const normalizedUsed = normalizeCount(used) ?? 0; const remaining = Math.max(0, normalizedLimit - normalizedUsed); const allowed = remaining > 0; return { allowed, reason: allowed ? allowedReason : deniedReason, featureKey, limitType, limit: normalizedLimit, used: normalizedUsed, remaining, period, message, }; } export function evaluateEntitlement(input: EvaluateEntitlementInput): EntitlementDecision { const { featureKey, limitType, limit, used, period, message, trialActive, trialPeriod, trialMessage } = input; if (!isFeatureKey(featureKey)) { return { allowed: false, reason: "unknown-feature", featureKey, limitType, limit: normalizeCount(limit), used: normalizeCount(used), period, trialPeriod, message, }; } if (input.hasSubscription !== true) { if (trialActive === true) { return { allowed: true, reason: "trial-active", featureKey, limitType, period, trialPeriod, message: trialMessage ?? message, }; } return { allowed: false, reason: trialActive === false ? "trial-expired" : "missing-subscription", featureKey, limitType, limit: normalizeCount(limit), used: normalizeCount(used), period, trialPeriod, message: trialActive === false ? (trialMessage ?? message) : message, }; } switch (limitType) { case entitlementLimitTypes.included: return { allowed: true, reason: "included", featureKey, limitType, period, message }; case entitlementLimitTypes.notIncluded: return { allowed: false, reason: "not-included", featureKey, limitType, period, message }; case entitlementLimitTypes.max: return usageDecision({ featureKey, limitType, limit, used, period, message, allowedReason: "max-remaining", deniedReason: "max-exhausted", }); case entitlementLimitTypes.monthly: return usageDecision({ featureKey, limitType, limit, used, period, message, allowedReason: "monthly-remaining", deniedReason: "monthly-exhausted", }); case entitlementLimitTypes.unlimited: return { allowed: true, reason: "unlimited", featureKey, limitType, period, message }; default: return { allowed: false, reason: "not-included", featureKey, limitType, limit: normalizeCount(limit), used: normalizeCount(used), period, message, }; } }