<?php
/**
 * YITH WooCommerce Name Your Price integration class
 *
 * @author  YITH
 * @package YITH\ProductBundles
 */

defined( 'YITH_WCPB' ) || exit;

/**
 * YITH WooCommerce Name Your Price Compatibility Class
 *
 * @class   YITH_WCPB_Name_Your_Price_Compatibility
 * @since   1.2.20
 */
class YITH_WCPB_Name_Your_Price_Compatibility {

	/**
	 * Single instance of the class.
	 *
	 * @var YITH_WCPB_Name_Your_Price_Compatibility
	 */
	protected static $instance;

	/**
	 * Returns single instance of the class
	 *
	 * @return YITH_WCPB_Name_Your_Price_Compatibility
	 */
	public static function get_instance() {
		return ! is_null( self::$instance ) ? self::$instance : self::$instance = new self();
	}

	/**
	 * YITH_WCPB_Name_Your_Price_Compatibility constructor.
	 */
	protected function __construct() {
		add_filter( 'ywcnp_product_types', array( $this, 'add_bundle_type' ) );
		add_filter( 'ywcnp_is_name_your_price', array( $this, 'is_name_your_price' ), 10, 2 );
	}

	/**
	 * Add bundle type to product types.
	 *
	 * @param array $types Product Types.
	 *
	 * @return array
	 */
	public function add_bundle_type( $types ) {
		$types[] = 'yith_bundle';

		return $types;
	}

	/**
	 * Check if this is a "name your price" product
	 *
	 * @param bool                   $result  The result.
	 * @param WC_Product_Yith_Bundle $product The product.
	 *
	 * @return mixed
	 */
	public function is_name_your_price( $result, $product ) {
		if ( $product && $product->is_type( 'yith_bundle' ) ) {
			$result = ! $product->per_items_pricing && $product->get_meta( '_is_nameyourprice' );
		}

		return $result;
	}
}

/**
 * Unique access to instance of YITH_WCPB_Name_Your_Price_Compatibility class
 *
 * @return YITH_WCPB_Name_Your_Price_Compatibility
 * @since 1.2.20
 */
function yith_wcpb_name_your_price_compatibility() {
	return YITH_WCPB_Name_Your_Price_Compatibility::get_instance();
}
