<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Plugins\Elementor;

use Elementor\Element_Base;
use Elementor\Plugin;
use ElementorPro\Modules\ThemeBuilder\Module;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Utils
 */
class Utils {
	/**
	 * Check if Elementor Pro is active
	 *
	 * @return bool
	 */
	public function is_elementor_pro_active(): bool {
		return defined( 'ELEMENTOR_PRO_VERSION' ) && class_exists( '\ElementorPro\Plugin' );
	}

	/**
	 * Get Elementor widget instance for integration with FiboFilters on single page
	 *
	 * @param string $widget Widget name.
	 * @param int    $queried_object_id Queried object ID.
	 * @param bool   $check_fibofilters_integration Check for FiboFilters integration in widget settings.
	 */
	public function get_element_for_integrate_with_it_on_single( string $widget = '', int $queried_object_id = 0, bool $check_fibofilters_integration = false ): ?Element_Base {
		if ( ! class_exists( '\Elementor\Plugin' ) ) {
			return null;
		}

		$elementor = Plugin::instance();
		$document  = $elementor->documents->get_doc_for_frontend( $queried_object_id );

		if ( empty( $document ) || ! $document->is_built_with_elementor() ) {
			return null;
		}

		$elements_data = $document->get_elements_data();
		$widget_data   = $this->get_elementor_widget_data( $elements_data, $widget, $check_fibofilters_integration );

		if ( empty( $widget_data ) ) {
			return null;
		}

		return $elementor->elements_manager->create_element_instance( $widget_data );
	}

	/**
	 * Get Elementor widget instance for integration with FiboFilters on archive page
	 *
	 * @param string $widget Widget name.
	 * @param bool   $check_fibofilters_integration Check for FiboFilters integration in widget settings.
	 */
	public function get_element_for_integrate_with_it_on_archive( string $widget = '', bool $check_fibofilters_integration = false ): ?Element_Base {
		if ( ! class_exists( '\Elementor\Plugin' ) || ! class_exists( '\ElementorPro\Modules\ThemeBuilder\Module' ) ) {
			return null;
		}

		$elementor     = Plugin::instance();
		$theme_builder = Module::instance();

		$locations_manager = $theme_builder->get_conditions_manager();
		$documents         = $locations_manager->get_documents_for_location( 'archive' );

		if ( empty( $documents ) ) {
			return null;
		}

		foreach ( $documents as $document ) {
			$elements_data = $document->get_elements_data();
			$widget_data   = $this->get_elementor_widget_data( $elements_data, $widget, $check_fibofilters_integration );

			if ( empty( $widget_data ) ) {
				continue;
			}

			return $elementor->elements_manager->create_element_instance( $widget_data );
		}

		return null;
	}


	/**
	 * Search for specific widget in Elementor data
	 *
	 * @param array  $elements Widget data.
	 * @param string $widget Widget name.
	 * @param bool   $check_fibofilters_integration Check for FiboFilters integration.
	 */
	public function get_elementor_widget_data( $elements, $widget, bool $check_fibofilters_integration = false ): array {
		$result = [];

		if ( ! is_array( $elements ) || empty( $elements ) || empty( $widget ) ) {
			return [];
		}

		if (
			$check_fibofilters_integration &&
			isset( $elements['widgetType'] ) &&
			$elements['widgetType'] === $widget &&
			isset( $elements['settings']['fibofilters_integration'] ) &&
			$elements['settings']['fibofilters_integration'] === 'yes'
		) {
			return $elements;
		} elseif (
			! $check_fibofilters_integration &&
			isset( $elements['widgetType'] ) &&
			$elements['widgetType'] === $widget
		) {
			return $elements;
		}

		// Plain array of elements.
		if ( ! isset( $elements['elements'] ) ) {
			foreach ( $elements as $element ) {
				$result_tmp = $this->get_elementor_widget_data( $element, $widget, $check_fibofilters_integration );
				if ( ! empty( $result_tmp ) ) {
					$result = $result_tmp;
				}
			}
		} elseif ( is_array( $elements['elements'] ) && ! empty( $elements['elements'] ) ) {
			// Assoc array - single element.
			$result_tmp = $this->get_elementor_widget_data( $elements['elements'], $widget, $check_fibofilters_integration );
			if ( ! empty( $result_tmp ) ) {
				$result = $result_tmp;
			}
		}

		return $result;
	}
}
