<?php // phpcs:ignore Generic.PHP.RequireStrictTypes.MissingDeclaration

namespace FiboFilters\API;

use FiboFilters\Config\Env;
use WP_REST_Settings_Controller;

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

/**
 * Class SettingsController
 */
class SettingsController extends WP_REST_Settings_Controller {
	public function __construct() {
		parent::__construct();

		$this->namespace = Env::$plugin_slug . '/v1';
		$this->rest_base = 'settings';
	}

	public function get_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
		$minimum_capability = apply_filters( 'fibofilters/shop_manager_has_access', false ) ? 'manage_woocommerce' : 'manage_options';

		return current_user_can( $minimum_capability );
	}

	protected function get_registered_options() {
		$rest_options = parent::get_registered_options();

		// Remove all options that are not related to the plugin.
		foreach ( $rest_options as $key => $value ) {
			if ( strpos( $key, Env::$plugin_prefix ) !== 0 ) {
				unset( $rest_options[ $key ] );
			}
		}

		return $rest_options;
	}
}
