<?php
/**
 * WooCommerce class.
 *
 * @package SaveToWishlist\Classes\Integrations
 * @since 1.0.0
 */
namespace SaveToWishlist\Classes\Integrations;

use SaveToWishlist\Abstracts\Singleton;
use SaveToWishlist\Helpers\Helper_Functions;

/**
 * Class WooCommerce
 *
 * This class handles the WooCommerce integration.
 */
class WooCommerce extends Singleton {
    /**
     * Helper functions.
     *
     * @var Helper_Functions
     */
    private $helper_functions;

    /**
     * Allowed block HTML.
     *
     * @since 1.0.4
     * @access private
     * @var array
     */
    private $allowed_block_html;

    /**
     * Constructor.
     *
     * @since 1.0.4
     * @access public
     */
    public function __construct() {
        parent::__construct();
        $this->helper_functions = Helper_Functions::instance();
    }

    /**
     * Run the WooCommerce integration.
     *
     * @return void
     */
    public function run() {
        add_filter( 'render_block', array( $this, 'woocommerce_save_to_wishlist_block_do_actions' ), 9999, 2 );
    }

    /**
     * Get allowed HTML for block content including SVG elements.
     *
     * @since 1.0.4
     * @access private
     * @return array Allowed HTML tags and attributes.
     */
    private function get_allowed_block_html() {
        if ( null === $this->allowed_block_html ) {
            $allowed_html     = wp_kses_allowed_html( 'post' );
            $svg_allowed_html = $this->helper_functions->get_allowed_cart_button_html();

            foreach ( $svg_allowed_html as $tag => $attributes ) {
                if ( isset( $allowed_html[ $tag ] ) && is_array( $allowed_html[ $tag ] ) && is_array( $attributes ) ) {
                    $allowed_html[ $tag ] = array_merge( $allowed_html[ $tag ], $attributes );
                } else {
                    $allowed_html[ $tag ] = $attributes;
                }
            }
            $this->allowed_block_html = $allowed_html;
        }

        return $this->allowed_block_html;
    }

    /**
     * Add custom actions to the WooCommerce Cart block.
     *
     * @param string $block_content The block content.
     * @param array  $block         The block data.
     *
     * @since 1.0.0
     * @since 1.0.4 Added SVG support.
     * @access public
     * @return string The modified block content.
     */
    public function woocommerce_save_to_wishlist_block_do_actions( $block_content, $block ) {
        $blocks = array(
            'woocommerce/cart',
            'woocommerce/filled-cart-block',
            'woocommerce/cart-items-block',
            'woocommerce/cart-line-items-block',
            'woocommerce/cart-cross-sells-block',
            'woocommerce/cart-cross-sells-products-block',
            'woocommerce/cart-order-summary-block',
            'woocommerce/cart-order-summary-heading-block',
            'woocommerce/proceed-to-checkout-block',
            'woocommerce/product-image',
            'woocommerce/product-price',
            'woocommerce/product-button',
        );
        if ( in_array( $block['blockName'], $blocks, true ) ) {
            ob_start();
            do_action( 'stwlite_before_' . $block['blockName'] );
            echo wp_kses( $block_content, $this->get_allowed_block_html() );
            do_action( 'stwlite_after_' . $block['blockName'] );
            $block_content = ob_get_contents();
            ob_end_clean();
        }

        return $block_content;
    }
}
