import type { SkuRow } from './read';

export interface PriceRange {
  min: string;
  max: string;
  maxDiscountPercent: number;
}

export function priceRange(skus: readonly SkuRow[]): PriceRange | null {
  const active = skus.filter((s) => s.isActive);
  if (active.length === 0) return null;
  const sorted = [...active].sort((a, b) => Number(a.discountedPrice) - Number(b.discountedPrice));
  return {
    min: sorted[0]!.discountedPrice,
    max: sorted[sorted.length - 1]!.discountedPrice,
    maxDiscountPercent: active.reduce((m, s) => Math.max(m, s.discountPercent), 0),
  };
}

export function resolveSkuPrice(sku: SkuRow): { original: string; discounted: string; percent: number } {
  return {
    original: sku.originalPrice,
    discounted: sku.discountedPrice,
    percent: sku.discountPercent,
  };
}
