<?php

namespace WcElectronInvoice\Providers;

defined( 'ABSPATH' ) || die( 'Not allowed.' );

class ProductsQuery {

	private static $instance;

	/**
	 * Return a single instance
	 *
	 * @return ProductsQuery
	 */
	public static function instance() {
		if ( ! isset( self::$instance ) ) {
			self::$instance = new ProductsQuery();
		}

		return self::$instance;
	}


	/**
	 * @var ProductsProviderAbstract[]
	 */
	protected $productsProviders = [];

	/**
	 * Get product by id and provider name
	 *
	 * @param $product_id
	 * @param $provider_name
	 *
	 * @return ProductAbstract
	 */
	public function getProduct( $product_id, $provider_name ) {
		$provider = $this->productsProviders[ $provider_name ];
		if ( ! $provider ) {
			return null;
		}

		return $provider->getProduct( $product_id );
	}

	/**
	 * Register a products provider
	 *
	 * @param ProductsProviderAbstract $provider
	 *
	 * @return void
	 */
	public function register_provider( ProductsProviderAbstract $provider ) {
		$this->productsProviders[ $provider->name() ] = $provider;
	}

}
