<?php
declare( strict_types=1 );

namespace FiboFilters\Indexer;

use FiboFilters\Filters\FilterSources;
use FiboFilters\Filters\StoreFilters;
use FiboFilters\Integrations\Multilingual;
use FiboFilters\InvalidSourceQueryException;
use FiboFilters\Sorting\Sorting;
use FiboFilters\Util\Cache;
use FiboFilters\WooCommerce\Helper as WooCommerceHelper;

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

/**
 * Class SourceQueryFactory
 */
class SourceQueryFactory {

	/** @var BuildInfo */
	private $build_info;

	/** @var Cache */
	private $cache;

	/** @var Config */
	private $config;

	/** @var FilterSources */
	private $filter_sources;

	/** @var Multilingual */
	private $multilingual;

	/** @var StoreFilters */
	private $store_filters;

	/** @var WooCommerceHelper */
	private $wc_helper;

	/** @var Sorting */
	private $sorting;

	public function __construct( BuildInfo $build_info, Cache $cache, Config $config, FilterSources $filter_sources, Multilingual $multilingual, StoreFilters $store_filters, WooCommerceHelper $wc_helper, Sorting $sorting ) {
		$this->build_info     = $build_info;
		$this->cache          = $cache;
		$this->config         = $config;
		$this->filter_sources = $filter_sources;
		$this->multilingual   = $multilingual;
		$this->store_filters  = $store_filters;
		$this->wc_helper      = $wc_helper;
		$this->sorting        = $sorting;
	}

	/**
	 * @param string $type Type of source query.
	 * @param array  $args Source query arguments.
	 *
	 * @throws InvalidSourceQueryException Invalid source query type.
	 * @see \FiboFilters\Indexer\SourceQuery::set_args() Here you can check args.
	 */
	public function get( string $type, array $args = [] ): SourceQueryInterface {
		$result = null;
		switch ( $type ) {
			case 'filters':
				$result = new SourceQuery( $this->build_info, $this->cache, $this->config, $this->filter_sources, $this->multilingual, $this->store_filters, $this->wc_helper, $this->sorting, $args );
				break;
		}

		if ( $result === null ) {
			throw new InvalidSourceQueryException( 'Invalid source query type' );
		}

		return $result;
	}
}
