<?php
namespace Jet_Engine_Divi_Views\Components\Jet_Smart_Filters;

use Jet_Engine_Divi_Views\Components\Base_Component;

// Prevent direct access to the file.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Base component class
 */
class Component extends Base_Component {

	/**
	 * Return JSF blocks attributes schema for Divi editor.
	 *
	 * @return array
	 */
	public function get_blocks_atts() {

		if ( ! function_exists( 'jet_smart_filters' ) ) {
			return [];
		}

		jet_smart_filters()->blocks->register_block_types();

		$atts = [];

		if ( empty( jet_smart_filters()->blocks->blocks_types ) ) {
			return $atts;
		}

		foreach ( jet_smart_filters()->blocks->blocks_types as $block_name => $block_type ) {
			if ( ! is_object( $block_type ) || ! method_exists( $block_type, 'get_attributes' ) ) {
				continue;
			}

			$block_atts = $block_type->get_attributes();

			if ( isset( $block_atts['__internalWidgetId'] ) ) {
				unset( $block_atts['__internalWidgetId'] );
			}

			$atts[ $block_name ] = $block_atts;
		}

		return $atts;
	}

	/**
	 * Register component
	 *
	 * @return void
	 */
	public function register() {

		if ( ! function_exists( 'jet_smart_filters' ) ) {
			return;
		}
        
		add_action( 'jet-engine/divi-views/register-modules', [ $this, 'register_modules' ] );
		add_filter( 'jet-engine-divi-views/modules-controller/assets', [ $this, 'modules_assets' ] );
		add_action( 'divi_visual_builder_assets_before_enqueue_scripts', [ $this, 'enqueue_builder_dependencies' ], 5 );
		add_action( 'wp_enqueue_scripts', function () {

			if ( ! function_exists( 'jet_smart_filters' ) ) {
				return;
			}

			if ( isset($_GET['et_fb']) && '1' === $_GET['et_fb'] ) {
				// enqueue assets
				jet_smart_filters()->set_filters_used();
				jet_smart_filters()->filter_types->filter_scripts();
				jet_smart_filters()->filter_types->filter_styles();
			}
		}, 10 );
	}

	/**
	 * Ensure JSF frontend dependencies are available inside Divi Visual Builder.
	 *
	 * Date Period depends on air-datepicker and crashes in builder if the script
	 * is not present before JSF public.js initializes the filter preview.
	 *
	 * @return void
	 */
	public function enqueue_builder_dependencies() {

		if ( ! function_exists( 'et_builder_d5_enabled' ) || ! function_exists( 'et_core_is_fb_enabled' ) ) {
			return;
		}

		if ( ! et_builder_d5_enabled() || ! et_core_is_fb_enabled() ) {
			return;
		}

		jet_smart_filters()->set_filters_used();
		jet_smart_filters()->filter_types->filter_scripts();
		jet_smart_filters()->filter_types->filter_styles();

		wp_enqueue_script( 'air-datepicker' );
		wp_enqueue_style( 'air-datepicker' );
	}

	/**
	 * Get component ID
	 *
	 * @return string
	 */
	public function get_id() {
		return 'jet-smart-filters-divi-views';
	}

	/**
	 * Register modules
	 *
	 * @param object $dependency_tree Dependency tree instance.
	 */
	public function register_modules( $dependency_tree ) {

		if ( ! function_exists( 'jet_smart_filters' ) ) {
			return;
		}

		jet_smart_filters()->blocks->register_block_types();

		$dependency_tree->add_dependency( new Modules\Select_Filter() );
		$dependency_tree->add_dependency( new Modules\Checkboxes_Filter() );
		$dependency_tree->add_dependency( new Modules\Range_Filter() );
		$dependency_tree->add_dependency( new Modules\Check_Range_Filter() );
		$dependency_tree->add_dependency( new Modules\Radio_Filter() );
		$dependency_tree->add_dependency( new Modules\Date_Range_Filter() );
		$dependency_tree->add_dependency( new Modules\Date_Period_Filter() );
		$dependency_tree->add_dependency( new Modules\Rating_Filter() );
		$dependency_tree->add_dependency( new Modules\Active_Filters() );
		$dependency_tree->add_dependency( new Modules\Active_Tags() );
		$dependency_tree->add_dependency( new Modules\Alphabet() );
		$dependency_tree->add_dependency( new Modules\Apply_Button() );
		$dependency_tree->add_dependency( new Modules\Color_Image() );
		$dependency_tree->add_dependency( new Modules\Pagination() );
		$dependency_tree->add_dependency( new Modules\Remove_Filters() );
		$dependency_tree->add_dependency( new Modules\Search() );
		$dependency_tree->add_dependency( new Modules\Sorting() );
		$dependency_tree->add_dependency( new Modules\Hidden() );
	}

	/**
	 * Register module related assets and data
	 */
	public function modules_assets( $assets ) {

		$editor_config = jet_smart_filters()->blocks->get_block_editor_config();
		$editor_config['atts'] = $this->get_blocks_atts();

		$assets[] = [
			'handle'   => 'jet-smart-filters-divi-views-editor',
			'src'      => JET_ENGINE_DIVI_VIEWS_URL . 'assets/js/jet-smart-filters-bundle.js',
			'deps'     => [],
			'localize' => [
				'object' => 'JSFDiviViews',
				'data'   => $editor_config,
			],
		];

		return $assets;
	}
}
