/**
 * Shared discount-price helpers for the vendor-add-deal flow.
 *
 * Formula: Math.round(original × (1 − pct/100) × 100) / 100
 * Identical numeric behaviour to the previously-inline versions in
 * SkuGrid, useAddDeal, VariantsStep.
 */

import { shekelFloatToDecimalString } from '@/lib/money.js';

/**
 * Returns the discounted price as a number (2 decimal places via Math.round).
 * Returns 0 if inputs are invalid.
 */
export function discountedPrice(original: number, pct: number): number {
  if (!Number.isFinite(original) || original <= 0 || !Number.isFinite(pct)) return 0;
  return Math.round(original * (1 - pct / 100) * 100) / 100;
}

/**
 * Returns the discounted price as a fixed-2 decimal string for API payloads (e.g. "45.00").
 * Returns '' if the original price is invalid / non-positive.
 */
export function discountedPriceStr(original: number, pct: number): string {
  if (!Number.isFinite(original) || original <= 0) return '';
  return shekelFloatToDecimalString(discountedPrice(original, pct));
}
