<?php
/**
 * Trait for context-aware condition checks.
 *
 * Provides a shared is_context_allowed() implementation for any class
 * that implements ConditionInterface::get_supported_contexts().
 *
 * @package     EDD\Pro\Emails\ConditionalTags\Conditions\Traits
 * @copyright   Copyright (c) 2026, Sandhills Development, LLC
 * @license     https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.6.6
 */

namespace EDD\Pro\Emails\ConditionalTags\Conditions\Traits;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use EDD\Pro\Emails\ConditionalTags\Utilities\Tags;

/**
 * Trait ContextAware
 *
 * @since 3.6.6
 */
trait ContextAware {

	/**
	 * Check whether this condition can run in the given context.
	 *
	 * Returns true if:
	 *   - The condition has no context restrictions (empty supported contexts).
	 *   - The current context is empty (no filtering).
	 *   - The current context is in the supported contexts list.
	 *
	 * @since 3.6.6
	 *
	 * @param mixed $context Email context.
	 * @return bool
	 */
	protected function is_context_allowed( $context ) {
		$supported = $this->get_supported_contexts();
		if ( empty( $supported ) ) {
			return true;
		}

		$context = Tags::normalize_context( $context );
		if ( '' === $context ) {
			return true;
		}

		return in_array( $context, (array) $supported, true );
	}
}
