<?php
/**
 * VendorRegistration class.
 *
 * @package DokanKits\Features\Vendor
 * @since   3.0.0
 */

namespace DokanKits\Features\Vendor;

use DokanKits\Features\Feature;
use WeDevs\Dokan\Registration;

/**
 * Vendor Registration Feature
 *
 * Removes the "I am a vendor" option from the WooCommerce my account page.
 * This feature allows marketplace owners to control the vendor registration
 * process by preventing users from self-registering as vendors through the
 * standard WooCommerce registration form.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Vendor
 */
class VendorRegistration extends Feature {

	/**
	 * Feature option key for enabling/disabling this feature
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $option_key = 'remove_vendor_checkbox';

	/**
	 * Feature settings group for admin UI organization
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $settings_group = 'vendor';

	/**
	 * Feature settings section within the group
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $settings_section = 'registration';

	/**
	 * Number of sub-features contained in this feature
	 *
	 * @since 3.0.0
	 * @var int
	 */
	protected int $total_features = 1;

	/**
	 * Register hooks with WordPress
	 *
	 * Sets up the action to remove the vendor registration option
	 * when the feature is enabled.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'init', array( $this, 'remove_registration_if_enabled' ), 20 );
	}

	/**
	 * Remove vendor registration if feature is enabled
	 *
	 * Removes Dokan's vendor registration form fields and validation
	 * from the WooCommerce registration process.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function remove_registration_if_enabled(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		// Remove Dokan's custom registration form fields.
		remove_action( 'woocommerce_register_form', 'dokan_seller_reg_form_fields', 10 );

		// Remove Dokan's registration validation.
		if ( class_exists( Registration::class ) && function_exists( 'dokan' ) ) {
			remove_filter( 'woocommerce_process_registration_errors', array( dokan()->registration, 'validate_registration' ) );
			remove_filter( 'woocommerce_registration_errors', array( dokan()->registration, 'validate_registration' ) );
		}
	}
}
