<?php
/**
 * Settings & Options Functions
 *
 * Core option handling and platform settings
 *
 * @since   3.0.0
 * @package WC_Affiliate
 */

use WC_Affiliate\Models\Settings;

/**
 * Get WC Affiliate option
 *
 * Retrieves a setting value from the WC Affiliate settings structure.
 * Supports nested access using dot notation (e.g., 'basic.general.dashboard_page').
 *
 * @since 1.0.0
 *
 * @param string $page Settings page key (e.g., 'basic', 'advanced', 'email').
 * @param string $key Settings section/field key within the page.
 * @param mixed  $default Default value if setting not found.
 *
 * @return mixed The option value or default.
 */
function wca_get_option( string $page, string $key = '', $default = '' ) {
	$option_model = new Settings();

	return $option_model->get_option( $page, $key, $default );
}

/**
 * Get the platform (woocommerce or easycommerce)
 *
 * Retrieves the currently configured e-commerce platform that WC Affiliate is integrating with.
 * This determines which platform-specific hooks and functionality will be used.
 *
 * @since 1.0.0
 *
 * @return string Platform name (woocommerce|easycommerce).
 */
function wca_get_platform(): string {
	return wca_get_option( 'basic', 'general.platform', 'woocommerce' );
}

/**
 * Get the currency symbol based on platform
 *
 * Retrieves the appropriate currency symbol from the active e-commerce platform.
 * Falls back to the platform's native currency function or defaults to dollar sign.
 *
 * @since 1.0.0
 *
 * @return string Currency symbol (e.g., '$', '€', '£').
 */
function wca_get_currency_symbol(): string {
	$platform = wca_get_platform();

	if ( 'easycommerce' === $platform && function_exists( 'easycommerce_currency_symbol' ) ) {
		return easycommerce_currency_symbol();
	}

	if ( 'woocommerce' === $platform && function_exists( 'get_woocommerce_currency_symbol' ) ) {
		return get_woocommerce_currency_symbol();
	}

	return '$';
}

/**
 * Get the active store currency code
 *
 * Retrieves the 3-letter ISO currency code from the active e-commerce
 * platform. Mirrors wca_get_currency_symbol() but returns the code
 * (e.g. 'USD') instead of the symbol (e.g. '$').
 *
 * @since 1.0.0
 *
 * @return string Currency code (e.g., 'USD', 'EUR', 'GBP').
 */
function wca_get_currency(): string {
	$platform = wca_get_platform();

	if ( 'easycommerce' === $platform && function_exists( 'easycommerce_currency' ) ) {
		return easycommerce_currency();
	}

	if ( 'woocommerce' === $platform && function_exists( 'get_woocommerce_currency' ) ) {
		return get_woocommerce_currency();
	}

	return 'USD';
}

/**
 * Get available commission types
 *
 * Returns an array of available commission calculation types
 * for affiliate commissions (fixed amount or percentage).
 *
 * @since 1.0.0
 *
 * @return array Array of commission types with labels.
 */
function wca_get_commission_types(): array {
	return array(
		'fixed'   => __( 'Fixed', 'wc-affiliate' ),
		'percent' => __( 'Percent', 'wc-affiliate' ),
	);
}

/**
 * Get the WooCommerce my account tab label for affiliates
 *
 * Retrieves the custom label text for the affiliate tab in WooCommerce My Account page.
 * This label is displayed in the navigation menu of the My Account area.
 *
 * @since 1.0.0
 *
 * @return string Tab label text.
 */
function wca_wc_tab_label(): string {
	return wca_get_option( 'advanced', 'my_account.wc_tab_label', __( 'Affiliate', 'wc-affiliate' ) );
}

/**
 * Get dashboard page ID
 *
 * Retrieves the WordPress page ID that serves as the affiliate dashboard.
 * This page displays the affiliate's statistics, links, and management interface.
 *
 * @since 1.0.0
 *
 * @return int Dashboard page ID.
 */
function wca_get_dashboard_page_id(): int {
	return (int) wca_get_option( 'basic', 'general.dashboard_page', 0 );
}

/**
 * Get commission type setting
 *
 * Retrieves the commission calculation method used for affiliate earnings.
 * Determines whether commissions are calculated as percentage or fixed amount.
 *
 * @since 1.0.0
 *
 * @return string Commission type ('percent' or 'fixed').
 */
function wca_get_commission_type(): string {
	return wca_get_option( 'basic', 'commission.commission_type', 'percent' );
}

/**
 * Get commission amount setting
 *
 * Retrieves the base commission amount or percentage for affiliate earnings.
 * This value is used as the default commission rate for all affiliates unless overridden.
 *
 * @since 1.0.0
 *
 * @return float Commission amount (percentage or fixed value).
 */
function wca_get_commission_amount(): float {
	return (float) wca_get_option( 'basic', 'commission.commission_amount', 20 );
}

/**
 * Get referral key setting
 *
 * Retrieves the URL query parameter key used to identify affiliate referrals.
 * This parameter appears in referral URLs (e.g., ?ref=affiliate_id).
 *
 * @since 1.0.0
 *
 * @return string The referral query parameter key.
 */
function wca_get_ref_key(): string {
	return wca_get_option( 'advanced', 'referral.ref_key', 'ref' );
}

/**
 * Get token type for affiliate identification
 *
 * Determines how affiliates are identified in referral URLs and tracking.
 * Supported types: 'id' (user ID), 'ID' (uppercase), 'slug' (user nicename),
 * 'email' (user email), or 'login' (username).
 *
 * @since 1.0.0
 *
 * @return string Token type identifier.
 */
function wca_get_token_type(): string {
	return wca_get_option( 'advanced', 'referral.token_type', 'ID' );
}

/**
 * Get referral cookie name
 *
 * Retrieves the cookie name used to store affiliate referral information in visitor's browser.
 * This cookie persists the affiliate reference across multiple page visits.
 *
 * @since 1.0.0
 *
 * @return string Cookie name.
 */
function wca_get_cookie_name(): string {
	return wca_get_option( 'advanced', 'referral.cookie_name', '_wc_affiliate' );
}

/**
 * Get visit count cookie name
 *
 * Retrieves the cookie name used to track the number of visits from a referred user.
 * This helps prevent duplicate visit counting for the same visitor.
 *
 * @since 1.0.0
 *
 * @return string Visit tracking cookie name.
 */
function wca_get_visit_cookie_name(): string {
	return wca_get_option( 'advanced', 'referral.visit_cookie_name', 'wc-affiliate-visit' );
}

/**
 * Get cookie expiry time value
 *
 * Retrieves the numeric value for cookie expiration duration.
 * This is combined with the expiry unit to calculate total cookie lifetime.
 *
 * @since 1.0.0
 *
 * @return int Cookie expiry time value.
 */
function wca_get_cookie_expiry_time(): int {
	return (int) wca_get_option( 'basic', 'tracking.cookie_expiry_time', 30 );
}

/**
 * Get cookie expiry unit value
 *
 * Retrieves the time unit in seconds used for cookie expiration calculation.
 * Common values: MINUTE_IN_SECONDS (60), HOUR_IN_SECONDS (3600), DAY_IN_SECONDS (86400).
 *
 * @since 1.0.0
 *
 * @return int Cookie expiry unit in seconds.
 */
function wca_get_cookie_expiry_unit(): int {
	return (int) wca_get_option( 'basic', 'tracking.cookie_expiry_unit', DAY_IN_SECONDS );
}

/**
 * Get cookie validity in seconds
 *
 * Calculates the total cookie expiration time in seconds by multiplying
 * the expiry time value with the expiry unit.
 *
 * @since 1.0.0
 *
 * @return int Cookie expiry time in seconds.
 */
function wca_get_cookie_expiry(): int {
	$time = wca_get_cookie_expiry_time();
	$unit = wca_get_cookie_expiry_unit();

	return $time * $unit;
}

/**
 * Generate a token for the current user based on the specified type
 *
 * Creates a unique identifier token for an affiliate based on their user data.
 * Supports multiple identification methods for flexible referral tracking.
 *
 * @since 1.0.0
 *
 * @param string $token_type The type of token to generate: 'id', 'slug', 'email', or 'login'.
 * @param int    $user_id user id.
 *
 * @return string The generated token, or empty string if user doesn't exist.
 */
function wca_generate_token( string $token_type = 'id', int $user_id = 0 ): string {
	$token_type  = strtolower( $token_type );
	$token_types = array( 'id', 'slug', 'email', 'login' );
	if ( ! in_array( $token_type, $token_types, true ) ) {
		$token_type = 'id';
	}

	if ( 0 !== $user_id ) {
		$user = get_user_by( 'id', $user_id );
	} else {
		$user = wp_get_current_user();
	}

	if ( ! $user instanceof WP_User || ! $user->exists() ) {
		// Use cryptographically secure random generation.
		// return wp_generate_password( 10, false, false ); .
		return '';
	}

	// If user exists, generate a token based on the type.
	switch ( $token_type ) {
		case 'slug':
			$identifier = $user->user_nicename;
			break;
		case 'email':
			$identifier = $user->user_email;
			break;
		case 'login':
			$identifier = $user->user_login;
			break;
		default:
			$identifier = $user->ID;
			break;
	}

	return $identifier;
}

/**
 * Get user token based on configured token type
 *
 * Retrieves the affiliate identification token for a specific user using
 * the globally configured token type setting.
 *
 * @since 1.0.0
 *
 * @param int $user_id User ID. Defaults to current user if not provided.
 *
 * @return string User token string or empty string if user not found.
 */
function wca_get_token( int $user_id = 0 ) {
	if ( ! $user_id ) {
		$user_id = get_current_user_id();
	}

	$token_type = wca_get_token_type();

	return wca_generate_token( $token_type, $user_id );
}

/**
 * Check if credit should be given only once per customer
 *
 * Determines whether an affiliate receives commission credit only for
 * the first purchase from a referred customer, or for all subsequent purchases.
 *
 * @since 1.0.0
 *
 * @return bool True if credit once is enabled, false otherwise.
 */
function wca_credit_once(): bool {
	return wca_get_option( 'advanced', 'behavior.credit_once', 'off' ) === 'on';
}

/**
 * Check if visit logging is enabled
 *
 * Determines whether all referral visits/hits should be logged in the database.
 * Logging enables detailed visit tracking and analytics but increases database usage.
 *
 * @since 1.0.0
 *
 * @return bool True if logging is enabled, false otherwise.
 */
function wca_log_enabled(): bool {
	return apply_filters( 'wc_affiliate_log_enabled', true );
}

/**
 * Check if cookie overwrite is allowed
 *
 * Determines whether affiliates can earn commission from their own purchases.
 * When disabled, self-referrals are excluded from commission calculation.
 *
 * @since 1.0.0
 *
 * @return bool True if self-referral is allowed, false otherwise.
 */
function wca_allow_overwrite(): bool {
	return wca_get_option( 'advanced', 'behavior.allow_overwrite', 'off' ) === 'on';
}

/**
 * Check if self-referral is allowed
 *
 * Determines whether affiliates can earn commission from their own purchases.
 * When disabled, self-referrals are excluded from commission calculation.
 *
 * @since 1.0.0
 *
 * @return bool True if self-referral is allowed, false otherwise.
 */
function wca_allow_self_referral(): bool {
	return wca_get_option( 'advanced', 'behavior.allow_self_referral', 'off' ) === 'on';
}

/**
 * Check if referred discount is enabled
 *
 * Determines whether referred customers receive a discount on their purchases.
 * This incentivizes customer conversion through affiliate links.
 *
 * @since 1.0.0
 *
 * @return bool True if referred discount is enabled, false otherwise.
 */
function wca_is_referred_discount_enabled(): bool {
	return wca_get_option( 'basic', 'discount.referred_discount_enabled', 'off' ) === 'on';
}

/**
 * Get dashboard page link or ID
 *
 * @since 1.0
 *
 * @param string $type Return type: 'link' or 'id'.
 *
 * @return string|int Dashboard URL or page ID
 */
function wca_get_dashboard( string $type = 'link' ) {
	$id = wca_get_dashboard_page_id();
	if ( empty( $id ) ) {
		return '';
	}

	if ( 'id' === $type ) {
		return $id;
	}

	return get_the_permalink( $id );
}

/**
 * Check if affiliates can pay with credit.
 *
 * @return bool True if pay with credit is enabled.
 */
function wca_can_pay_with_credit(): bool {
	return wca_get_option( 'advanced', 'behavior.enable_pay_with_credits', 'off' ) === 'on';
}

/**
 * Check if reCAPTCHA is enabled for my account pages.
 *
 * @return bool True if reCAPTCHA is enabled.
 */
function wca_is_recaptcha_enabled(): bool {
	return wca_get_option( 'advanced', 'my_account.recaptcha_enabled', 'off' ) === 'on';
}

/**
 * Get reCAPTCHA site key.
 *
 * @return string The reCAPTCHA site key.
 */
function wca_get_recaptcha_site_key(): string {
	return wca_get_option( 'advanced', 'my_account.sitekey_recaptcha', '' );
}

/**
 * Get minimum payout amount.
 *
 * @return float The minimum payout amount.
 */
function wca_get_minimum_payout(): float {
	return (float) wca_get_option( 'payout', 'general.minimum_payout', 50 );
}

/**
 * Get enabled payout methods.
 *
 * @return array The enabled payout methods.
 */
function wca_get_enabled_payout_methods(): array {
	return wca_get_option( 'payout', 'general.payout_methods', array() );
}

/**
 * Check if affiliate auto approval is enabled.
 *
 * @return bool True if auto approval is enabled.
 */
function wca_is_auto_approve_enabled(): bool {
	return wca_get_option( 'basic', 'general.auto_approve_affiliate', 'no' ) === 'yes';
}

/**
 * Check if external URL generation is allowed.
 *
 * @return bool True if external URL generation is allowed.
 */
function wca_is_external_url_generation_allowed(): bool {
	return wca_get_option( 'advanced', 'behavior.allow_external_url_generation', 'off' ) === 'on';
}

/**
 * Check if email validation is enabled.
 *
 * @return bool True if email validation is enabled.
 */
function wca_is_email_validation_enabled(): bool {
	return wca_get_option( 'basic', 'tracking.email_validation_enabled', 'off' ) === 'on';
}

/**
 * Check if recurring order bonus is enabled.
 *
 * @return bool True if recurring order bonus is enabled.
 */
function wca_is_recurring_order_enabled(): bool {
	return wca_get_option( 'basic', 'recurring.recurring_order_enabled', 'off' ) === 'on';
}

/**
 * Get commission base setting.
 *
 * @return string Commission base ('order_total', 'order_subtotal', or 'product_price').
 */
function wca_get_commission_base(): string {
	return wca_get_option( 'basic', 'commission.commission_base', 'order_total' );
}

/**
 * Check if commission should exclude shipping.
 *
 * @return bool True if shipping should be excluded.
 */
function wca_should_exclude_shipping(): bool {
	return wca_get_option( 'basic', 'commission.exclude_shipping', 'off' ) === 'on';
}

/**
 * Check if commission should exclude tax.
 *
 * @return bool True if tax should be excluded.
 */
function wca_should_exclude_tax(): bool {
	return wca_get_option( 'basic', 'commission.exclude_tax', 'off' ) === 'on';
}

/**
 * Check if commission should exclude fees.
 *
 * @return bool True if fees should be excluded.
 */
function wca_should_exclude_fees(): bool {
	return wca_get_option( 'basic', 'commission.exclude_fees', 'off' ) === 'on';
}

/**
 * Check if admin access should be restricted for affiliates.
 *
 * @return bool True if admin access should be restricted.
 */
function wca_should_restrict_admin_access(): bool {
	return wca_get_option( 'advanced', 'behavior.restrict_admin_access', 'on' ) === 'on';
}

/**
 * Get login page ID.
 *
 * @return int Login page ID.
 */
function wca_get_login_page_id(): int {
	return (int) wca_get_option( 'basic', 'general.login_page', 0 );
}

/**
 * Get registration page ID.
 *
 * @return int Registration page ID.
 */
function wca_get_registration_page_id(): int {
	return (int) wca_get_option( 'basic', 'general.registration_page', 0 );
}

/**
 * Get Terms & Conditions page URL.
 *
 * @return string Terms & Conditions page URL.
 */
function wca_get_terms_conditions_page_url(): string {
	return wca_get_option( 'basic', 'tracking.terms_conditions', '' );
}

/**
 * Get email validation expiry type.
 *
 * @return int Email validation expiry type.
 */
function wca_get_email_validation_expiry_type(): int {
	return (int) wca_get_option( 'basic', 'tracking.email_validation_expiry_type', 1 );
}

/**
 * Get email validation expiry unit.
 *
 * @return int Email validation expiry unit in seconds.
 */
function wca_get_email_validation_expiry_unit(): int {
	return (int) wca_get_option( 'basic', 'tracking.email_validation_expiry_unit', HOUR_IN_SECONDS );
}

/**
 * Get email validation expiry time in seconds.
 *
 * @return int Email validation expiry time in seconds.
 */
function wca_get_email_validation_expiry(): int {
	$type = wca_get_email_validation_expiry_type();
	$unit = wca_get_email_validation_expiry_unit();

	return $type * $unit;
}

/**
 * Check if commission recurring is enabled.
 *
 * @return bool True if recurring commissions are enabled.
 */
function wca_is_recurring_commission_enabled(): bool {
	return wca_get_option( 'basic', 'recurring.recurring_order_enabled', 'off' ) === 'on';
}

/**
 * Get recurring commission rate.
 *
 * @return float Recurring commission rate.
 */
function wca_get_recurring_commission_rate(): float {
	return (float) wca_get_option( 'basic', 'recurring.recurring_commission_rate', 50 );
}

/**
 * Get recurring commission type.
 *
 * @return string Recurring commission type.
 */
function wca_get_recurring_commission_type(): string {
	return wca_get_option( 'basic', 'recurring.recurring_commission_type', 'percent' );
}

/**
 * Get recurring commission amount.
 *
 * @return float Recurring commission amount.
 */
function wca_get_recurring_commission_amount(): float {
	return (float) wca_get_option( 'basic', 'recurring.recurring_commission_amount', 0 );
}

/**
 * Get admin email
 *
 * @since 1.0
 * @return string Admin email address
 */
function wca_admin_email(): string {
	return get_option( 'admin_email' );
}

/**
 * Get admin emails for notifications
 *
 * @return array Array of admin email addresses
 */
function wca_get_admin_emails(): array {
	$admin_email = wca_admin_email();

	return array( $admin_email );
}

// Email helper functions.

/**
 * Check if affiliate application email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_affiliate_application_email_enabled(): bool {
	return wca_get_option( 'email', 'affiliate.affiliate_application.affiliate_email_application_enable', 'off' ) === 'on';
}

/**
 * Get affiliate application email subject
 *
 * @return string Email subject
 */
function wca_get_affiliate_application_subject(): string {
	$data_path = 'affiliate.affiliate_application.affiliate_applied_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get affiliate application email message
 *
 * @return string Email message
 */
function wca_get_affiliate_application_message(): string {
	$data_path = 'affiliate.affiliate_application.affiliate_applied_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if admin application email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_admin_application_email_enabled(): bool {
	return wca_get_option( 'email', 'admin.admin_application.admin_application_enable', 'off' ) === 'on';
}

/**
 * Get admin application email subject
 *
 * @return string Email subject
 */
function wca_get_admin_application_subject(): string {
	$data_path = 'admin.admin_application.affiliate_application_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get admin application email message
 *
 * @return string Email message
 */
function wca_get_admin_application_message(): string {
	$data_path = 'admin.admin_application.affiliate_application_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if affiliate approval email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_affiliate_approval_email_enabled(): bool {
	return wca_get_option( 'email', 'affiliate.approval.affiliate_approved_enable', 'off' ) === 'on';
}

/**
 * Get affiliate approval email subject
 *
 * @return string Email subject
 */
function wca_get_affiliate_approval_subject(): string {
	$data_path = 'affiliate.approval.affiliate_approved_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get affiliate approval email message
 *
 * @return string Email message
 */
function wca_get_affiliate_approval_message(): string {
	$data_path = 'affiliate.approval.affiliate_approved_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if affiliate rejection email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_affiliate_rejection_email_enabled(): bool {
	return wca_get_option( 'email', 'affiliate.rejection.affiliate_rejected_enable', 'off' ) === 'on';
}

/**
 * Get affiliate rejection email subject
 *
 * @return string Email subject
 */
function wca_get_affiliate_rejection_subject(): string {
	$data_path = 'affiliate.rejection.affiliate_rejected_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get affiliate rejection email message
 *
 * @return string Email message
 */
function wca_get_affiliate_rejection_message(): string {
	$data_path = 'affiliate.rejection.affiliate_rejected_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if affiliate commission email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_affiliate_commission_email_enabled(): bool {
	return wca_get_option( 'email', 'affiliate.affiliate_commission.affiliate_commission_enable', 'off' ) === 'on';
}

/**
 * Get affiliate commission email subject
 *
 * @return string Email subject
 */
function wca_get_affiliate_commission_subject(): string {
	$data_path = 'affiliate.affiliate_commission.affiliate_commission_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get affiliate commission email message
 *
 * @return string Email message
 */
function wca_get_affiliate_commission_message(): string {
	$data_path = 'affiliate.affiliate_commission.affiliate_commission_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if admin commission email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_admin_commission_email_enabled(): bool {
	return wca_get_option( 'email', 'admin.admin_commission.admin_commission_enable', 'off' ) === 'on';
}

/**
 * Get admin commission email subject
 *
 * @return string Email subject
 */
function wca_get_admin_commission_subject(): string {
	$data_path = 'admin.admin_commission.admin_commission_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get admin commission email message
 *
 * @return string Email message
 */
function wca_get_admin_commission_message(): string {
	$data_path = 'admin.admin_commission.admin_commission_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if affiliate payout request email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_affiliate_payout_request_email_enabled(): bool {
	return wca_get_option( 'email', 'affiliate.affiliate_payout_request.affiliate_payout_request_enable', 'off' ) === 'on';
}

/**
 * Get affiliate payout request email subject
 *
 * @return string Email subject
 */
function wca_get_affiliate_payout_request_subject(): string {
	$data_path = 'affiliate.affiliate_payout_request.affiliate_payout_request_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get affiliate payout request email message
 *
 * @return string Email message
 */
function wca_get_affiliate_payout_request_message(): string {
	$data_path = 'affiliate.affiliate_payout_request.affiliate_payout_request_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if admin payout request email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_admin_payout_request_email_enabled(): bool {
	return wca_get_option( 'email', 'admin.admin_payout_request.admin_payout_request_enable', 'off' ) === 'on';
}

/**
 * Get admin payout request email subject
 *
 * @return string Email subject
 */
function wca_get_admin_payout_request_subject(): string {
	$data_path = 'admin.admin_payout_request.admin_payout_request_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get admin payout request email message
 *
 * @return string Email message
 */
function wca_get_admin_payout_request_message(): string {
	$data_path = 'admin.admin_payout_request.admin_payout_request_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if payout processed email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_payout_processed_email_enabled(): bool {
	return wca_get_option( 'email', 'admin.payout_processed.affiliate_payout_processed_enable', 'on' ) === 'on';
}

/**
 * Get payout processed email subject
 *
 * @return string Email subject
 */
function wca_get_payout_processed_subject(): string {
	$data_path = 'admin.payout_processed.affiliate_payout_processed_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get payout processed email message
 *
 * @return string Email message
 */
function wca_get_payout_processed_message(): string {
	$data_path = 'admin.payout_processed.affiliate_payout_processed_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if admin transaction email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_admin_transaction_email_enabled(): bool {
	return wca_get_option( 'email', 'admin.admin_transaction.admin_transaction_enable', 'off' ) === 'on';
}

/**
 * Get admin transaction email subject
 *
 * @return string Email subject
 */
function wca_get_admin_transaction_subject(): string {
	$data_path = 'admin.admin_transaction.admin_transaction_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get admin transaction email message
 *
 * @return string Email message
 */
function wca_get_admin_transaction_message(): string {
	$data_path = 'admin.admin_transaction.admin_transaction_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if affiliate paid referral email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_affiliate_paid_referral_email_enabled(): bool {
	return wca_get_option( 'email', 'affiliate.affiliate_paid_referral.affiliate_paid_referral_enable', 'off' ) === 'on';
}

/**
 * Get affiliate paid referral email subject
 *
 * @return string Email subject
 */
function wca_get_affiliate_paid_referral_subject(): string {
	$data_path = 'affiliate.affiliate_paid_referral.affiliate_paid_referral_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get affiliate paid referral email message
 *
 * @return string Email message
 */
function wca_get_affiliate_paid_referral_message(): string {
	$data_path = 'affiliate.affiliate_paid_referral.affiliate_paid_referral_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Check if admin paid referral email is enabled
 *
 * @return bool True if enabled
 */
function wca_is_admin_paid_referral_email_enabled(): bool {
	return wca_get_option( 'email', 'admin.admin_paid_referral.admin_paid_referral_enable', 'off' ) === 'on';
}

/**
 * Get admin paid referral email subject
 *
 * @return string Email subject
 */
function wca_get_admin_paid_referral_subject(): string {
	$data_path = 'admin.admin_paid_referral.admin_paid_referral_subject';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Get admin paid referral email message
 *
 * @return string Email message
 */
function wca_get_admin_paid_referral_message(): string {
	$data_path = 'admin.admin_paid_referral.admin_paid_referral_message';
	$defaults  = wca_get_nested_value( wca_get_default_email_templates(), $data_path );

	return wca_get_option( 'email', $data_path, $defaults );
}

/**
 * Update WC Affiliate settings with validation
 *
 * @param string $page Settings page.
 * @param string $key Settings key.
 * @param mixed  $values Values to update.
 *
 * @return Settings|WP_Error True on success, WP_Error on failure.
 */
function wca_update_settings( string $page, string $key, $values ) {
	$option_model = new Settings();

	// For single page updates, we don't have full form data for cross-page dependencies
	// so we'll validate with just the page data.
	$validation_result = wca_validate_settings_values( $values, $page, $values );
	if ( is_wp_error( $validation_result ) ) {
		return $validation_result;
	}

	// Update settings using the model.
	return $option_model->set_option( $page, $key, $values );
}

/**
 * Update multiple settings pages at once
 *
 * @param array $settings Settings array keyed by page.
 *
 * @return bool|WP_Error True on success, WP_Error on failure.
 */
function wca_update_settings_pages( array $settings ) {
	$option_model = new Settings();

	// Validate all pages with full form data for cross-page dependencies.
	foreach ( $settings as $page => $values ) {
		$validation_result = wca_validate_settings_values( $values, $page, $settings );
		if ( is_wp_error( $validation_result ) ) {
			return $validation_result;
		}
	}

	// Update all settings.
	return $option_model->update_settings( $settings );
}

/**
 * Get all settings values
 *
 * @return array All settings in nested structure.
 */
function wca_get_settings_values(): array {
	$option_model = new Settings();

	return $option_model->get_all_settings();
}

/**
 * Get default email template settings
 *
 * Retrieves the default configuration for email notification templates,
 * including subjects and messages sourced from the email settings page defaults.
 * Array keys mirror the dot-notation option paths used in wca_get_option().
 *
 * @since 1.0.0
 *
 * @return array Array of default email template configurations keyed by option path segments.
 */
function wca_get_default_email_templates(): array {
	static $templates;

	if ( ! isset( $templates ) ) {
		$templates = array(
			// --- Affiliate Emails ---
			'affiliate' => array(

				'affiliate_application'    => array(
					'affiliate_email_application_enable' => 'on',
					'affiliate_applied_subject'          => '%%site_title%% - Thanks for your interest in being an affiliate',
					'affiliate_applied_message'          => "
					Hi %%first_name%%, <br><br>
					Thanks for applying to become an affiliate for %%site_title%%. <br>
					We've received your application, and our team is currently reviewing it. You'll hear back from us soon with an update. <br>
					If you have any questions in the meantime, feel free to reply to this email.<br><br>
					— The %%site_title%% Team
				",
				),

				'approval'                 => array(
					'affiliate_approved_enable'  => 'on',
					'affiliate_approved_subject' => 'Congratulations! Your affiliate application has been approved 🎉',
					'affiliate_approved_message' => '
					Hi %%first_name%%, <br><br>
					Good news! Your affiliate application has been approved! <br>
					You can now log in to your affiliate dashboard to grab your referral links, track commissions, and manage payouts. <br>
					👉 Access your dashboard: %%dashboard_url%% <br>
					Welcome aboard, and happy promoting! <br><br>
					— The %%site_title%% Team
				',
				),

				'rejection'                => array(
					'affiliate_rejected_enable'  => 'on',
					'affiliate_rejected_subject' => '%%site_title%% - Update on your affiliate application',
					'affiliate_rejected_message' => "Hi %%first_name%%, <br />
					At first, thanks for your interest in the %%site_title%% affiliate program. <br />
					We've carefully reviewed your application and regret to inform you that we're unable to approve it at this moment. This may be due to one or more of the following reasons: <br />
					<ul>
					<li>It doesn't fully align with our affiliate guidelines</li>
					<li>Your marketing plan/style/place is not closely related to our products or audience</li>
					<li>The application appears incomplete or spammy in nature</li>
					</ul>
					Please note that this decision isn't final. You're welcome to reapply in the future if your situation changes. <br />
					We appreciate your understanding. Please contact us for further discussion. <br />
					— The %%site_title%% Team
				",
				),

				'affiliate_payout_request' => array(
					'affiliate_payout_request_enable'  => 'on',
					'affiliate_payout_request_subject' => '%%site_title%% - Payout request received',
					'affiliate_payout_request_message' => "
					Hi %%first_name%%, <br><br>
					We've received your payout request, and it's now being processed. <br>
					Request Summary:
					<ul>
					<li>Requested amount: %%amount%%</li>
					<li>Request Time: %%payout_time%%</li>
					</ul>
					Once the payment is completed, you'll receive a confirmation email. Let us know if you have any queries about this. <br>
					Thanks for being an affiliate with us. <br><br>
					— The %%site_title%% Team
				",
				),

				'affiliate_commission'     => array(
					'affiliate_commission_enable'  => 'on',
					'affiliate_commission_subject' => "%%site_title%% - You've earned a new commission 🎉",
					'affiliate_commission_message' => "
					Hi %%first_name%%, <br><br>
					Nice work! You've just earned a commission of %%commission%% on %%site_title%%. <br>
					Commission Details:
					<ul>
					<li>Commission amount: %%amount%%</li>
					<li>Order ID: %%order_id%%</li>
					<li>Earning Time: %%earning_time%%</li>
					</ul>
					You can view the details and track your earnings anytime from your affiliate dashboard.<br>
					👉 View dashboard: %%dashboard_url%% <br>
					Keep up the good work! <br><br>
					— The %%site_title%% Team
				",
				),

				'affiliate_paid_referral'  => array(
					'affiliate_paid_referral_enable'  => 'off',
					'affiliate_paid_referral_subject' => '%%site_title%% - Your referral payment has been sent',
					'affiliate_paid_referral_message' => '
					Hi %%first_name%%, <br><br>
					Good news! Your referral payment has been processed successfully.<br>
					Payment Details:
					<ul>
					<li>Payment amount: %%amount%%</li>
					<li>Order ID: %%order_id%%</li>
					<li>Processing Time: %%processing_time%%</li>
					</ul>
					You can check the payment details in your affiliate dashboard.<br>
					👉 View dashboard: %%dashboard_url%% <br>
					Note: Processing time may vary based on the payment gateway and banking system. <br>
					Thanks for being a valued affiliate. Keep pushing! <br><br>
					— The %%site_title%% Team
				',
				),
			),

			// --- Admin Emails ---
			'admin'     => array(

				'admin_application'    => array(
					'admin_application_enable'      => 'on',
					'affiliate_application_subject' => '[Action required] New affiliate application received',
					'affiliate_application_message' => "
					Hi Admin, <br>
					A new affiliate application has been submitted on %%site_title%%. <br><br>
					<strong>Applicant details:</strong>
					<ul>
					<li>Name: %%first_name%%</li>
					<li>Email: %%user_email%%</li>
					<li>Website: %%website_url%%</li>
					</ul>
					<strong>What you can do now:</strong>
					<ul>
					<li>Review the applicant's details and promotional intent</li>
					<li>Approve or reject the application</li>
					<li>Update affiliate settings if needed</li>
					</ul>
					👉 Review application: %%dashboard_url%% <br>
					Take timely actions to maintain a healthy and trustworthy affiliate network.
				",
				),

				'admin_payout_request' => array(
					'admin_payout_request_enable'  => 'on',
					'admin_payout_request_subject' => '%%first_name%% has asked for a payout [%%amount%%]',
					'admin_payout_request_message' => '
					Hi Admin, <br><br>
					An affiliate has submitted a payout request on %%site_title%%. <br><br>
					<strong>Request details:</strong>
					<ul>
					<li>Affiliate: %%first_name%%</li>
					<li>Email: %%user_email%%</li>
					<li>Requested amount: %%amount%%</li>
					<li>Request time: %%payout_time%%</li>
					</ul>
					<strong>Now you can do these:</strong>
					<ul>
					<li>Review the payout request details</li>
					<li>Verify commissions and payment method</li>
					<li>Approve and process the payout</li>
					</ul>
					👉 Review payout request: %%dashboard_url%% <br>
					Timely payouts help build trust and keep affiliates motivated.
				',
				),

				'payout_processed'     => array(
					'affiliate_payout_processed_enable'  => 'on',
					'affiliate_payout_processed_subject' => 'Payout processed for %%first_name%%',
					'affiliate_payout_processed_message' => "
					Hi Admin, <br><br>
					You've successfully processed the payout request for %%first_name%%. <br><br>
					<strong>Payment details:</strong>
					<ul>
					<li>Paid amount: %%amount%%</li>
					<li>Affiliate: %%first_name%%</li>
					<li>Processing time: %%processing_time%%</li>
					</ul>
					👉 View payout details: %%dashboard_url%% <br>
					The affiliate has also been notified automatically.
					Please keep records (invoice) for accounting purposes and keep reviewing upcoming payout requests.
				",
				),

				'admin_commission'     => array(
					'admin_commission_enable'  => 'off',
					'admin_commission_subject' => 'New referral commission earned by %%first_name%%',
					'admin_commission_message' => '
					Hi Admin, <br><br>
					A new referral commission has been earned by %%first_name%% on your store %%site_title%%. <br><br>
					<strong>Commission details:</strong>
					<ul>
					<li>Affiliate: %%first_name%%</li>
					<li>Commission amount: %%commission%%</li>
					<li>Order ID: %%order_id%%</li>
					</ul>
					👉 View commission details: %%dashboard_url%% <br><br>
					<strong>What you can do now:</strong>
					<ul>
					<li>Review the order and referral source</li>
					<li>Monitor affiliate performance</li>
					<li>Adjust commission rules if necessary</li>
					</ul>
				',
				),

				'admin_transaction'    => array(
					'admin_transaction_enable'  => 'off',
					'admin_transaction_subject' => 'New Transaction Created',
					'admin_transaction_message' => 'A new transaction has been created.',
				),

				'admin_paid_referral'  => array(
					'admin_paid_referral_enable'  => 'off',
					'admin_paid_referral_subject' => 'Referral Payment Processed',
					'admin_paid_referral_message' => 'A referral payment has been processed.',
				),
			),
		);
	}

	return apply_filters( 'wc_affiliate_default_email_templates', $templates );
}

/**
 * Get nested value from array using dot notation
 *
 * Utility function to retrieve values from nested arrays using dot notation.
 * Example: 'basic.general.platform' accesses $array['basic']['general']['platform'].
 *
 * @since 1.0.0
 *
 * @param array  $data Array to search in.
 * @param string $path Dot notation key (e.g., 'level1.level2.level3').
 *
 * @return mixed The value if found, null otherwise.
 */
function wca_get_nested_value( array $data, string $path ) {
	$keys    = explode( '.', $path );
	$current = $data;

	foreach ( $keys as $key ) {
		if ( isset( $current[ $key ] ) ) {
			$current = $current[ $key ];
		} else {
			return null;
		}
	}

	return $current;
}

/**
 * Update nested value in array using dot notation
 *
 * Updates a value in a nested array structure using dot notation path.
 * Creates intermediate arrays as needed if they don't exist.
 * Example: 'basic.general.platform' updates $array['basic']['general']['platform'].
 *
 * @since 1.0.0
 *
 * @param array  $data Array to modify (passed by reference).
 * @param string $path Dot notation key (e.g., 'level1.level2.level3').
 * @param mixed  $value Value to set at the specified path.
 *
 * @return bool True on success, false if path is empty.
 */
function wca_update_nested_value( array &$data, string $path, $value ): bool {
	if ( empty( $path ) ) {
		return false;
	}

	$keys    = explode( '.', $path );
	$current = &$data;

	// Traverse to the parent of the final key, creating arrays as needed.
	foreach ( $keys as $i => $key ) {
		// If this is the last key, set the value.
		if ( ( count( $keys ) - 1 ) === $i ) {
			$current[ $key ] = $value;

			return true;
		}

		// If the key doesn't exist or isn't an array, create it as an array.
		if ( ! isset( $current[ $key ] ) || ! is_array( $current[ $key ] ) ) {
			$current[ $key ] = array();
		}

		// Move the reference deeper into the array.
		$current = &$current[ $key ];
	}

	return false;
}

/**
 * Get legacy settings mappings for migration
 *
 * Provides a comprehensive mapping from legacy flat option keys to new hierarchical field paths.
 * Used by both installer migration and import functionality.
 *
 * @since 1.0.0
 *
 * @return array Array of legacy option mappings with filter support.
 */
function wca_get_legacy_settings_mappings(): array {
	$mappings = array(
		'wc_affiliate_basic'    => array(
			// General settings.
			'dashboard'                   => 'basic.general.dashboard_page',
			'platform'                    => 'basic.general.platform',
			'auto_approve_affiliate'      => 'basic.general.auto_approve_affiliate',
			'login_page'                  => 'basic.general.login_page',
			'registration_page'           => 'basic.general.registration_page',

			// Commission settings.
			'commission_type'             => 'basic.commission.commission_type',
			'commission_amount'           => 'basic.commission.commission_amount',
			'commission_base'             => 'basic.commission.commission_base',
			'exclude_shipping'            => 'basic.commission.exclude_shipping',
			'exclude_tax'                 => 'basic.commission.exclude_tax',
			'exclude_fees'                => 'basic.commission.exclude_fees',

			// Recurring commission settings.
			'recurring_order'             => 'basic.recurring.recurring_order_enabled',
			'recurring_commission_type'   => 'basic.recurring.recurring_commission_type',
			'recurring_commission_rate'   => 'basic.recurring.recurring_commission_rate',
			'recurring_commission_amount' => 'basic.recurring.recurring_commission_amount',
			// 'first_renewal_rate'          => 'basic.recurring.first_renewal_rate',

			// Referral status settings.
			'rf-status-for-pending'       => 'basic.referral_status.pending',
			'rf-status-for-processing'    => 'basic.referral_status.processing',
			'rf-status-for-on-hold'       => 'basic.referral_status.on_hold',
			'rf-status-for-completed'     => 'basic.referral_status.completed',
			'rf-status-for-cancelled'     => 'basic.referral_status.cancelled',
			'rf-status-for-refunded'      => 'basic.referral_status.refunded',
			'rf-status-for-failed'        => 'basic.referral_status.failed',

			// Tracking settings.
			'expiry_time'                 => 'basic.tracking.cookie_expiry_time',
			'expiry_unit'                 => 'basic.tracking.cookie_expiry_unit',
			'email_validation_enabled'    => 'basic.tracking.email_validation_enabled',
			'terms_conditions'            => 'basic.tracking.terms_conditions',

			// Email validation settings.
			'evalidation_expiary_type'    => 'basic.tracking.email_validation_expiry_type',
			'evalidation_expiary_unit'    => 'basic.tracking.email_validation_expiry_unit',

			// Legacy field name mappings.
			'terms_url'                   => 'basic.tracking.terms_conditions',

			// Legacy fields (mapped to new locations).
			'pay_with_credit'             => 'advanced.behavior.enable_pay_with_credits',
			'enable_discount'             => 'basic.discount.referred_discount_enabled',
			'discount_type'               => 'basic.discount.discount_type',
			'discount_amount'             => 'basic.discount.discount_amount',
		),
		'wc_affiliate_advanced' => array(
			// Referral settings.
			'ref_key'                        => 'advanced.referral.ref_key',
			'token_type'                     => 'advanced.referral.token_type',
			'cookie_name'                    => 'advanced.referral.cookie_name',
			'visit_cookie_name'              => 'advanced.referral.visit_cookie_name',
			'commission_tiers'               => 'advanced.referral.commission_tiers',
			'bonus_commissions'              => 'advanced.referral.bonus_commissions',

			// Behavior settings.
			'allow_overwrite'                => 'advanced.behavior.allow_overwrite',
			'credit_once'                    => 'advanced.behavior.credit_once',
			'allow_external_url_generation'  => 'advanced.behavior.allow_external_url_generation',
			'allow_self_referral'            => 'advanced.behavior.allow_self_referral',
			'restrict_admin_access'          => 'advanced.behavior.restrict_admin_access',

			// My Account settings.
			'wc_affiliate_enable_recaptcha'  => 'advanced.my_account.recaptcha_enabled',
			'wc_affiliate_sitekey_recaptcha' => 'advanced.my_account.sitekey_recaptcha',
			'wc_tab_label'                   => 'advanced.my_account.wc_tab_label',
		),
		'wc_affiliate_payout'   => array(
			'payout_amount'         => 'payout.general.minimum_payout',
			'enable_payout_methods' => 'payout.general.payout_methods',
		),
		'wc_affiliate_messages' => array(
			'register_message'               => 'messages.system.register_message',
			'update_user_message'            => 'messages.system.update_user_message',
			'register_new_affiliate_message' => 'messages.system.new_affiliate_registration_message',
			'verify_humann_message'          => 'messages.system.Human_verification_message',
			'request_payout_message'         => 'messages.financial.request_payout_message',
			'insufficient_balance_message'   => 'messages.financial.insufficient_balance_message',
		),
		'wc_affiliate_email'    => array(
			// Affiliate application emails.
			'affiliate_applied_enable'        => 'email.affiliate.affiliate_application.affiliate_email_application_enable',
			'affiliate_applied_subject'       => 'email.affiliate.affiliate_application.affiliate_applied_subject',
			'affiliate_applied_message'       => 'email.affiliate.affiliate_application.affiliate_applied_message',
			'account_approved_subject'        => 'email.affiliate.approval.affiliate_approved_subject',
			'account_approved_message'        => 'email.affiliate.approval.affiliate_approved_message',

			// Affiliate rejection emails.
			'affiliate_rejected_enable'       => 'email.affiliate.rejection.affiliate_rejected_enable',
			'affiliate_rejected_subject'      => 'email.affiliate.rejection.affiliate_rejected_subject',
			'affiliate_rejected_message'      => 'email.affiliate.rejection.affiliate_rejected_message',
			'account_reject_enable'           => 'email.affiliate.rejection.affiliate_rejected_enable',
			'account_reject_subject'          => 'email.affiliate.rejection.affiliate_rejected_subject',
			'account_reject_message'          => 'email.affiliate.rejection.affiliate_rejected_message',

			// Affiliate payout request emails.
			'request_payout_enable'           => 'email.affiliate.affiliate_payout_request.affiliate_payout_request_enable',
			'request_payout_subject'          => 'email.affiliate.affiliate_payout_request.affiliate_payout_request_subject',
			'request_payout_mail_message'     => 'email.affiliate.affiliate_payout_request.affiliate_payout_request_message',

			// Affiliate commission emails.
			'add_credit_enable'               => 'email.affiliate.affiliate_commission.affiliate_commission_enable',
			'add_credit_subject'              => 'email.affiliate.affiliate_commission.affiliate_commission_subject',
			'add_credit_message'              => 'email.affiliate.affiliate_commission.affiliate_commission_message',

			// Affiliate paid referral emails.
			'affiliate_paid_referral_enable'  => 'email.affiliate.affiliate_paid_referral.affiliate_paid_referral_enable',
			'affiliate_paid_referral_subject' => 'email.affiliate.affiliate_paid_referral.affiliate_paid_referral_subject',
			'affiliate_paid_referral_message' => 'email.affiliate.affiliate_paid_referral.affiliate_paid_referral_message',

			// Admin application emails.
			'affiliate_applied_admin_enable'  => 'email.admin.admin_application.admin_application_enable',
			'affiliate_applied_admin_subject' => 'email.admin.admin_application.affiliate_application_subject',
			'affiliate_applied_admin_message' => 'email.admin.admin_application.affiliate_application_message',

			// Admin commission emails.
			'add_credit_admin_enable'         => 'email.admin.admin_commission.admin_commission_enable',
			'add_credit_admin_subject'        => 'email.admin.admin_commission.admin_commission_subject',
			'add_credit_admin_message'        => 'email.admin.admin_commission.admin_commission_message',

			// Admin payout request emails.
			'request_payout_admin_enable'     => 'email.admin.admin_payout_request.admin_payout_request_enable',
			'request_payout_admin_subject'    => 'email.admin.admin_payout_request.admin_payout_request_subject',
			'request_payout_admin_message'    => 'email.admin.admin_payout_request.admin_payout_request_message',

			// Admin paid referral emails.
			'admin_paid_referral_enable'      => 'email.admin.admin_paid_referral.admin_paid_referral_enable',
			'admin_paid_referral_subject'     => 'email.admin.admin_paid_referral.admin_paid_referral_subject',
			'admin_paid_referral_message'     => 'email.admin.admin_paid_referral.admin_paid_referral_message',

			// Payout processed emails.
			'payout_process_enable'           => 'email.admin.payout_processed.affiliate_payout_processed_enable',
			'payout_process_subject'          => 'email.admin.payout_processed.affiliate_payout_processed_subject',
			'payout_process_message'          => 'email.admin.payout_processed.affiliate_payout_processed_message',

			// Transaction emails.
			'admin_transaction_enable'        => 'email.admin.admin_transaction.admin_transaction_enable',
			'admin_transaction_subject'       => 'email.admin.admin_transaction.admin_transaction_subject',
			'admin_transaction_message'       => 'email.admin.admin_transaction.admin_transaction_message',
		),
	);

	/**
	 * Filter the legacy settings mappings
	 *
	 * Allows third-party plugins to modify or extend the legacy settings mappings.
	 *
	 * @since 1.0.0
	 *
	 * @param array $mappings Array of legacy option mappings.
	 */
	return apply_filters( 'wc_affiliate_legacy_settings_mappings', $mappings );
}

/**
 * Validate settings values
 *
 * @param array  $values Values to validate.
 * @param string $page Settings page key.
 * @param array  $full_values Full form data for cross-page dependencies.
 *
 * @return bool|WP_Error True on success, WP_Error on failure.
 */
function wca_validate_settings_values( array $values, string $page, array $full_values = array() ) {
	$pages = wca_get_settings_pages();

	if ( ! isset( $pages[ $page ] ) ) {
		return new \WP_Error( 'invalid_settings_page', __( 'Invalid settings page', 'wc-affiliate' ) );
	}

	$page_config = $pages[ $page ];

	// Validate fields in sections: { section_id: { field_id: field_value } }.
	if ( isset( $page_config['sections'] ) ) {
		foreach ( $page_config['sections'] as $section_id => $section ) {
			if ( isset( $section['fields'] ) && isset( $values[ $section_id ] ) ) {
				foreach ( $section['fields'] as $field_key => $field_config ) {
					$value            = $values[ $section_id ][ $field_key ] ?? null;
					$validation_error = wca_validate_field_value( $value, $field_config, $full_values );
					if ( is_wp_error( $validation_error ) ) {
						return $validation_error;
					}
				}
			}
		}
	}

	return true;
}

/**
 * Get settings configuration pages
 *
 * Retrieves the complete configuration structure for all settings pages,
 * including tabs, sections, fields, and their validation rules.
 *
 * @since 1.0.0
 *
 * @return array Array of settings page configurations.
 */
function wca_get_settings_pages(): array {
	static $default_pages;

	if ( ! isset( $default_pages ) ) {
		$default_templates = wca_get_default_email_templates();

		$default_pages = array(
			'basic'               => array(
				'id'       => 'basic',
				'label'    => __( 'Basic Settings', 'wc-affiliate' ),
				'icon'     => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7.25177 3.10211C7.29309 2.66737 7.49502 2.26365 7.8181 1.96982C8.14117 1.676 8.56219 1.51318 8.99889 1.51318C9.4356 1.51318 9.85662 1.676 10.1797 1.96982C10.5028 2.26365 10.7047 2.66737 10.746 3.10211C10.7709 3.38295 10.863 3.65368 11.0146 3.89137C11.1662 4.12906 11.3729 4.32672 11.6171 4.46762C11.8613 4.60852 12.1359 4.68851 12.4175 4.70083C12.6992 4.71314 12.9797 4.65742 13.2353 4.53836C13.6321 4.3582 14.0818 4.33213 14.4968 4.46523C14.9118 4.59833 15.2624 4.88108 15.4804 5.25844C15.6984 5.63581 15.7682 6.08079 15.6763 6.50679C15.5843 6.93279 15.3371 7.30933 14.9828 7.56311C14.7521 7.725 14.5637 7.94007 14.4337 8.19013C14.3037 8.4402 14.2358 8.71789 14.2358 8.99974C14.2358 9.28158 14.3037 9.55928 14.4337 9.80934C14.5637 10.0594 14.7521 10.2745 14.9828 10.4364C15.3371 10.6902 15.5843 11.0667 15.6763 11.4927C15.7682 11.9187 15.6984 12.3637 15.4804 12.741C15.2624 13.1184 14.9118 13.4011 14.4968 13.5342C14.0818 13.6673 13.6321 13.6413 13.2353 13.4611C12.9797 13.3421 12.6992 13.2863 12.4175 13.2986C12.1359 13.311 11.8613 13.391 11.6171 13.5319C11.3729 13.6728 11.1662 13.8704 11.0146 14.1081C10.863 14.3458 10.7709 14.6165 10.746 14.8974C10.7047 15.3321 10.5028 15.7358 10.1797 16.0297C9.85662 16.3235 9.4356 16.4863 8.99889 16.4863C8.56219 16.4863 8.14117 16.3235 7.8181 16.0297C7.49502 15.7358 7.29309 15.3321 7.25177 14.8974C7.22698 14.6164 7.13484 14.3456 6.98317 14.1078C6.83149 13.87 6.62475 13.6723 6.38044 13.5314C6.13614 13.3905 5.86147 13.3105 5.5797 13.2983C5.29794 13.2861 5.01737 13.3419 4.76177 13.4611C4.36494 13.6413 3.91526 13.6673 3.50027 13.5342C3.08527 13.4011 2.73465 13.1184 2.51663 12.741C2.29862 12.3637 2.22881 11.9187 2.32079 11.4927C2.41277 11.0667 2.65997 10.6902 3.01427 10.4364C3.24498 10.2745 3.43332 10.0594 3.56334 9.80934C3.69335 9.55928 3.76123 9.28158 3.76123 8.99974C3.76123 8.71789 3.69335 8.4402 3.56334 8.19013C3.43332 7.94007 3.24498 7.725 3.01427 7.56311C2.66047 7.3092 2.41371 6.93281 2.32195 6.5071C2.23018 6.08139 2.29998 5.63677 2.51776 5.25965C2.73554 4.88253 3.08575 4.59985 3.50034 4.46654C3.91492 4.33324 4.36426 4.35884 4.76102 4.53836C5.01659 4.65742 5.29708 4.71314 5.57874 4.70083C5.86041 4.68851 6.13497 4.60852 6.37917 4.46762C6.62338 4.32672 6.83004 4.12906 6.98167 3.89137C7.1333 3.65368 7.22543 3.38295 7.25027 3.10211" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M9 11.25C10.2426 11.25 11.25 10.2426 11.25 9C11.25 7.75736 10.2426 6.75 9 6.75C7.75736 6.75 6.75 7.75736 6.75 9C6.75 10.2426 7.75736 11.25 9 11.25Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>',
				'sections' => array(
					'general'         => array(
						'id'     => 'general',
						'label'  => __( 'General', 'wc-affiliate' ),
						'fields' => array(
							'platform'               => array(
								'id'       => 'platform',
								'label'    => __( 'Platform', 'wc-affiliate' ),
								'type'     => 'select',
								'options'  => array(
									'woocommerce'  => __( 'WooCommerce', 'wc-affiliate' ),
									'easycommerce' => __( 'EasyCommerce', 'wc-affiliate' ),
								),
								'default'  => 'woocommerce',
								'required' => true,
							),
							'dashboard_page'         => array(
								'id'       => 'dashboard_page',
								'label'    => __( 'Dashboard page', 'wc-affiliate' ),
								'type'     => 'select',
								'options'  => wca_get_pages_options(),
								'default'  => 0,
								'required' => true,
							),
							'login_page'             => array(
								'id'      => 'login_page',
								'label'   => __( 'Login page', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_pages_options(),
								'default' => 0,
							),
							'registration_page'      => array(
								'id'      => 'registration_page',
								'label'   => __( 'Registration page', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_pages_options(),
								'default' => 0,
							),
							'auto_approve_affiliate' => array(
								'id'      => 'auto_approve_affiliate',
								'label'   => __( 'Auto Approve Affiliate', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => array(
									'no'  => __( 'No', 'wc-affiliate' ),
									'yes' => __( 'Yes', 'wc-affiliate' ),
								),
								'default' => 'no',
								'desc'    => __( 'Automatically approve affiliate applications.', 'wc-affiliate' ),
							),
						),
					),
					'commission'      => array(
						'id'     => 'commission',
						'label'  => __( 'Affiliate Commission', 'wc-affiliate' ),
						'fields' => array(
							'commission_base'   => array(
								'id'       => 'commission_base',
								'label'    => __( 'Commission Base', 'wc-affiliate' ),
								'type'     => 'select',
								'options'  => array(
									'order_total'    => __( 'Order Total', 'wc-affiliate' ),
									'order_subtotal' => __( 'Order Subtotal', 'wc-affiliate' ),
									'product_price'  => __( 'Product Price', 'wc-affiliate' ),
								),
								'default'  => 'order_total',
								'tooltip'  => __( 'Commission will be calculated based on the selected option.', 'wc-affiliate' ),
								'required' => true,
							),
							'commission_type'   => array(
								'id'       => 'commission_type',
								'label'    => __( 'Commission Type', 'wc-affiliate' ),
								'type'     => 'select',
								'options'  => wca_get_commission_types(),
								'default'  => 'percent',
								'tooltip'  => __( 'Commission will be calculated based on the selected option.', 'wc-affiliate' ),
								'required' => true,
							),
							'commission_amount' => array(
								'id'       => 'commission_amount',
								'label'    => __( 'Commission Amount', 'wc-affiliate' ),
								'type'     => 'number',
								'default'  => 20,
								'tooltip'  => __( 'Commission will be calculated based on the selected option.', 'wc-affiliate' ),
								'required' => true,
							),
							'exclude_shipping'  => array(
								'id'      => 'exclude_shipping',
								'label'   => __( 'Exclude Shipping', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Exclude shipping costs from commission calculation.', 'wc-affiliate' ),
							),
							'exclude_tax'       => array(
								'id'      => 'exclude_tax',
								'label'   => __( 'Exclude Tax', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Exclude tax amounts from commission calculation.', 'wc-affiliate' ),
							),
							'exclude_fees'      => array(
								'id'      => 'exclude_fees',
								'label'   => __( 'Exclude Fees', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Exclude fee amounts from commission calculation.', 'wc-affiliate' ),
							),
						),
					),
					'recurring'       => array(
						'id'     => 'recurring',
						'label'  => __( 'Recurring Commissions', 'wc-affiliate' ),
						'fields' => array(
							'recurring_order_enabled'     => array(
								'id'      => 'recurring_order_enabled',
								'label'   => __( 'Recurring Order', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'This feature requires WooCommerce Subscriptions to work with recurring orders.', 'wc-affiliate' ),
							),
							'recurring_commission_type'   => array(
								'id'      => 'recurring_commission_type',
								'label'   => __( 'Recurring Commission Type', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_commission_types(),
								'default' => 'percent',
								'tooltip' => __( 'Type of commission for recurring payments.', 'wc-affiliate' ),
							),
							'recurring_commission_rate'   => array(
								'id'         => 'recurring_commission_rate',
								'label'      => __( 'Recurring Commission Rate (%)', 'wc-affiliate' ),
								'type'       => 'number',
								'default'    => 50,
								'min'        => 0,
								'max'        => 100,
								'tooltip'    => __( 'Percentage of original commission for recurring payments.', 'wc-affiliate' ),
								'depends_on' => array(
									'field'   => 'basic.recurring.recurring_commission_type',
									'value'   => 'percent',
									'message' => __( 'recurring orders are enabled', 'wc-affiliate' ),
								),
							),
							'recurring_commission_amount' => array(
								'id'         => 'recurring_commission_amount',
								'label'      => __( 'Recurring Commission Amount', 'wc-affiliate' ),
								'type'       => 'number',
								'default'    => 0,
								'min'        => 0,
								'tooltip'    => __( 'Fixed amount for recurring commission payments.', 'wc-affiliate' ),
								'depends_on' => array(
									'field'   => 'basic.recurring.recurring_commission_type',
									'value'   => 'fixed',
									'message' => __( 'recurring orders are enabled', 'wc-affiliate' ),
								),
							),
						),
					),
					'referral_status' => array(
						'id'     => 'referral_status',
						'label'  => __( 'Referral Status', 'wc-affiliate' ),
						'fields' => array(
							'pending'    => array(
								'id'      => 'pending',
								'label'   => __( 'Pending Payment', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_referral_status_options(),
								'default' => 'pending',
								'desc'    => __( 'What should be the referral status when order status is \'Pending payment\'?', 'wc-affiliate' ),
							),
							'processing' => array(
								'id'      => 'processing',
								'label'   => __( 'Processing', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_referral_status_options(),
								'default' => 'pending',
								'desc'    => __( 'What should be the referral status when order status is \'Processing\'?', 'wc-affiliate' ),
							),
							'on_hold'    => array(
								'id'      => 'on_hold',
								'label'   => __( 'On hold', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_referral_status_options(),
								'default' => 'pending',
								'desc'    => __( 'What should be the referral status when order status is \'On hold\'?', 'wc-affiliate' ),
							),
							'completed'  => array(
								'id'      => 'completed',
								'label'   => __( 'Completed', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_referral_status_options(),
								'default' => 'unpaid',
								'desc'    => __( 'What should be the referral status when order status is \'Completed\'?', 'wc-affiliate' ),
							),
							'cancelled'  => array(
								'id'      => 'cancelled',
								'label'   => __( 'Cancelled', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_referral_status_options(),
								'default' => 'cancelled',
								'desc'    => __( 'What should be the referral status when order status is \'Cancelled\'?', 'wc-affiliate' ),
							),
							'refunded'   => array(
								'id'      => 'refunded',
								'label'   => __( 'Refunded', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_referral_status_options(),
								'default' => 'cancelled',
								'desc'    => __( 'What should be the referral status when order status is \'Refunded\'?', 'wc-affiliate' ),
							),
							'failed'     => array(
								'id'      => 'failed',
								'label'   => __( 'Failed', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => wca_get_referral_status_options(),
								'default' => 'cancelled',
								'desc'    => __( 'What should be the referral status when order status is \'Failed\'?', 'wc-affiliate' ),
							),
						),
					),
					'tracking'        => array(
						'id'     => 'tracking',
						'label'  => __( 'Others', 'wc-affiliate' ),
						'fields' => array(
							'cookie_expiry_time'       => array(
								'id'       => 'cookie_expiry_time',
								'label'    => __( 'Cookie Expiry Time', 'wc-affiliate' ),
								'type'     => 'number',
								'default'  => 30,
								'tooltip'  => __( 'Set the number of days you want to keep the cookie. Default is 1 month.', 'wc-affiliate' ),
								'required' => true,
							),
							'cookie_expiry_unit'       => array(
								'id'       => 'cookie_expiry_unit',
								'label'    => __( 'Cookie Expiry Unit', 'wc-affiliate' ),
								'type'     => 'select',
								'options'  => array(
									3600    => __( 'Hours', 'wc-affiliate' ),
									86400   => __( 'Days', 'wc-affiliate' ),
									604800  => __( 'Weeks', 'wc-affiliate' ),
									2592000 => __( 'Months', 'wc-affiliate' ),
								),
								'default'  => 2592000,
								'required' => true,
							),
							'email_validation_enabled' => array(
								'id'      => 'email_validation_enabled',
								'label'   => __( 'Email validation', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Do you want to validate email address after registration?', 'wc-affiliate' ),
							),
							'terms_conditions'         => array(
								'id'      => 'terms_conditions',
								'label'   => __( 'Terms & Conditions', 'wc-affiliate' ),
								'type'    => 'text',
								'default' => '',
								'tooltip' => __( 'Enter your terms and conditions page URL here.', 'wc-affiliate' ),
							),
						),
					),
				),
			),
			'advanced'            => array(
				'id'       => 'advanced',
				'label'    => __( 'Advanced Settings', 'wc-affiliate' ),
				'icon'     => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
								<path d="M2.58333 5.11121H4.41646C4.54356 5.64494 4.83381 6.11827 5.24134 6.45635C5.64886 6.79442 6.1504 6.97797 6.66667 6.97797C7.18293 6.97797 7.68447 6.79442 8.092 6.45635C8.49952 6.11827 8.78977 5.64494 8.91687 5.11121H15.4167C15.5714 5.11121 15.7197 5.04565 15.8291 4.92896C15.9385 4.81227 16 4.65401 16 4.48899C16 4.32396 15.9385 4.1657 15.8291 4.04901C15.7197 3.93232 15.5714 3.86676 15.4167 3.86676H8.91687C8.78977 3.33303 8.49952 2.85971 8.092 2.52163C7.68447 2.18355 7.18293 2 6.66667 2C6.1504 2 5.64886 2.18355 5.24134 2.52163C4.83381 2.85971 4.54356 3.33303 4.41646 3.86676H2.58333C2.42862 3.86676 2.28025 3.93232 2.17085 4.04901C2.06146 4.1657 2 4.32396 2 4.48899C2 4.65401 2.06146 4.81227 2.17085 4.92896C2.28025 5.04565 2.42862 5.11121 2.58333 5.11121ZM6.66667 3.24454C6.89741 3.24454 7.12297 3.31753 7.31483 3.45427C7.50669 3.59101 7.65622 3.78537 7.74453 4.01276C7.83283 4.24015 7.85593 4.49037 7.81092 4.73177C7.7659 4.97316 7.65479 5.1949 7.49162 5.36894C7.32846 5.54298 7.12058 5.6615 6.89427 5.70952C6.66796 5.75754 6.43338 5.73289 6.2202 5.6387C6.00702 5.54451 5.82481 5.38501 5.69662 5.18036C5.56842 4.97571 5.5 4.73511 5.5 4.48899C5.5 4.15894 5.62292 3.84241 5.84171 3.60903C6.0605 3.37565 6.35725 3.24454 6.66667 3.24454Z" fill="currentColor"/>
								<path d="M2.58333 10.0892H9.08312C9.21023 10.623 9.50048 11.0963 9.908 11.4344C10.3155 11.7725 10.8171 11.956 11.3333 11.956C11.8496 11.956 12.3511 11.7725 12.7587 11.4344C13.1662 11.0963 13.4564 10.623 13.5835 10.0892H15.4167C15.5714 10.0892 15.7197 10.0237 15.8291 9.90699C15.9385 9.7903 16 9.63204 16 9.46701C16 9.30199 15.9385 9.14373 15.8291 9.02704C15.7197 8.91035 15.5714 8.84479 15.4167 8.84479H13.5835C13.4564 8.31106 13.1662 7.83773 12.7587 7.49966C12.3511 7.16158 11.8496 6.97803 11.3333 6.97803C10.8171 6.97803 10.3155 7.16158 9.908 7.49966C9.50048 7.83773 9.21023 8.31106 9.08312 8.84479H2.58333C2.42862 8.84479 2.28025 8.91035 2.17085 9.02704C2.06146 9.14373 2 9.30199 2 9.46701C2 9.63204 2.06146 9.7903 2.17085 9.90699C2.28025 10.0237 2.42862 10.0892 2.58333 10.0892ZM11.3333 8.22257C11.5641 8.22257 11.7896 8.29555 11.9815 8.4323C12.1734 8.56904 12.3229 8.76339 12.4112 8.99079C12.4995 9.21818 12.5226 9.46839 12.4776 9.70979C12.4326 9.95119 12.3215 10.1729 12.1583 10.347C11.9951 10.521 11.7872 10.6395 11.5609 10.6875C11.3346 10.7356 11.1 10.7109 10.8869 10.6167C10.6737 10.5225 10.4915 10.363 10.3633 10.1584C10.2351 9.95374 10.1667 9.71314 10.1667 9.46701C10.1667 9.13697 10.2896 8.82044 10.5084 8.58706C10.7272 8.35368 11.0239 8.22257 11.3333 8.22257Z" fill="currentColor"/>
								<path d="M2.58333 15.0673H4.41646C4.54356 15.601 4.83381 16.0743 5.24134 16.4124C5.64886 16.7505 6.1504 16.934 6.66667 16.934C7.18293 16.934 7.68447 16.7505 8.092 16.4124C8.49952 16.0743 8.78977 15.601 8.91687 15.0673H15.4167C15.5714 15.0673 15.7197 15.0017 15.8291 14.885C15.9385 14.7683 16 14.6101 16 14.445C16 14.28 15.9385 14.1218 15.8291 14.0051C15.7197 13.8884 15.5714 13.8228 15.4167 13.8228H8.91687C8.78977 13.2891 8.49952 12.8158 8.092 12.4777C7.68447 12.1396 7.18293 11.9561 6.66667 11.9561C6.1504 11.9561 5.64886 12.1396 5.24134 12.4777C4.83381 12.8158 4.54356 13.2891 4.41646 13.8228H2.58333C2.42862 13.8228 2.28025 13.8884 2.17085 14.0051C2.06146 14.1218 2 14.28 2 14.445C2 14.6101 2.06146 14.7683 2.17085 14.885C2.28025 15.0017 2.42862 15.0673 2.58333 15.0673ZM6.66667 13.2006C6.89741 13.2006 7.12297 13.2736 7.31483 13.4103C7.50669 13.5471 7.65622 13.7414 7.74453 13.9688C7.83283 14.1962 7.85593 14.4464 7.81092 14.6878C7.7659 14.9292 7.65479 15.151 7.49162 15.325C7.32846 15.499 7.12058 15.6176 6.89427 15.6656C6.66796 15.7136 6.43338 15.6889 6.2202 15.5948C6.00702 15.5006 5.82481 15.3411 5.69662 15.1364C5.56842 14.9318 5.5 14.6912 5.5 14.445C5.5 14.115 5.62292 13.7985 5.84171 13.5651C6.0605 13.3317 6.35725 13.2006 6.66667 13.2006Z" fill="currentColor"/>
								</svg>',
				'sections' => array(
					'referral'   => array(
						'id'     => 'referral',
						'label'  => __( 'Referral Settings', 'wc-affiliate' ),
						'fields' => array(
							'ref_key'           => array(
								'id'       => 'ref_key',
								'label'    => __( 'Referral Key', 'wc-affiliate' ),
								'type'     => 'text',
								'default'  => 'ref',
								'required' => true,
								'tooltip'  => __( 'This key will be used to generate affiliate link. You can change it anytime.', 'wc-affiliate' ),
							),
							'cookie_name'       => array(
								'id'       => 'cookie_name',
								'label'    => __( 'Cookie Name', 'wc-affiliate' ),
								'type'     => 'text',
								'default'  => '_wc_affiliate',
								'required' => true,
								'tooltip'  => __( 'This cookie will be used to track the referral link. You can change it anytime.', 'wc-affiliate' ),
							),
							'visit_cookie_name' => array(
								'id'       => 'visit_cookie_name',
								'label'    => __( 'Visit Cookie Name', 'wc-affiliate' ),
								'type'     => 'text',
								'default'  => 'wc-affiliate-visit',
								'required' => true,
								'tooltip'  => __( 'This cookie will be used to track the referral link. You can change it anytime.', 'wc-affiliate' ),
							),
							'token_type'        => array(
								'id'      => 'token_type',
								'label'   => __( 'Token Type', 'wc-affiliate' ),
								'type'    => 'select',
								'options' => array(
									'ID'    => __( 'ID', 'wc-affiliate' ),
									'slug'  => __( 'Slug', 'wc-affiliate' ),
									'email' => __( 'Email', 'wc-affiliate' ),
									'login' => __( 'Login', 'wc-affiliate' ),
								),
								'default' => 'ID',
								'tooltip' => __( 'What should an affiliate be identified by?', 'wc-affiliate' ),
							),
						),
					),
					'my_account' => array(
						'id'     => 'my_account',
						'label'  => __( 'My Account', 'wc-affiliate' ),
						'fields' => array(
							'recaptcha_enabled' => array(
								'id'    => 'recaptcha_enabled',
								'label' => __( 'Enable reCAPTCHA', 'wc-affiliate' ),
								'type'  => 'checkbox',
								'desc'  => __( 'Enable reCAPTCHA verification for affiliate registration.', 'wc-affiliate' ),
							),
							'sitekey_recaptcha' => array(
								'id'         => 'sitekey_recaptcha',
								'label'      => __( 'reCAPTCHA Site Key', 'wc-affiliate' ),
								'type'       => 'text',
								'default'    => '',
								'tooltip'    => __( 'Enter your reCAPTCHA site key.', 'wc-affiliate' ),
								'depends_on' => array(
									'field'   => 'advanced.my_account.recaptcha_enabled',
									'value'   => 'on',
									'message' => __( 'reCAPTCHA is enabled', 'wc-affiliate' ),
								),
							),
						),
					),
					'behavior'   => array(
						'id'     => 'behavior',
						'fields' => array(
							'allow_overwrite'         => array(
								'id'      => 'allow_overwrite',
								'label'   => __( 'Allow Overwrite', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Allow latest affiliate to overwrite previous referral', 'wc-affiliate' ),
							),
							'credit_once'             => array(
								'id'      => 'credit_once',
								'label'   => __( 'Credit Once', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Give commission only once per customer', 'wc-affiliate' ),
							),
							'allow_external_url_generation' => array(
								'id'      => 'allow_external_url_generation',
								'label'   => __( 'Allow External URL Generation', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Allow external URL generation for visitor tracking.', 'wc-affiliate' ),
							),
							'allow_self_referral'     => array(
								'id'      => 'allow_self_referral',
								'label'   => __( 'Self Referral', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Allow affiliates to refer themselves', 'wc-affiliate' ),
							),
							'restrict_admin_access'   => array(
								'id'      => 'restrict_admin_access',
								'label'   => __( 'Restrict Admin Access', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'on',
								'desc'    => __( 'Prevent affiliates from accessing the WordPress admin area', 'wc-affiliate' ),
							),
							'enable_pay_with_credits' => array(
								'id'      => 'enable_pay_with_credits',
								'label'   => __( 'Enable Pay with Credits', 'wc-affiliate' ),
								'type'    => 'checkbox',
								'default' => 'off',
								'desc'    => __( 'Allow affiliates to pay for orders using their commission credits', 'wc-affiliate' ),
							),
						),
					),
				),
			),
			'payout'              => array(
				'id'       => 'payout',
				'label'    => __( 'Payout Settings', 'wc-affiliate' ),
				'icon'     => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
								<path d="M15 3.75H3C2.17157 3.75 1.5 4.42157 1.5 5.25V12.75C1.5 13.5784 2.17157 14.25 3 14.25H15C15.8284 14.25 16.5 13.5784 16.5 12.75V5.25C16.5 4.42157 15.8284 3.75 15 3.75Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M1.5 7.5H16.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								</svg>',
				'sections' => wca_get_payment_method_settings(),
			),
			'multi_level'         => array(
				'id'       => 'multi_level',
				'label'    => __( 'Multilevel Commission', 'wc-affiliate' ),
				'icon'     => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
								<path d="M12 15.75V14.25C12 13.4544 11.6839 12.6913 11.1213 12.1287C10.5587 11.5661 9.79565 11.25 9 11.25H4.5C3.70435 11.25 2.94129 11.5661 2.37868 12.1287C1.81607 12.6913 1.5 13.4544 1.5 14.25V15.75" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M12 2.34619C12.6433 2.51297 13.213 2.88864 13.6198 3.41424C14.0265 3.93984 14.2471 4.58561 14.2471 5.25019C14.2471 5.91477 14.0265 6.56055 13.6198 7.08614C13.213 7.61174 12.6433 7.98741 12 8.15419" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M16.5 15.7502V14.2502C16.4995 13.5855 16.2783 12.9397 15.871 12.4144C15.4638 11.889 14.8936 11.5138 14.25 11.3477" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M6.75 8.25C8.40685 8.25 9.75 6.90685 9.75 5.25C9.75 3.59315 8.40685 2.25 6.75 2.25C5.09315 2.25 3.75 3.59315 3.75 5.25C3.75 6.90685 5.09315 8.25 6.75 8.25Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								</svg>',
				'buttons'  => false,
				'sections' => array(
					'skeleton' => array(
						'id'              => 'skeleton',
						'type'            => 'skeleton',
						'backgroundImage' => WC_AFFILIATE_PLUGIN_URL . 'build/images/multilevel-comission-dummy.png',
						'fields'          => array(
							'title'             => array(
								'id'       => 'title',
								'label'    => __( 'Title', 'wc-affiliate' ),
								'type'     => 'heading',
								'tag'      => 'h2',
								'default'  => __( 'Unlock Multilevel Commission Features', 'wc-affiliate' ),
								'attrs'    => array(
									'headingClassName' => 'font-medium text-white',
								),
								'required' => true,
							),
							'description'       => array(
								'id'       => 'description',
								'label'    => __( 'Description', 'wc-affiliate' ),
								'type'     => 'paragraph',
								'default'  => __( 'Take your affiliate program to the next level with multilevel commission features. Earn commissions from multiple levels of referrals and maximize your affiliate network potential.', 'wc-affiliate' ),
								'attrs'    => array(
									'paragraphClassName' => 'text-sm font-normal text-white',
								),
								'required' => true,
							),
							'learn_more_button' => array(
								'id'     => 'learn_more_button',
								'label'  => __( 'Learn More Button', 'wc-affiliate' ),
								'type'   => 'button',
								'action' => 'redirect',
								'value'  => __( 'Learn More', 'wc-affiliate' ),
								'attrs'  => array(
									'data-redirect-target' => '_blank',
									'data-redirect-url'    => 'https://codexpert.io/how-does-multilevel-commission-system-work-in-wc-affiliate/',
									'className'            => 'h-[48px] w-[167px] bg-transparent border border-white rounded-sm text-sm font-medium text-white hover:bg-white hover:text-[#000000] transition-all duration-300',
									'variant'              => 'outline',
									'fieldContainerClassName' => 'inline-block mr-4',
								),
							),
							'upgrade_button'    => array(
								'id'     => 'upgrade_button',
								'label'  => __( 'Upgrade Button', 'wc-affiliate' ),
								'type'   => 'button',
								'action' => 'redirect',
								'value'  => __( 'Upgrade Now', 'wc-affiliate' ),
								'attrs'  => array(
									'data-redirect-target' => '_blank',
									'data-redirect-url'    => 'https://wcaffiliate.com/pricing/',
									'className'            => 'h-[48px] w-[204px] bg-[#FFCF5C] border border-[#FFCF5C] rounded-sm text-sm font-medium text-[#1C1C1C] hover:bg-[#FFB400] hover:border-[#FFB400] transition-all duration-300',
									'fieldContainerClassName' => 'inline-block',
								),
							),
						),
					),
				),
			),
			'messages'            => array(
				'id'       => 'messages',
				'label'    => __( 'Message', 'wc-affiliate' ),
				'icon'     => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
								<path d="M16.5 12.75C16.5 13.1478 16.342 13.5294 16.0607 13.8107C15.7794 14.092 15.3978 14.25 15 14.25H5.121C4.72321 14.2501 4.34174 14.4082 4.0605 14.6895L2.409 16.341C2.33453 16.4155 2.23965 16.4662 2.13637 16.4867C2.03308 16.5072 1.92602 16.4967 1.82873 16.4564C1.73144 16.4161 1.64828 16.3479 1.58976 16.2603C1.53125 16.1727 1.50001 16.0698 1.5 15.9645V3.75C1.5 3.35218 1.65804 2.97064 1.93934 2.68934C2.22064 2.40804 2.60218 2.25 3 2.25H15C15.3978 2.25 15.7794 2.40804 16.0607 2.68934C16.342 2.97064 16.5 3.35218 16.5 3.75V12.75Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								</svg>',
				'sections' => array(
					'system'    => array(
						'id'     => 'system',
						'label'  => __( 'System Messages', 'wc-affiliate' ),
						'fields' => array(
							'register_message'           => array(
								'id'       => 'register_message',
								'label'    => __( 'Registration Success', 'wc-affiliate' ),
								'type'     => 'textarea',
								'default'  => 'Thanks! Your application was received. We will review your account information and get back to you shortly.',
								'tooltip'  => __( 'This message will be shown when user register successfully.', 'wc-affiliate' ),
								'required' => true,
							),
							'update_user_message'        => array(
								'id'       => 'update_user_message',
								'label'    => __( 'Profile Update Message', 'wc-affiliate' ),
								'type'     => 'textarea',
								'default'  => 'Your profile has been updated successfully',
								'tooltip'  => __( 'This message will be shown when user update their profile.', 'wc-affiliate' ),
								'required' => true,
							),
							'new_affiliate_registration_message' => array(
								'id'       => 'new_affiliate_registration_message',
								'label'    => __( 'New Affiliate Registration', 'wc-affiliate' ),
								'type'     => 'textarea',
								'default'  => 'Congratulation! New affiliates has been created successfully.',
								'tooltip'  => __( 'This message will be shown when new affiliate register successfully.', 'wc-affiliate' ),
								'required' => true,
							),
							'Human_verification_message' => array(
								'id'       => 'Human_verification_message',
								'label'    => __( 'Human Verification', 'wc-affiliate' ),
								'type'     => 'textarea',
								'default'  => 'Please complete the captcha to continue.',
								'tooltip'  => __( 'This message will be shown when user need to complete captcha to continue.', 'wc-affiliate' ),
								'required' => true,
							),
						),
					),
					'financial' => array(
						'id'     => 'financial',
						'label'  => __( 'Financial Messages', 'wc-affiliate' ),
						'fields' => array(
							'request_payout_message'       => array(
								'id'       => 'request_payout_message',
								'label'    => __( 'Payout Request', 'wc-affiliate' ),
								'type'     => 'textarea',
								'default'  => 'We\'ve received your payout request. We\'ll proceed it shortly.',
								'tooltip'  => __( 'This message will be shown when user try to request payout.', 'wc-affiliate' ),
								'required' => true,
							),
							'insufficient_balance_message' => array(
								'id'       => 'insufficient_balance_message',
								'label'    => __( 'Insufficient Balance', 'wc-affiliate' ),
								'type'     => 'textarea',
								'default'  => 'Sorry! Your balance is insufficient to proceed.',
								'tooltip'  => __( 'This message will be shown when user try to request payout but their balance is insufficient.', 'wc-affiliate' ),
								'required' => true,
							),
						),
					),
				),
			),
			'email'               => array(
				'id'    => 'email',
				'label' => __( 'Email', 'wc-affiliate' ),
				'icon'  => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
							<path d="M16.5 5.25L9.75675 9.54525C9.52792 9.67816 9.268 9.74817 9.00338 9.74817C8.73875 9.74817 8.47883 9.67816 8.25 9.54525L1.5 5.25" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
							<path d="M15 3H3C2.17157 3 1.5 3.67157 1.5 4.5V13.5C1.5 14.3284 2.17157 15 3 15H15C15.8284 15 16.5 14.3284 16.5 13.5V4.5C16.5 3.67157 15.8284 3 15 3Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
							</svg>',
				'tabs'  => array(
					'affiliate' => array(
						'id'       => 'affiliate',
						'label'    => __( 'Affiliate', 'wc-affiliate' ),
						'icon'     => 'dashicons-groups',
						'sections' => array(
							'affiliate_application'    => array(
								'id'     => 'affiliate_application',
								'label'  => __( 'Affiliate Application', 'wc-affiliate' ),
								'fields' => array(
									'affiliate_email_application_enable' => array(
										'id'      => 'affiliate_email_application_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'on',
										'desc'    => __( 'Enable application email.', 'wc-affiliate' ),
									),
									'affiliate_applied_subject'          => array(
										'id'         => 'affiliate_applied_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.affiliate_application.affiliate_applied_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the user when they apply for an affiliate account.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.affiliate_application.affiliate_email_application_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate application email is enabled', 'wc-affiliate' ),
										),
									),
									'affiliate_applied_message'          => array(
										'id'         => 'affiliate_applied_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.affiliate_application.affiliate_applied_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the user when they apply for an affiliate account.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.affiliate_application.affiliate_email_application_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate application email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'approval'                 => array(
								'id'     => 'approval',
								'label'  => __( 'Approval Emails', 'wc-affiliate' ),
								'fields' => array(
									'affiliate_approved_enable'  => array(
										'id'      => 'affiliate_approved_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'on',
										'desc'    => __( 'Enable approval email.', 'wc-affiliate' ),
									),
									'affiliate_approved_subject' => array(
										'id'         => 'affiliate_approved_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.approval.affiliate_approved_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the affiliate when their application is approved.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.approval.affiliate_approved_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate approval email is enabled', 'wc-affiliate' ),
										),
									),
									'affiliate_approved_message' => array(
										'id'         => 'affiliate_approved_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.approval.affiliate_approved_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the affiliate when their application is approved.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.approval.affiliate_approved_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate approval email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'rejection'                => array(
								'id'     => 'rejection',
								'label'  => __( 'Rejection Emails', 'wc-affiliate' ),
								'fields' => array(
									'affiliate_rejected_enable'  => array(
										'id'      => 'affiliate_rejected_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'on',
										'desc'    => __( 'Enable rejection email.', 'wc-affiliate' ),
									),
									'affiliate_rejected_subject' => array(
										'id'         => 'affiliate_rejected_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.rejection.affiliate_rejected_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the admin when an affiliate application is rejected.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.rejection.affiliate_rejected_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate rejection email is enabled', 'wc-affiliate' ),
										),
									),
									'affiliate_rejected_message' => array(
										'id'         => 'affiliate_rejected_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.rejection.affiliate_rejected_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the affiliate when their application is rejected.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.rejection.affiliate_rejected_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate rejection email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'affiliate_payout_request' => array(
								'id'     => 'affiliate_payout_request',
								'label'  => __( 'Payout Request', 'wc-affiliate' ),
								'fields' => array(
									'affiliate_payout_request_enable'  => array(
										'id'      => 'affiliate_payout_request_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'on',
										'desc'    => __( 'Enable payout request email.', 'wc-affiliate' ),
									),
									'affiliate_payout_request_subject' => array(
										'id'         => 'affiliate_payout_request_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.affiliate_payout_request.affiliate_payout_request_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the affiliate when they request a payout.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%amount%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.affiliate_payout_request.affiliate_payout_request_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate payout request email is enabled', 'wc-affiliate' ),
										),
									),
									'affiliate_payout_request_message' => array(
										'id'         => 'affiliate_payout_request_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.affiliate_payout_request.affiliate_payout_request_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the affiliate when they request a payout.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%amount%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.affiliate_payout_request.affiliate_payout_request_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate payout request email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'affiliate_commission'     => array(
								'id'     => 'affiliate_commission',
								'label'  => __( 'Commission Earned', 'wc-affiliate' ),
								'fields' => array(
									'affiliate_commission_enable'  => array(
										'id'      => 'affiliate_commission_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'on',
										'desc'    => __( 'Enable commission earned email.', 'wc-affiliate' ),
									),
									'affiliate_commission_subject' => array(
										'id'         => 'affiliate_commission_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.affiliate_commission.affiliate_commission_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the affiliate when they earn a commission.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%commission%%</code>, <code>%%order_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.affiliate_commission.affiliate_commission_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate commission email is enabled', 'wc-affiliate' ),
										),
									),
									'affiliate_commission_message' => array(
										'id'         => 'affiliate_commission_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.affiliate_commission.affiliate_commission_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the affiliate when they earn a commission.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%commission%%</code>, <code>%%order_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.affiliate_commission.affiliate_commission_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate commission email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'affiliate_paid_referral'  => array(
								'id'     => 'affiliate_paid_referral',
								'label'  => __( 'Paid Referral', 'wc-affiliate' ),
								'fields' => array(
									'affiliate_paid_referral_enable'  => array(
										'id'      => 'affiliate_paid_referral_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'off',
										'desc'    => __( 'Enable paid referral email.', 'wc-affiliate' ),
									),
									'affiliate_paid_referral_subject' => array(
										'id'         => 'affiliate_paid_referral_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.affiliate_paid_referral.affiliate_paid_referral_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the affiliate when their referral is paid.', 'wc-affiliate' ),
										'desc'       => __(
											'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%referral_id%%</code>, <code>%%referral_order_id%%</code>, <code>%%referral_commission%%</code>, <code>%%referral_status%%</code>, <code>%%dashboard_url%%</code>',
											'wc-affiliate'
										),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.affiliate_paid_referral.affiliate_paid_referral_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate paid referral email is enabled', 'wc-affiliate' ),
										),
									),
									'affiliate_paid_referral_message' => array(
										'id'         => 'affiliate_paid_referral_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'affiliate.affiliate_paid_referral.affiliate_paid_referral_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the affiliate when their referral is paid.', 'wc-affiliate' ),
										'desc'       => __(
											'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%referral_id%%</code>, <code>%%referral_order_id%%</code>, <code>%%referral_commission%%</code>, <code>%%referral_status%%</code>, <code>%%dashboard_url%%</code>',
											'wc-affiliate'
										),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.affiliate.affiliate_paid_referral.affiliate_paid_referral_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'affiliate paid referral email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
						),
					),
					'admin'     => array(
						'id'       => 'admin',
						'label'    => __( 'Admin', 'wc-affiliate' ),
						'icon'     => 'dashicons-admin-users',
						'sections' => array(
							'admin_application'    => array(
								'id'     => 'admin_application',
								'label'  => __( 'Application Emails', 'wc-affiliate' ),
								'fields' => array(
									'admin_application_enable' => array(
										'id'      => 'admin_application_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'on',
										'desc'    => __( 'Enable application email.', 'wc-affiliate' ),
									),
									'affiliate_application_subject' => array(
										'id'         => 'affiliate_application_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_application.affiliate_application_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the admin when a new affiliate application is received.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_application.admin_application_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin application email is enabled', 'wc-affiliate' ),
										),
									),
									'affiliate_application_message' => array(
										'id'         => 'affiliate_application_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_application.affiliate_application_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the admin when a new affiliate application is received.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_application.admin_application_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin application email is enabled', 'wc-affiliate' ),
										),
									),
								// 'daily_summary'  => array(
								// 'id'         => 'daily_summary',
								// 'label'      => __( 'Daily Summary', 'wc-affiliate' ),
								// 'type'       => 'checkbox',
								// 'default'    => 'off',
								// 'desc'       => __( 'Enable daily summary emails.', 'wc-affiliate' ),
								// 'depends_on' => array(
								// 'field'   => 'email.admin.admin_application.admin_application_enable',
								// 'value'   => 'on',
								// 'mode'    => 'disable',
								// 'message' => __( 'admin application email is enabled', 'wc-affiliate' ),
								// ),
								// ),
								// 'weekly_reports' => array(
								// 'id'         => 'weekly_reports',
								// 'label'      => __( 'Weekly Reports', 'wc-affiliate' ),
								// 'type'       => 'checkbox',
								// 'default'    => 'off',
								// 'desc'       => __( 'Enable weekly report emails.', 'wc-affiliate' ),
								// 'depends_on' => array(
								// 'field'   => 'email.admin.admin_application.admin_application_enable',
								// 'value'   => 'on',
								// 'mode'    => 'disable',
								// 'message' => __( 'admin application email is enabled', 'wc-affiliate' ),
								// ),
								// ),
								),
							),
							'admin_payout_request' => array(
								'id'     => 'admin_payout_request',
								'label'  => __( 'Payout Request', 'wc-affiliate' ),
								'fields' => array(
									'admin_payout_request_enable'  => array(
										'id'      => 'admin_payout_request_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'on',
										'desc'    => __( 'Enable payout request email.', 'wc-affiliate' ),
									),
									'admin_payout_request_subject' => array(
										'id'         => 'admin_payout_request_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_payout_request.admin_payout_request_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the affiliate when they request a payout.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%amount%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_payout_request.admin_payout_request_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin payout request email is enabled', 'wc-affiliate' ),
										),
									),
									'admin_payout_request_message' => array(
										'id'         => 'admin_payout_request_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_payout_request.admin_payout_request_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the affiliate when they request a payout.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%amount%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_payout_request.admin_payout_request_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin payout request email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'payout_processed'     => array(
								'id'     => 'payout_processed',
								'label'  => __( 'Payout Processed', 'wc-affiliate' ),
								'fields' => array(
									'affiliate_payout_processed_enable'  => array(
										'id'      => 'affiliate_payout_processed_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'on',
										'desc'    => __( 'Enable payout processed email.', 'wc-affiliate' ),
									),
									'affiliate_payout_processed_subject' => array(
										'id'         => 'affiliate_payout_processed_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'admin.payout_processed.affiliate_payout_processed_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the affiliate when they request a payout.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%amount%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.payout_processed.affiliate_payout_processed_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'payout processed email is enabled', 'wc-affiliate' ),
										),
									),
									'affiliate_payout_processed_message' => array(
										'id'         => 'affiliate_payout_processed_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'admin.payout_processed.affiliate_payout_processed_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the affiliate when they request a payout.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%amount%%</code>, <code>%%dashboard_url%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.payout_processed.affiliate_payout_processed_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'payout processed email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'admin_commission'     => array(
								'id'     => 'admin_commission',
								'label'  => __( 'Commission Earned', 'wc-affiliate' ),
								'fields' => array(
									'admin_commission_enable'  => array(
										'id'      => 'admin_commission_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'off',
										'desc'    => __( 'Enable commission earned email.', 'wc-affiliate' ),
									),
									'admin_commission_subject' => array(
										'id'         => 'admin_commission_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_commission.admin_commission_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the affiliate when they earn a commission.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%commission%%</code>, <code>%%order_id%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_commission.admin_commission_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin commission email is enabled', 'wc-affiliate' ),
										),
									),
									'admin_commission_message' => array(
										'id'         => 'admin_commission_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_commission.admin_commission_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the affiliate when they earn a commission.', 'wc-affiliate' ),
										'desc'       => __( 'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%commission%%</code>, <code>%%order_id%%</code>', 'wc-affiliate' ),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_commission.admin_commission_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin commission email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'admin_transaction'    => array(
								'id'     => 'admin_transaction',
								'label'  => __( 'Transaction Created', 'wc-affiliate' ),
								'fields' => array(
									'admin_transaction_enable'  => array(
										'id'      => 'admin_transaction_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'off',
										'desc'    => __( 'Enable transaction created email.', 'wc-affiliate' ),
									),
									'admin_transaction_subject' => array(
										'id'         => 'admin_transaction_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_transaction.admin_transaction_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the admin when a new transaction is created.', 'wc-affiliate' ),
										'desc'       => __(
											'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%transaction_id%%</code>, <code>%%transaction_type%%</code>, <code>%%transaction_amount%%</code>, <code>%%transaction_status%%</code>',
											'wc-affiliate'
										),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_transaction.admin_transaction_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin transaction email is enabled', 'wc-affiliate' ),
										),
									),
									'admin_transaction_message' => array(
										'id'         => 'admin_transaction_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_transaction.admin_transaction_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the admin when a new transaction is created.', 'wc-affiliate' ),
										'desc'       => __(
											'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%transaction_id%%</code>, <code>%%transaction_type%%</code>, <code>%%transaction_amount%%</code>, <code>%%transaction_status%%</code>',
											'wc-affiliate'
										),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_transaction.admin_transaction_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin transaction email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
							'admin_paid_referral'  => array(
								'id'     => 'admin_paid_referral',
								'label'  => __( 'Paid Referral', 'wc-affiliate' ),
								'fields' => array(
									'admin_paid_referral_enable'  => array(
										'id'      => 'admin_paid_referral_enable',
										'label'   => __( 'Enable', 'wc-affiliate' ),
										'type'    => 'checkbox',
										'default' => 'off',
										'desc'    => __( 'Enable paid referral email.', 'wc-affiliate' ),
									),
									'admin_paid_referral_subject' => array(
										'id'         => 'admin_paid_referral_subject',
										'label'      => __( 'Subject', 'wc-affiliate' ),
										'type'       => 'text',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_paid_referral.admin_paid_referral_subject' ),
										'tooltip'    => __( 'This subject will be used in the email sent to the admin when a referral payment is processed.', 'wc-affiliate' ),
										'desc'       => __(
											'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%referral_id%%</code>, <code>%%referral_order_id%%</code>, <code>%%referral_commission%%</code>, <code>%%referral_status%%</code>',
											'wc-affiliate'
										),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_paid_referral.admin_paid_referral_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin paid referral email is enabled', 'wc-affiliate' ),
										),
									),
									'admin_paid_referral_message' => array(
										'id'         => 'admin_paid_referral_message',
										'label'      => __( 'Message', 'wc-affiliate' ),
										'type'       => 'editor',
										'default'    => wca_get_nested_value( $default_templates, 'admin.admin_paid_referral.admin_paid_referral_message' ),
										'tooltip'    => __( 'This message will be used in the email sent to the admin when a referral payment is processed.', 'wc-affiliate' ),
										'desc'       => __(
											'Available placeholders: <code>%%site_title%%</code>, <code>%%first_name%%</code>, <code>%%last_name%%</code>, <code>%%user_email%%</code>, <code>%%display_name%%</code>, <code>%%user_id%%</code>, <code>%%referral_id%%</code>, <code>%%referral_order_id%%</code>, <code>%%referral_commission%%</code>, <code>%%referral_status%%</code>',
											'wc-affiliate'
										),
										'required'   => true,
										'depends_on' => array(
											'field'   => 'email.admin.admin_paid_referral.admin_paid_referral_enable',
											'value'   => 'on',
											'mode'    => 'disable',
											'message' => __( 'admin paid referral email is enabled', 'wc-affiliate' ),
										),
									),
								),
							),
						),
					),
				),
			),
			'cross_domain_cookie' => array(
				'id'       => 'cross_domain_cookie',
				'label'    => __( 'xDomain Cookie', 'wc-affiliate' ),
				'icon'     => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
								<g clip-path="url(#clip0_4301_4797)">
								<path d="M9 1.5C7.51664 1.5 6.0666 1.93987 4.83323 2.76398C3.59986 3.58809 2.63856 4.75943 2.07091 6.12987C1.50325 7.50032 1.35472 9.00832 1.64411 10.4632C1.9335 11.918 2.64781 13.2544 3.6967 14.3033C4.7456 15.3522 6.08197 16.0665 7.53683 16.3559C8.99168 16.6453 10.4997 16.4968 11.8701 15.9291C13.2406 15.3614 14.4119 14.4001 15.236 13.1668C16.0601 11.9334 16.5 10.4834 16.5 9C15.9788 9.1605 15.4236 9.17587 14.8943 9.04445C14.365 8.91304 13.8815 8.63981 13.4958 8.25416C13.1102 7.86851 12.837 7.38503 12.7056 6.8557C12.5741 6.32638 12.5895 5.77124 12.75 5.25C12.2288 5.4105 11.6736 5.42587 11.1443 5.29445C10.615 5.16304 10.1315 4.88981 9.74584 4.50416C9.36019 4.11851 9.08696 3.63503 8.95555 3.1057C8.82414 2.57638 8.8395 2.02124 9 1.5Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M6.375 6.375V6.3825" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M12 11.625V11.6325" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M9 9V9.0075" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M8.25 12.75V12.7575" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M5.25 10.5V10.5075" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								</g>
								<defs>
								<clipPath id="clip0_4301_4797">
								<rect width="18" height="18" fill="white"/>
								</clipPath>
								</defs>
								</svg>',
				'buttons'  => false,
				'sections' => array(
					'skeleton' => array(
						'id'              => 'skeleton',
						'type'            => 'skeleton',
						'backgroundImage' => WC_AFFILIATE_PLUGIN_URL . 'build/images/xDomain-Cookie.png',
						'fields'          => array(
							'title'             => array(
								'id'       => 'title',
								'label'    => __( 'Title', 'wc-affiliate' ),
								'type'     => 'heading',
								'tag'      => 'h2',
								'default'  => __( 'Unlock Cross-Domain Cookie Features', 'wc-affiliate' ),
								'attrs'    => array(
									'headingClassName' => 'font-medium text-white',
								),
								'required' => true,
							),
							'description'       => array(
								'id'       => 'description',
								'label'    => __( 'Description', 'wc-affiliate' ),
								'type'     => 'paragraph',
								'default'  => __( 'Enable seamless affiliate tracking across multiple domains with our advanced cross-domain cookie system. Track referrals and commissions across your entire network of websites.', 'wc-affiliate' ),
								'attrs'    => array(
									'paragraphClassName' => 'text-sm font-normal text-white',
								),
								'required' => true,
							),
							'learn_more_button' => array(
								'id'     => 'learn_more_button',
								'label'  => __( 'Learn More Button', 'wc-affiliate' ),
								'type'   => 'button',
								'action' => 'redirect',
								'value'  => __( 'Learn More', 'wc-affiliate' ),
								'attrs'  => array(
									'data-redirect-target' => '_blank',
									'data-redirect-url'    => 'https://codexpert.io/cross-domain-cookie-tracking/',
									'className'            => 'h-[48px] w-[167px] bg-transparent border border-white rounded-sm text-sm font-medium text-white hover:bg-white hover:text-[#000000] transition-all duration-300',
									'variant'              => 'outline',
									'fieldContainerClassName' => 'inline-block mr-4',
								),
							),
							'upgrade_button'    => array(
								'id'     => 'upgrade_button',
								'label'  => __( 'Upgrade Button', 'wc-affiliate' ),
								'type'   => 'button',
								'action' => 'redirect',
								'value'  => __( 'Upgrade Now', 'wc-affiliate' ),
								'attrs'  => array(
									'data-redirect-target' => '_blank',
									'data-redirect-url'    => 'https://wcaffiliate.com/pricing/',
									'className'            => 'h-[48px] w-[204px] bg-[#FFCF5C] border border-[#FFCF5C] rounded-sm text-base font-medium text-[#1C1C1C] hover:bg-[#FFB400] hover:border-[#FFB400] transition-all duration-300',
									'fieldContainerClassName' => 'inline-block',
								),
							),
						),
					),
				),
			),
			'short_links'         => array(
				'id'       => 'short_links',
				'label'    => __( 'Shortlinks', 'wc-affiliate' ),
				'icon'     => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
								<g clip-path="url(#clip0_4301_4808)">
								<path d="M7.5 9.74997C7.82209 10.1806 8.23302 10.5369 8.70491 10.7947C9.17681 11.0525 9.69863 11.2058 10.235 11.2442C10.7713 11.2826 11.3097 11.2052 11.8135 11.0173C12.3173 10.8294 12.7748 10.5353 13.155 10.155L15.405 7.90497C16.0881 7.19772 16.4661 6.25046 16.4575 5.26722C16.449 4.28398 16.0546 3.34343 15.3593 2.64815C14.664 1.95287 13.7235 1.55849 12.7403 1.54995C11.757 1.5414 10.8098 1.91938 10.1025 2.60247L8.8125 3.88497" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								<path d="M10.5006 8.24992C10.1785 7.81933 9.76762 7.46304 9.29573 7.20522C8.82383 6.9474 8.30201 6.79409 7.76565 6.75567C7.22929 6.71726 6.69095 6.79465 6.18713 6.98259C5.68331 7.17053 5.2258 7.46462 4.84564 7.84492L2.59564 10.0949C1.91255 10.8022 1.53457 11.7494 1.54311 12.7327C1.55165 13.7159 1.94604 14.6565 2.64132 15.3517C3.3366 16.047 4.27715 16.4414 5.26038 16.45C6.24362 16.4585 7.19088 16.0805 7.89814 15.3974L9.18064 14.1149" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
								</g>
								<defs>
								<clipPath id="clip0_4301_4808">
								<rect width="18" height="18" fill="white"/>
								</clipPath>
								</defs>
								</svg>',
				'buttons'  => false,
				'sections' => array(
					'skeleton' => array(
						'id'              => 'skeleton',
						'type'            => 'skeleton',
						'backgroundImage' => WC_AFFILIATE_PLUGIN_URL . 'build/images/short-link.png',
						'fields'          => array(
							'title'             => array(
								'id'       => 'title',
								'label'    => __( 'Title', 'wc-affiliate' ),
								'type'     => 'heading',
								'tag'      => 'h2',
								'default'  => __( 'Unlock Shortlinks Features', 'wc-affiliate' ),
								'attrs'    => array(
									'headingClassName' => 'font-medium text-white',
								),
								'required' => true,
							),
							'description'       => array(
								'id'       => 'description',
								'label'    => __( 'Description', 'wc-affiliate' ),
								'type'     => 'paragraph',
								'default'  => __( 'Create clean, professional affiliate links with our advanced shortlink system. Track clicks, conversions, and performance with custom branded URLs that your affiliates will love to share.', 'wc-affiliate' ),
								'attrs'    => array(
									'paragraphClassName' => 'text-sm font-normal text-white',
								),
								'required' => true,
							),
							'learn_more_button' => array(
								'id'     => 'learn_more_button',
								'label'  => __( 'Learn More Button', 'wc-affiliate' ),
								'type'   => 'button',
								'action' => 'redirect',
								'value'  => __( 'Learn More', 'wc-affiliate' ),
								'attrs'  => array(
									'data-redirect-target' => '_blank',
									'data-redirect-url'    => 'https://codexpert.io/affiliate-short-links/',
									'className'            => 'h-[48px] w-[167px] bg-transparent border border-white rounded-sm text-sm font-medium text-white hover:bg-white hover:text-[#000000] transition-all duration-300',
									'variant'              => 'outline',
									'fieldContainerClassName' => 'inline-block mr-4',
								),
							),
							'upgrade_button'    => array(
								'id'     => 'upgrade_button',
								'label'  => __( 'Upgrade Button', 'wc-affiliate' ),
								'type'   => 'button',
								'action' => 'redirect',
								'value'  => __( 'Upgrade Now', 'wc-affiliate' ),
								'attrs'  => array(
									'data-redirect-target' => '_blank',
									'data-redirect-url'    => 'https://wcaffiliate.com/pricing/',
									'className'            => 'h-[48px] w-[204px] bg-[#FFCF5C] border border-[#FFCF5C] rounded-sm text-base font-medium text-[#1C1C1C] hover:bg-[#FFB400] hover:border-[#FFB400] transition-all duration-300',
									'fieldContainerClassName' => 'inline-block',
								),
							),
						),
					),
				),
			),
			'export'              => array(
				'id'      => 'export',
				'label'   => __( 'Export', 'wc-affiliate' ),
				'icon'    => '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7 19.6302C6.00798 19.6276 5.05718 19.2331 4.35478 18.5325C3.65237 17.832 3.25527 16.8823 3.25 15.8902V13.3902C3.24998 13.1904 3.32868 12.9986 3.46905 12.8564C3.60942 12.7141 3.80017 12.6329 4 12.6302C4.19811 12.6328 4.38737 12.7127 4.52747 12.8528C4.66756 12.9929 4.74741 13.1821 4.75 13.3802V15.8802C4.75522 16.4754 4.99395 17.0446 5.41478 17.4655C5.83561 17.8863 6.40488 18.125 7 18.1302H17C17.295 18.1302 17.5871 18.072 17.8595 17.9588C18.132 17.8456 18.3793 17.6797 18.5875 17.4706C18.7956 17.2616 18.9604 17.0135 19.0724 16.7405C19.1844 16.4676 19.2413 16.1752 19.24 15.8802V13.3802C19.2344 13.278 19.2501 13.1758 19.2861 13.08C19.322 12.9842 19.3775 12.8968 19.4489 12.8235C19.5203 12.7502 19.6062 12.6925 19.701 12.6541C19.7959 12.6157 19.8977 12.5973 20 12.6002C20.0988 12.5961 20.1973 12.6123 20.2895 12.6479C20.3817 12.6835 20.4656 12.7377 20.536 12.8071C20.6063 12.8765 20.6617 12.9597 20.6985 13.0514C20.7353 13.1431 20.7528 13.2414 20.75 13.3402V15.8402C20.7526 16.3339 20.6575 16.8232 20.4701 17.2799C20.2827 17.7367 20.0068 18.1518 19.6582 18.5013C19.3095 18.8509 18.8952 19.1279 18.4389 19.3165C17.9827 19.5052 17.4937 19.6016 17 19.6002H7V19.6302Z" fill="currentColor"/>
<path d="M12.0001 16.1202C11.802 16.1176 11.6127 16.0377 11.4726 15.8977C11.3325 15.7576 11.2527 15.5683 11.2501 15.3702V6.37019C11.2487 6.27132 11.2672 6.17319 11.3044 6.08159C11.3416 5.98999 11.3968 5.90677 11.4667 5.83686C11.5367 5.76694 11.6199 5.71175 11.7115 5.67454C11.8031 5.63732 11.9012 5.61884 12.0001 5.62019C12.1973 5.62017 12.3865 5.69781 12.5269 5.83631C12.6673 5.97481 12.7474 6.16301 12.7501 6.36019V15.3602C12.7501 15.56 12.6714 15.7518 12.531 15.8941C12.3906 16.0363 12.1999 16.1176 12.0001 16.1202Z" fill="currentColor"/>
<path d="M7.74 10.1302C7.59381 10.1256 7.45206 10.079 7.33175 9.99579C7.21143 9.91262 7.11767 9.7965 7.06172 9.66137C7.00577 9.52623 6.98999 9.37782 7.0163 9.23395C7.0426 9.09007 7.10986 8.95684 7.21 8.85024L11.44 4.59024C11.5836 4.45155 11.7754 4.37402 11.975 4.37402C12.1746 4.37402 12.3664 4.45155 12.51 4.59024L16.76 8.82024C16.8925 8.96242 16.9646 9.15046 16.9612 9.34476C16.9578 9.53906 16.879 9.72445 16.7416 9.86186C16.6042 9.99928 16.4188 10.078 16.2245 10.0814C16.0302 10.0848 15.8422 10.0127 15.7 9.88024L12 6.18024L8.28 9.90024C8.20974 9.97246 8.12581 10.03 8.0331 10.0695C7.9404 10.109 7.84077 10.1296 7.74 10.1302Z" fill="currentColor"/>
</svg>
',
				'buttons' => false,
				'dynamic' => true,
			),
		);
	}

	return apply_filters( 'wc_affiliate_settings_pages', $default_pages );
}

/**
 * Validate a single field value with support for conditional requirements
 *
 * Performs validation on individual field values based on field type,
 * required status, and conditional dependencies (depends_on).
 *
 * @since 1.0.0
 *
 * @param mixed $value The field value to validate.
 * @param array $field_config The field configuration array containing validation rules.
 * @param array $all_values All form values for dependency checking.
 *
 * @return true|WP_Error True on success, WP_Error on validation failure.
 */
function wca_validate_field_value( $value, array $field_config, array $all_values = array() ) {
	if ( isset( $field_config['depends_on'] ) ) {
		$depends_on = $field_config['depends_on'];

		// Check if field should be disabled based on depends_on (skip validation for disabled fields).
		// if ( isset( $depends_on['mode'] ) && 'disable' === $depends_on['mode'] ) {
		// if ( isset( $depends_on['field'] ) && isset( $depends_on['value'] ) ) {
		// Check if the parent field has the required value.
		// $parent_value = wca_get_nested_value( $all_values, $depends_on['field'] );
		//
		// error_log( 'wca_validate_field_value: parent_value ' . print_r( $parent_value, true ) );
		//
		// Handle array values (like payment methods which are stored as arrays).
		// if ( is_array( $parent_value ) ) {
		// $condition_met = in_array( $depends_on['value'], $parent_value, true );
		// } else {
		// $condition_met = $parent_value === $depends_on['value'];
		// }
		//
		// If condition is NOT met (field should be disabled), skip validation.
		// if ( ! $condition_met ) {
		// return true;
		// }
		// }
		// }

		// Check conditional requirements based on depends_on.
		if ( isset( $depends_on['field'] ) && isset( $depends_on['value'] ) ) {
			// Check if the parent field has the required value.
			$parent_value = wca_get_nested_value( $all_values, $depends_on['field'] );

			// Handle array values (like payment methods which are stored as arrays).
			if ( is_array( $parent_value ) ) {
				$condition_met = in_array( $depends_on['value'], $parent_value, true );
			} else {
				$condition_met = $parent_value === $depends_on['value'];
			}

			// If condition is NOT met (the field should be disabled or hidden), skip validation.
			if ( ! $condition_met ) {
				return true;
			}

			if ( empty( $value ) ) {
				return new WP_Error(
					'validation_error',
					// translators: %1$s: Field label, %2$s: Condition message.
					sprintf( __( 'Field "%1$s" is required when %2$s', 'wc-affiliate' ), $field_config['label'], $depends_on['message'] ?? 'conditions are met' )
				);
			}
		}
	}

	// Check required fields.
	if ( isset( $field_config['required'] ) && $field_config['required'] && empty( $value ) ) {
		return new WP_Error(
			'validation_error',
			// translators: %s: Field label.
			sprintf( __( 'Field "%s" is required', 'wc-affiliate' ), $field_config['label'] )
		);
	}

	// Type validation.
	if ( ! empty( $value ) ) {
		switch ( $field_config['type'] ) {
			case 'number':
				if ( ! is_numeric( $value ) ) {
					return new WP_Error(
						'validation_error',
						// translators: %s: Field label.
						sprintf( __( 'Field "%s" must be a number', 'wc-affiliate' ), $field_config['label'] )
					);
				}
				break;
			case 'email':
				if ( ! is_email( $value ) ) {
					return new WP_Error(
						'validation_error',
						// translators: %s: Field label.
						sprintf( __( 'Field "%s" must be a valid email', 'wc-affiliate' ), $field_config['label'] )
					);
				}
				break;
			case 'url':
				if ( ! filter_var( $value, FILTER_VALIDATE_URL ) ) {
					return new WP_Error(
						'validation_error',
						// translators: %s: Field label.
						sprintf( __( 'Field "%s" must be a valid URL', 'wc-affiliate' ), $field_config['label'] )
					);
				}
				break;
		}
	}

	return true;
}

/**
 * Get import/export data type options
 *
 * Retrieves the available data types that can be selected for
 * import or export operations, excluding system-specific types.
 *
 * @since 1.0.0
 *
 * @return array Array of data type names suitable for import/export.
 */
function wca_get_import_export_options(): array {
	$data_types = wca_get_affiliate_data_types();

	// Extract names from data types, excluding 'settings' and 'complete' for import/export.
	$options = array_map( fn( $data_type ) => $data_type['name'], $data_types );

	return apply_filters( 'wc_affiliate_import_export_options', $options );
}

/**
 * Get referral status options for settings
 *
 * Retrieves all available referral status values that can be used
 * for filtering, sorting, and status management in the system.
 *
 * @since 1.0.0
 *
 * @return array Array of referral status options with labels.
 */
function wca_get_referral_status_options(): array {
	return apply_filters(
		'wc_affiliate_referral_status_options',
		array(
			'pending'   => __( 'Pending', 'wc-affiliate' ),
			'unpaid'    => __( 'Unpaid', 'wc-affiliate' ),
			'paid'      => __( 'Paid', 'wc-affiliate' ),
			'rejected'  => __( 'Rejected', 'wc-affiliate' ),
			'cancelled' => __( 'Cancelled', 'wc-affiliate' ),
		)
	);
}

/**
 * Get payment method settings configuration
 *
 * Retrieves the settings structure for configuring affiliate payout methods,
 * including minimum payout thresholds and method-specific configuration fields.
 *
 * @since 1.0.0
 *
 * @return array Array of payment method settings configurations with sections and fields.
 */
function wca_get_payment_method_settings(): array {
	$payment_methods = wca_get_payment_methods_options();
	$defaults        = array(
		'general' => array(
			'id'     => 'general',
			'label'  => __( 'General Payout Settings', 'wc-affiliate' ),
			'fields' => array(
				'minimum_payout' => array(
					'id'          => 'minimum_payout',
					'label'       => __( 'Minimum Payout Amount', 'wc-affiliate' ),
					'description' => __( 'The minimum commission balance an affiliate must accumulate before they can request a payout.', 'wc-affiliate' ),
					'type'        => 'number',
					'default'     => 50,
					'required'    => true,
					'attrs'       => array(
						'min'  => 0,
						'step' => 0.01,
					),
				),
				'payout_methods' => array(
					'id'          => 'payout_methods',
					'label'       => __( 'Payout Methods', 'wc-affiliate' ),
					'description' => __( 'Select which payment methods are available for affiliate payouts. Affiliates can choose from enabled methods.', 'wc-affiliate' ),
					'type'        => 'checkbox',
					'options'     => $payment_methods,
					'default'     => array(),
					'multiple'    => true,
				),
			),
		),
	);

	foreach ( $payment_methods as $payment_method => $payment_method_label ) {
		if ( 'manual' === $payment_method ) {
			continue;
		}

		$settings = array(
			'id'     => $payment_method,
			'label'  => $payment_method_label,
			'fields' => array(),
		);

		$defaults[ $payment_method ] = apply_filters( 'wc_affiliate_payout_method_settings_' . $payment_method, $settings );
	}

	return apply_filters( 'wc_affiliate_payout_method_settings', $defaults );
}
