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

namespace DokanKits\Features\Vendor;

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

/**
 * Enable Store Support Feature
 *
 * Allows admins to enable store support directly for vendors.
 * This feature provides a toggle in the vendor capabilities section
 * for admins to enable or disable store support functionality globally.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Vendor
 */
class EnableStoreSupportByAdmin extends Feature {

	/**
	 * Feature option key for enabling/disabling this feature
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $option_key = 'enable_store_support_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 = 'capabilities';

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

	/**
	 * Get active feature count
	 *
	 * @since 3.0.0
	 * @return int
	 */
	public function get_active_feature_count(): int {
		return $this->is_enabled() ? $this->total_features : 0;
	}

	/**
	 * Register hooks with WordPress
	 *
	 * Sets up filters to modify the store support functionality
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'init', array( $this, 'remove_store_form_if_enabled' ), 20 );
		add_filter( 'dokan_vendor_shop_data', array( $this, 'update_store_support_data' ) );
	}

	/**
	 * Remove store support form if the feature is enabled
	 *
	 * This method removes the store support button title input field
	 * from the vendor settings form if the store support feature is enabled.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function remove_store_form_if_enabled(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		if ( ! function_exists( 'dokan_pro' ) || ! dokan_pro()->module->is_active( 'store_support' ) ) {
			return;
		}

		remove_action( 'dokan_settings_form_bottom', array( dokan_pro()->module->store_support, 'add_support_btn_title_input' ), 13 );
		remove_action( 'dokan_store_profile_saved', array( dokan_pro()->module->store_support, 'save_supoort_btn_title' ), 13 );
	}

	/**
	 * Update store support data for the vendor
	 *
	 * This method modifies the store support data array to ensure
	 * that the support button is shown and configured correctly.
	 *
	 * @since 3.0.0
	 *
	 * @param array $data Vendor store support data.
	 *
	 * @return array Modified store support data.
	 */
	public function update_store_support_data( $data ): array {
		if ( ! $this->is_enabled() ) {
			return $data;
		}

		// Add or modify store support data as needed.
		$data['show_support_btn_product'] ??= 'yes';
		$data['support_btn_name'] ??= '';
		$data['show_support_btn'] ??= 'yes';

		return $data;
	}
}
