/**
 * Credit-related utility functions
 * Extracted to break circular dependency between payment.ts, session.ts, and subscription.ts
 */
import type { TLineItemExpanded, TPriceExpanded } from '../store/models';
import { Price as TPrice } from '../store/models/price';

export function isCreditMetered(price: TPrice | TPriceExpanded) {
  return price.type === 'recurring' && price.recurring?.usage_type === 'metered' && !!price.recurring?.meter_id;
}

export function isCreditMeteredLineItems(lineItems: TLineItemExpanded[]) {
  return lineItems.every((item) => item.price && isCreditMetered(item.price));
}

/**
 * Get the effective price for a subscription item (upsell_price takes precedence)
 */
export function getSubscriptionItemPrice(item: TLineItemExpanded) {
  return item.upsell_price || item.price;
}
