import { adjustDecimals } from "./adjustDecimals";

/**
 * 1. Setting a minimum price difference threshold, which is $1000 if the input value is greater than $50,000, otherwise $500.
 * 2. Deciding whether to show the price impact based on:
 *    - The percentage price impact being greater than 2% and the price difference being at least $20, or
 *    - The price difference meeting or exceeding the minimum threshold.
 */

export const calculatePriceImpact = (
	inputValue?: number,
	outputValue?: number,
): string | undefined => {
	if (!inputValue || !outputValue) return;

	const priceDifference = inputValue - outputValue;
	const priceImpact = (priceDifference * 100) / inputValue;

	const minPriceDiff = inputValue > 50000 ? 1000 : 500;

	const showPriceImpact =
		(priceImpact > 2 && priceDifference >= 20) ||
		priceDifference >= minPriceDiff;

	if (showPriceImpact) {
		return `-$${adjustDecimals(priceDifference, 2)}`;
	}

	return undefined;
};
