<?php
declare( strict_types=1 );

namespace FiboFilters\API;

use FiboFilters\Index\Descriptors;
use FiboFilters\Integrations\Multilingual;
use FiboFilters\Struct\QueryContext;
use WP_REST_Response;
use WP_REST_Request;

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

/**
 * Class Filters
 */
class Filters {
	/** @var Descriptors */
	private Descriptors $descriptors;

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

	public function __construct( Descriptors $descriptors, Multilingual $multilingual ) {
		$this->descriptors  = $descriptors;
		$this->multilingual = $multilingual;
	}

	public function descriptors(): WP_REST_Response {
		$language = $this->multilingual->is_multilingual() ? $this->multilingual->get_current_language() : '';

		return new WP_REST_Response( $this->descriptors->get_all_descriptors( new QueryContext( $language ) ) );
	}

	public function updated_descriptors( WP_REST_Request $request ): WP_REST_Response {
		$language = $this->multilingual->is_multilingual() ? $this->multilingual->get_current_language() : '';

		return new WP_REST_Response(
			$this->descriptors->get_changed_descriptors(
				$request->get_param( 'date_end' ),
				$request->get_param( 'date_start' ),
				new QueryContext( $language )
			)
		);
	}
}
