const { registerCheckoutFilters } = window.wc.blocksCheckout;
const { formatPrice } = window.wc.priceFormat || {};

const modifyItemName = (defaultValue, extensions, args) => {
    // Get custom data from extensions
    const customData = extensions?.['coupons-plus'];


    // If this item has a custom price, you can add visual indicators to the name
    if (customData?.has_discount) {
        // Example: You could add an indicator that this item has special pricing
        return `<div className=""> ${defaultValue}<br/><div class="wc-block-components-product-badge wc-block-components-sale-badge"> ${customData.discount_amount_formatted} OFF</div></div>`;
    }

    return defaultValue;
};

const modifyCartItemPrice = (defaultValue, extensions, args, validation) => {
    // Get custom data from extensions
    const customData = extensions?.['coupons-plus'];


    // If this item has a custom price, you can add visual indicators to the name
    if (customData?.has_discount) {
        // Example: You could add an indicator that this item has special pricing
        const currencySettings = window.wcSettings?.currency || {};
        const discountAmountInMinorUnits = Math.round(customData.discount_amount * Math.pow(10, currencySettings.minorUnit || 2));
        const formattedDiscount = formatPrice(discountAmountInMinorUnits, currencySettings);
        return `${defaultValue} (-${formattedDiscount})`;
    }

    return defaultValue;
}

registerCheckoutFilters('coupons-plus', {
    itemName: modifyItemName,
    cartItemPrice: modifyCartItemPrice,
});
