import { billingCadences, calculateBillingCatalogPrice, hiveGridBasePlans, type BillingCatalogPriceBreakdown, } from "./commercial-catalog.js"; import type { EntitlementPlanId } from "./plans.js"; export const REGISTRATION_MAX_SEAT_QUANTITY = 10_000; export type RegistrationPlanAdviceOption = { readonly planCode: EntitlementPlanId; readonly publicName: "Core" | "Swarm" | "Colony"; readonly breakdown: BillingCatalogPriceBreakdown; readonly recommended: boolean; }; export type RegistrationPlanAdvice = { readonly officeSeatQuantity: number; readonly fieldSeatQuantity: number; readonly recommendedPlanCode: EntitlementPlanId; readonly options: readonly RegistrationPlanAdviceOption[]; }; function requireRegistrationSeatQuantity(value: number, label: string, minimum: number): number { if (!Number.isSafeInteger(value) || value < minimum || value > REGISTRATION_MAX_SEAT_QUANTITY) { throw new RangeError(`${label} must be a safe integer between ${minimum} and ${REGISTRATION_MAX_SEAT_QUANTITY}.`); } return value; } /** * Recommends the cheapest monthly HiveGrid base-plan configuration for a team. * * The catalog order is the deterministic tie-breaker: Core, then Swarm, then * Colony. This function deliberately compares the complete recurring net * amount including extra seats instead of upgrading as soon as an included * quantity is exceeded. */ export function adviseRegistrationPlan(input: { readonly officeSeatQuantity: number; readonly fieldSeatQuantity: number; }): RegistrationPlanAdvice { const officeSeatQuantity = requireRegistrationSeatQuantity(input.officeSeatQuantity, "officeSeatQuantity", 1); const fieldSeatQuantity = requireRegistrationSeatQuantity(input.fieldSeatQuantity, "fieldSeatQuantity", 0); const pricedOptions = hiveGridBasePlans.map((plan) => ({ planCode: plan.technicalCode, publicName: plan.publicName, breakdown: calculateBillingCatalogPrice({ planCode: plan.technicalCode, cadence: billingCadences.monthly, officeSeatQuantity, fieldSeatQuantity, }), })); const recommended = pricedOptions.reduce((cheapest, candidate) => candidate.breakdown.recurringNetAmountCents < cheapest.breakdown.recurringNetAmountCents ? candidate : cheapest ); return Object.freeze({ officeSeatQuantity, fieldSeatQuantity, recommendedPlanCode: recommended.planCode, options: Object.freeze( pricedOptions.map((option) => Object.freeze({ ...option, recommended: option.planCode === recommended.planCode, }) ) ), }); }