export function numericPrice(price: string): number {
  const value = Number(price.replace(/[^0-9.]/g, ''));
  return Number.isFinite(value) ? value : Number.POSITIVE_INFINITY;
}

export function decoratePlans<T extends { price: string }>(plans: T[]) {
  const sorted = [...plans]
    .map((plan, index) => ({ plan, index, value: numericPrice(plan.price) }))
    .sort((a, b) => a.value - b.value || a.index - b.index);

  const cheapestIndex = sorted[0]?.index ?? -1;
  const mobileOrder = new Map(sorted.map((entry, order) => [entry.index, order]));

  return plans.map((plan, index) => ({
    ...plan,
    isPopular: index === cheapestIndex,
    mobileOrder: mobileOrder.get(index) ?? index
  }));
}
