<?php

namespace CPBW\Frontend;
use CPBW\App\Utilities\Settings;

class BadgeSettings {

    /**
     * Initialize the class
     *
     * @return void
     */
    public function __construct() {
        $this->show_sale_badge_in_wc_commom_themes();

        /**
         * Hide astra sale badge
         */
        add_action('wp_enqueue_scripts', [$this, 'hide_astra_sale_badge']);
    }   

    /**
     * Show sale badge in WooCommerce common themes
     *
     * @return void
     */
    public function show_sale_badge_in_wc_commom_themes () {
        if( Settings::get( 'wc_badge_enabled' ) === false ) {
            add_filter( 'woocommerce_sale_flash', '__return_empty_string' );
            // add_filter( 'render_block_data', function( $block ) {
            //     if ( $block['blockName'] === 'woocommerce/product-image' ) {
            //         $block['attrs']['showSaleBadge'] = false;
            //     }
            //     return $block;
            // });
            
            add_filter( 'render_block', function( $block_content, $block ) {
                if ( isset( $block['blockName'] ) && $block['blockName'] === 'woocommerce/product-sale-badge' ) {
                    return ''; // Don’t render this block
                }
                return $block_content;
            }, 10, 2 );
        }
    }

    /**
     * Hide Astra theme sale badge
     * 
     * @return void
     */
    public function hide_astra_sale_badge() {
        if( Settings::get( 'wc_badge_enabled' ) === false ) {
            // Inline CSS to hide the Astra sale badge
            $custom_css = '
                .ast-on-card-button.ast-onsale-card {
                    display: none !important;
                }
            ';

            // Add the CSS inline with proper escaping
            wp_add_inline_style('astra-theme-css', $custom_css);
        }
    }
}