<?php
namespace Prodigy\Includes\Support\Addons\Elementor\Builder\Conditions;

use ElementorPro\Modules\ThemeBuilder;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\Includes\Prodigy as ProdigyPlugin;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Represents the condition for a product archive in the theme builder.
 *
 * This class defines the behavior, properties, and methods required
 * to handle product archive conditions, including sub-conditions
 * and logic for checking the validity of the condition.
 */
class Product_Archive extends ThemeBuilder\Conditions\Condition_Base {

	private $post_type;
	private $post_taxonomies;

	public function __construct( array $data = array() ) {
		$this->post_type       = ProdigyPlugin::PRODIGY_POST_TYPE_DEFAULT;
		$taxonomies            = get_object_taxonomies( $this->post_type, 'objects' );
		$this->post_taxonomies = wp_filter_object_list(
			$taxonomies,
			array(
				'public'            => true,
				'show_in_nav_menus' => true,
			)
		);

		parent::__construct( $data );
	}

	public static function get_type() {
		return 'archive';
	}

	public function get_name() {
		return 'product_archive';
	}

	public static function get_priority() {
		return 40;
	}

	public function get_label() {
		return __( 'Product Archive', 'prodigy-commerce' );
	}

	public function get_all_label() {
		return __( 'All Product Archives', 'prodigy-commerce' );
	}

	public function register_sub_conditions() {
		$this->register_sub_condition( new Shop_page() );

		foreach ( $this->post_taxonomies as $slug => $object ) {
			if ( in_array( $object->name, array( ProdigyPlugin::get_prodigy_category_type(), ProdigyPlugin::get_prodigy_tag_type() ) ) ) {
				$condition = new ThemeBuilder\Conditions\Taxonomy(
					array(
						'object' => $object,
					)
				);
				$this->register_sub_condition( $condition );
			}
		}
	}

	/**
	 * @param $args
	 *
	 * @return bool
	 */
	public function check( $args ): bool {
		return Prodigy_Url_Helper::is_archive_page_url();
	}
}
