<?php
/**
 * RemoveSplitShipping class.
 *
 * @package DokanKits\Features\Shipping
 * @since   3.0.0
 */

namespace DokanKits\Features\Shipping;

use DokanKits\Features\Feature;

/**
 * Remove Split Shipping Feature
 *
 * Removes the split shipping functionality from Dokan Lite and Pro.
 * This feature allows store administrators to disable vendor-specific shipping
 * packages, consolidating all orders into a single shipping cost calculation.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Shipping
 */
class RemoveSplitShipping extends Feature {

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

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

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

	/**
	 * Register hooks with WordPress
	 *
	 * Sets up the necessary actions to remove split shipping functionality
	 * when enabled in the settings.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'init', array( $this, 'remove_split_shipping_if_enabled' ), 20 );
	}

	/**
	 * Check if the feature is enabled
	 *
	 * Overrides the parent is_enabled() method to check if either Lite or Pro
	 * split shipping removal is enabled.
	 *
	 * @since 3.0.0
	 * @return bool Whether any split shipping removal is enabled.
	 */
	public function is_enabled(): bool {
		return $this->is_lite_split_shipping_removal_enabled() || $this->is_pro_split_shipping_removal_enabled();
	}

	/**
	 * Check if Lite split shipping removal is enabled
	 *
	 * @since 3.0.0
	 * @return bool Whether Lite split shipping removal is enabled.
	 */
	public function is_lite_split_shipping_removal_enabled(): bool {
		return wc_string_to_bool( $this->get_setting( 'remove_split_shipping_checkbox', false ) );
	}

	/**
	 * Check if Pro split shipping removal is enabled
	 *
	 * @since 3.0.0
	 * @return bool Whether Pro split shipping removal is enabled.
	 */
	public function is_pro_split_shipping_removal_enabled(): bool {
		return wc_string_to_bool( $this->get_setting( 'remove_split_shipping_pro_checkbox', false ) );
	}

	/**
	 * Get active feature count
	 *
	 * Returns the number of active sub-features within this feature class.
	 * For RemoveSplitShipping, this counts how many removal options are enabled.
	 *
	 * @since 3.0.0
	 * @return int
	 */
	public function get_active_features_count(): int {
		$count = 0;

		if ( $this->is_lite_split_shipping_removal_enabled() ) {
			++$count;
		}

		if ( $this->is_pro_split_shipping_removal_enabled() ) {
			++$count;
		}

		return $count;
	}

	/**
	 * Remove split shipping if feature is enabled
	 *
	 * Checks settings and removes split shipping functionality for
	 * Dokan Lite, Pro, or both as configured.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function remove_split_shipping_if_enabled(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		if ( $this->is_lite_split_shipping_removal_enabled() ) {
			$this->remove_lite_split_shipping();
		}

		if ( $this->is_pro_split_shipping_removal_enabled() ) {
			$this->remove_pro_split_shipping();
		}
	}

	/**
	 * Remove split shipping for Dokan Lite
	 *
	 * Removes all hooks related to split shipping functionality
	 * in the Dokan Lite plugin.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	protected function remove_lite_split_shipping(): void {
		remove_all_filters( 'woocommerce_cart_shipping_packages' );
		remove_all_filters( 'woocommerce_shipping_package_name' );
		remove_all_actions( 'woocommerce_checkout_create_order_shipping_item' );
	}

	/**
	 * Remove split shipping for Dokan Pro
	 *
	 * Removes specific hooks related to split shipping functionality
	 * in the Dokan Pro plugin.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	protected function remove_pro_split_shipping(): void {
		remove_filter( 'woocommerce_cart_shipping_packages', 'dokan_custom_split_shipping_packages' );
		remove_filter( 'woocommerce_shipping_package_name', 'dokan_change_shipping_pack_name' );
		remove_action( 'woocommerce_checkout_create_order_shipping_item', 'dokan_add_shipping_pack_meta' );
	}
}
