UNPKG

optumflex-subscription-core

Version:

Core logic and utilities for subscription management, pricing calculations, and data processing - framework agnostic

356 lines (352 loc) 11.2 kB
type BillingCycle = 'weekly' | 'monthly' | 'quarterly' | 'halfyearly' | 'yearly'; type PlanType = 'subscriptionPlans' | 'modelPortfolios'; interface PricingPackage { id: string; title: string; description: string; prices: { [key in BillingCycle]: number; }; discounted: { [key in BillingCycle]: number; }; features: string[]; } interface CartItem { plan: PricingPackage; cycle: BillingCycle; } interface CheckoutPayload { packageCode: string; duration: BillingCycle; } interface DiscountInfo { originalPrice: number; discountedPrice: number; savings: number; savingsPercentage: number; isDiscounted: boolean; } interface SubscriptionState { subscriptionPlans: PricingPackage[]; modelPortfolios: PricingPackage[]; selectedCycles: { [planId: string]: BillingCycle; }; planType: PlanType; sortBy: string; loading: boolean; error: string | null; } interface UseSubscriptionDataOptions { initialPlanType?: PlanType; initialSortBy?: string; onError?: (error: string) => void; onSuccess?: (message: string) => void; enableCart?: boolean; appUrl?: string; apiKey?: string; } interface UseSubscriptionDataReturn { subscriptionPlans: PricingPackage[]; modelPortfolios: PricingPackage[]; selectedCycles: { [planId: string]: BillingCycle; }; planType: PlanType; sortBy: string; loading: boolean; error: string | null; setSelectedCycle: (planId: string, cycle: BillingCycle) => void; setPlanType: (planType: PlanType) => void; setSortBy: (sortBy: string) => void; processApiResponse: (data: any) => void; getDisplayedPlans: () => PricingPackage[]; getPlanStatistics: () => { totalPlans: number; plansWithDiscounts: number; averagePrice: number; priceRange: { min: number; max: number; }; }; getDiscountInfo: (plan: PricingPackage, cycle: BillingCycle) => DiscountInfo; getAvailableCycles: (plan: PricingPackage) => BillingCycle[]; generateSinglePlanCheckoutUrl: (plan: PricingPackage, cycle: BillingCycle) => string; formatPrice: (amount: number) => string; getPlanPrice: (plan: PricingPackage, cycle: BillingCycle) => string; cart?: CartItem[]; cartItemCount?: number; cartTotal?: number; showCart?: boolean; addToCart?: (plan: PricingPackage, cycle: BillingCycle) => void; removeFromCart?: (planId: string, cycle: BillingCycle) => void; updateCartItemCycle?: (planId: string, newCycle: BillingCycle) => void; clearCart?: () => void; setShowCart?: (show: boolean) => void; handleCheckout?: () => void; } interface CartHandlers { addToCart: (plan: PricingPackage, cycle: BillingCycle) => void; removeFromCart: (planId: string, cycle: BillingCycle) => void; updateCartItemCycle: (planId: string, newCycle: BillingCycle) => void; clearCart: () => void; setShowCart: (show: boolean) => void; handleCheckout: () => void; } /** * Set API key for the package (call this once at app startup) */ declare const setApiKey: (key: string, endpoint?: string) => void; /** * Ensure package is initialized before executing any function */ declare const ensureInitialized: () => Promise<void>; /** * Get package validation status */ declare const getPackageStatus: () => { isInitialized: boolean; error?: string; domain?: string; expiresAt?: string; }; /** * Parse features from API response format */ declare const parseFeatures: (featuresArr: any[]) => string[]; /** * Parse a single plan from API response */ declare const parsePlan: (pkg: any) => PricingPackage; /** * Find the minimum price cycle for a plan */ declare const getMinimumPriceCycle: (prices: PricingPackage["prices"]) => BillingCycle; /** * Get available billing cycles for a plan */ declare const getAvailableCycles: (prices: PricingPackage["prices"]) => BillingCycle[]; /** * Calculate discount information for a plan and cycle */ declare const calculateDiscountInfo: (plan: PricingPackage, cycle: BillingCycle) => DiscountInfo; /** * Sort plans based on criteria */ declare const sortPlans: (plans: PricingPackage[], sortBy: string, selectedCycles: { [planId: string]: BillingCycle; }) => PricingPackage[]; /** * Generate checkout URL with cart data */ declare const generateCheckoutUrl: (cart: CheckoutPayload[], baseUrl: string) => string; /** * Handle buy now functionality for a single plan */ declare const handleBuyNow: (plan: PricingPackage, cycle: BillingCycle, companyInfo?: { application?: string; }) => void; /** * Format price with currency symbol */ declare const formatPrice: (price: number) => string; /** * Calculate total cart value */ declare const calculateCartTotal: (cart: { plan: PricingPackage; cycle: BillingCycle; }[]) => number; /** * Create checkout payload from cart items */ declare const createCheckoutPayload: (cart: CartItem[]) => CheckoutPayload[]; /** * Check if a plan has any discounts */ declare const hasDiscounts: (plan: PricingPackage) => boolean; /** * Get the best discount percentage for a plan */ declare const getBestDiscountPercentage: (plan: PricingPackage) => number; /** * Validate plan data */ declare const validatePlan: (plan: any) => boolean; /** * Filter plans by search query */ declare const filterPlansBySearch: (plans: PricingPackage[], searchQuery: string) => PricingPackage[]; /** * Get period label for display */ declare const getPeriodLabel: (period: string) => string; /** * Get icon for plan based on index */ declare const getIcon: (index: number) => string; /** * Get icon color class based on index */ declare const getIconColor: (index: number) => string; /** * Handle period selection for a plan */ declare const handlePeriodSelect: (planId: string, period: string, selectedPeriods: { [key: string]: string; }, setSelectedPeriods: (periods: { [key: string]: string; }) => void) => void; /** * Get default billing cycles for all plans */ declare const getDefaultCycles: (plans: PricingPackage[]) => { [planId: string]: BillingCycle; }; /** * Format currency with proper symbol and formatting */ declare const formatCurrency: (amount: number, currency?: string) => string; /** * Calculate savings amount and percentage */ declare const calculateSavings: (originalPrice: number, discountedPrice: number) => { amount: number; percentage: number; }; /** * Process raw API response and convert to subscription data format */ declare const processSubscriptionData: (apiResponse: any) => { subscriptionPlans: PricingPackage[]; modelPortfolios: PricingPackage[]; }; /** * Get subscription data with default cycles set */ declare const getSubscriptionData: (apiResponse: any) => { subscriptionPlans: PricingPackage[]; modelPortfolios: PricingPackage[]; defaultCycles: { [planId: string]: BillingCycle; }; }; /** * Create subscription state from API response */ declare const createSubscriptionState: (apiResponse: any, options?: { initialPlanType?: PlanType; initialSortBy?: string; }) => { subscriptionPlans: PricingPackage[]; modelPortfolios: PricingPackage[]; selectedCycles: { [planId: string]: BillingCycle; }; planType: PlanType; sortBy: string; }; /** * Update selected cycles for plans */ declare const updateSelectedCycles: (currentCycles: { [planId: string]: BillingCycle; }, planId: string, cycle: BillingCycle) => { [planId: string]: BillingCycle; }; /** * Get sorted and filtered plans for display */ declare const getDisplayPlans: (plans: PricingPackage[], sortBy: string, selectedCycles: { [planId: string]: BillingCycle; }, searchQuery?: string) => PricingPackage[]; /** * Generate checkout URL for a single plan */ declare const generateSinglePlanCheckoutUrl: (plan: PricingPackage, cycle: BillingCycle, companyInfo: { application: string; }) => string; /** * Get plan statistics */ declare const getPlanStatistics: (plans: PricingPackage[]) => { totalPlans: number; plansWithDiscounts: number; averagePrice: number; priceRange: { min: number; max: number; }; }; /** * Validate API response structure */ declare const validateApiResponse: (response: any) => boolean; /** * Transform API response to component-ready format */ declare const transformApiResponse: (apiResponse: any) => { isValid: boolean; data?: { subscriptionPlans: PricingPackage[]; modelPortfolios: PricingPackage[]; defaultCycles: { [planId: string]: BillingCycle; }; }; error?: string; }; /** * Check if a billing cycle is selected for a specific plan */ declare const isSelected: (selectedCycles: { [planId: string]: BillingCycle; }, planId: string, cycle: BillingCycle) => boolean; /** * Get displayed plans with sorting and filtering applied */ declare const getDisplayedPlans: (plans: PricingPackage[], sortBy: string, selectedCycles: { [planId: string]: BillingCycle; }, searchQuery?: string) => PricingPackage[]; /** * Scroll to a specific section on the page */ declare const scrollToSection: (sectionId: string) => void; /** * Handle keyboard events for accessibility */ declare const handleKeyDown: (event: React.KeyboardEvent, action: () => void) => void; /** * Get CSS classes for cycle selection styling */ declare const getCycleSelectionClasses: (isSelected: boolean, baseClasses?: string) => string; /** * Generate benefits data structure */ declare const generateBenefitsData: (benefits: Array<{ icon: React.ReactNode; title: string; description: string; }>) => { icon: React.ReactNode; title: string; description: string; id: number; }[]; /** * Generate FAQ data structure */ declare const generateFAQData: (faqs: Array<{ question: string; answer: string; }>) => { question: string; answer: string; id: number; }[]; declare const useSubscriptionData: (options?: UseSubscriptionDataOptions) => UseSubscriptionDataReturn; //# sourceMappingURL=use-subscription-data.d.ts.map export { calculateCartTotal, calculateDiscountInfo, calculateSavings, createCheckoutPayload, createSubscriptionState, ensureInitialized, filterPlansBySearch, formatCurrency, formatPrice, generateBenefitsData, generateCheckoutUrl, generateFAQData, generateSinglePlanCheckoutUrl, getAvailableCycles, getBestDiscountPercentage, getCycleSelectionClasses, getDefaultCycles, getDisplayPlans, getDisplayedPlans, getIcon, getIconColor, getMinimumPriceCycle, getPackageStatus, getPeriodLabel, getPlanStatistics, getSubscriptionData, handleBuyNow, handleKeyDown, handlePeriodSelect, hasDiscounts, isSelected, parseFeatures, parsePlan, processSubscriptionData, scrollToSection, setApiKey, sortPlans, transformApiResponse, updateSelectedCycles, useSubscriptionData, validateApiResponse, validatePlan }; export type { BillingCycle, CartHandlers, CartItem, CheckoutPayload, DiscountInfo, PlanType, PricingPackage, SubscriptionState, UseSubscriptionDataOptions, UseSubscriptionDataReturn };