<?php
/**
 * WooCommerce Attributes REST API Loader
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Rest_APIs
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Rest_APIs;

use WP_REST_Server;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Controllers\Rest_API\WooCommerce_Attributes_Rest_Controller;

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

/**
 * Class WooCommerce_Attributes_APIs
 *
 * Registers all WooCommerce Attributes REST API controllers and routes
 */
class WooCommerce_Attributes_APIs {

	/**
	 * Register all routes
	 *
	 * @return void
	 */
	public static function register_routes() {
		$controller = new WooCommerce_Attributes_Rest_Controller();

		// Get WooCommerce attributes and taxonomies.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/woocommerce-attributes',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $controller, 'get_attributes' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
			)
		);

		// Get WooCommerce countries.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/woocommerce-countries',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $controller, 'get_countries' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
			)
		);

		// Get WooCommerce states for a country.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/woocommerce-states',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $controller, 'get_states' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
					'args'                => array(
						'country' => array(
							'required'          => true,
							'type'              => 'string',
							'validate_callback' => function ( $param ) {
								return is_string( $param ) && ! empty( trim( $param ) );
							},
						),
					),
				),
			)
		);
	}
}
