<?php

namespace Brandy\BlockSettings\ProductDetailsDisplayTabs;

use Brandy\Traits\SingletonTrait;
use Brandy\Interfaces\ExternalBlockSettingsInterface;

class Caller implements ExternalBlockSettingsInterface {
	use SingletonTrait;

	private $enqueued_styles      = false;
	private $current_display_tabs = null;

	protected function __construct() {
		add_filter( 'block_type_metadata_settings', array( $this, 'declare_attribute' ), 10 );
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_settings_scripts' ) );
		// Filter product tabs based on displayTabs attribute (only on frontend)
		if ( ! is_admin() ) {
			add_filter( 'render_block_data', array( $this, 'set_display_tabs_context' ), 10, 3 );
		}
	}

	private function is_product_details_block( $name ) {
		return in_array( $name, array( 'woocommerce/product-details' ) );
	}

	public function declare_attribute( $settings ) {

		if ( empty( $settings['name'] ) ) {
			return $settings;
		}

		if ( ! $this->is_product_details_block( $settings['name'] ) ) {
			return $settings;
		}

		if ( ! empty( $settings['attributes'] ) ) {
			$settings['attributes']['displayTabs'] = array(
				'type'    => 'array',
				'default' => array( 'description', 'additional_information', 'reviews' ),
			);
		}
		return $settings;
	}


	public function enqueue_settings_scripts() {
		wp_enqueue_script( 'brandy/product-details-display-tabs', BRANDY_TEMPLATE_URL . '/inc/BlockSettings/ProductDetailsDisplayTabs/script.js', array( 'wp-edit-post' ), BRANDY_SCRIPT_VERSION, true );
		wp_enqueue_style( 'brandy/product-details-display-tabs', BRANDY_TEMPLATE_URL . '/inc/BlockSettings/ProductDetailsDisplayTabs/style.css', array(), BRANDY_SCRIPT_VERSION );
	}

	/**
	 * Set displayTabs context before block renders.
	 *
	 * @param array    $parsed_block The parsed block data.
	 * @param array    $source_block The source block data.
	 * @param WP_Block $parent_block The parent block instance.
	 * @return array The parsed block data.
	 */
	public function set_display_tabs_context( $content, $block, $context ) {
		if ( ! $this->is_product_details_block( $block['blockName'] ) ) {
			return $content;
		}
		$display_tabs               = $block['attrs']['displayTabs'] ?? null;
		if ( empty( $display_tabs ) ) {
			return $content;
		}
		$this->current_display_tabs = $display_tabs;
		add_filter( 'woocommerce_product_tabs', array( $this, 'filter_product_tabs' ), 10 );
		return $content;
	}

	/**
	 * Filter product tabs based on displayTabs attribute.
	 *
	 * @param array $tabs Array of product tabs.
	 * @return array Filtered tabs.
	 */
	public function filter_product_tabs( $tabs ) {
		// Filter out tabs that are not in displayTabs
		$filtered_tabs = array();
		foreach ( $tabs as $key => $tab ) {
			// Map tab keys to displayTabs values
			$tab_key_mapping = array(
				'description'            => 'description',
				'additional_information' => 'additional_information',
				'reviews'                => 'reviews',
			);

			// Check if this tab should be displayed
			$should_display = false;
			if ( isset( $tab_key_mapping[ $key ] ) ) {
				// Standard WooCommerce tabs
				$should_display = in_array( $tab_key_mapping[ $key ], $this->current_display_tabs, true );
			} else {
				// Custom tabs from plugins - keep them by default
				// You can modify this logic if you want to control custom tabs too
				$should_display = true;
			}

			if ( $should_display ) {
				$filtered_tabs[ $key ] = $tab;
			}
		}

		return $filtered_tabs;
	}
}

Caller::get_instance();
