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

use Elementor\Controls_Manager;
use ElementorPro\Modules\ThemeBuilder;
use Prodigy\Includes\Models\Prodigy_Products;

class Prodigy_Product_Ids extends ThemeBuilder\Conditions\Condition_Base {

	/** @var object Prodigy_Product_Parser */
	private $product;

	public function __construct( array $data = array() ) {
		parent::__construct( $data );
	}

	protected function register_controls() {
		$this->add_control(
			'prodigy_product_ids',
			array(
				'section'  => 'settings',
				'type'     => Controls_Manager::SELECT2,
				'multiple' => false,
				'options'  => Prodigy_Products::get_products_by_type( Prodigy_Products::PRODUCTS_IDS ),
				'default'  => array(),
			)
		);
	}

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

	public static function get_priority() {
		return 40;
	}

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

	/**
	 * @param array $args
	 * @return bool
	 */
	public function check( $args ) {
		$is_product = false;
		if ( isset( $args['id'] ) && is_single() ) {
			$local_product = Prodigy_Products::get_product_meta_by_remote_id( 'prodigy_remote_product_id', $args['id'] );
		}
		if ( ! empty( $local_product ) ) {
			$is_product = url_to_postid( get_permalink() ) == $local_product->post_id;
		}

		return $is_product;
	}
}
