import BaseComponent from './BaseComponent.js';

/**
 * PriceDisplay component
 * Handles dynamic price updates based on variant selection
 */
class PriceDisplay extends BaseComponent {
    constructor(selector, productData) {
        super(selector);
        this.productData = productData;
        this.defaultPriceText = this._element.text();
    }

    /**
     * Initialize price display
     */
    init() {
        // Listen for variant changes
        if (window.ShopyPressEventBus) {
            window.ShopyPressEventBus.on('variant:changed', (data) => {
                this.updatePrice(data.variant);
            });
        }
    }

    /**
     * Update price display
     * @param {Object|null} variant - Variant object or null
     */
    updatePrice(variant) {
        if (!variant) {
            // Reset to default price range
            this._element.text(this.defaultPriceText);
            return;
        }

        // Display variant-specific price
        const formattedPrice = this.formatPrice(variant.price);
        this._element.text(formattedPrice);
    }

    /**
     * Format price (simple formatter, PHP handles currency formatting)
     * @param {string|number} price - Price value
     * @returns {string} - Formatted price
     */
    formatPrice(price) {
        // For now, just prepend $ symbol
        // In future, this could use currency settings passed from PHP
        const numPrice = parseFloat(price);
        return '$' + numPrice.toFixed(2);
    }
}

export default PriceDisplay;
