// src/utils/priceParser.ts
/**
 * Parses a price string into an integer number of cents.
 * Handles formats like "$10.99", "99¢", "250".
 * @param price The price string to parse.
 * @returns The price in cents, or null if unparsable.
 */
export const parsePriceToCents = (price: string): number | null => {
    if (!price || typeof price !== 'string') return null;

    const cleanedPrice = price.trim();

    // Handle "X for Y" cases - these are not a single item price.
    if (cleanedPrice.match(/\d+\s+for/i)) {
        return null;
    }

    // Handle "99¢" format
    const centsMatch = cleanedPrice.match(/(\d+\.?\d*)\s?¢/);
    if (centsMatch && centsMatch[1]) {
        return Math.round(parseFloat(centsMatch[1]));
    }

    // Handle "$10.99" or "10.99" format
    // The regex is updated to handle leading decimals (e.g., ".99") by changing `\d+` to `\d*`.
    // It now correctly captures cases with or without a leading zero.
    const dollarsMatch = cleanedPrice.match(/\$?(\d*\.?\d+)/);
    if (dollarsMatch && dollarsMatch[1]) {
        const numericValue = parseFloat(dollarsMatch[1]);
        return Math.round(numericValue * 100);
    }

    return null;
};
