<?php
/**
 * @license GPL-2.0-or-later
 *
 * Modified by gravityview on 12-September-2025 using {@see https://github.com/BrianHenryIE/strauss}.
 */

namespace GravityKit\GravityView\Foundation\Licenses;

use Exception;
use GravityKit\GravityView\Foundation\Helpers\Arr;
use GravityKit\GravityView\Foundation\Helpers\Core as CoreHelpers;

/**
 * Class for managing product dependencies.
 */
class ProductDependencyChecker {
	const FAILURE_NOT_FOUND               = 'not_found';
	const FAILURE_NOT_INSTALLED           = 'not_installed';
	const FAILURE_INACTIVE                = 'inactive';
	const FAILURE_OLDER_VERSION           = 'older_version';
	const FAILURE_UNLICENSED              = 'unlicensed';
	const FAILURE_NO_DOWNLOAD_LINK        = 'no_download_link';
	const FAILURE_CIRCULAR_REFERENCE      = 'circular_reference';
	const FAILURE_UNMET_DEPENDENCY        = 'unmet_dependency';
	const FAILURE_UNKNOWN_PRODUCT_ACTION  = 'unknown_product_action';
	const FAILURE_MISSING_DEPENDENCIES    = 'missing_dependencies';
	const FAILURE_MISSING_PRODUCT_VERSION = 'missing_product_version';
	const ACTION_UPDATE                   = 'update';
	const ACTION_ACTIVATE                 = 'activate';

	/**
	 * Array of products data.
	 *
	 * @since 1.2.0
	 *
	 * @see   ProductManager::get_products_data()
	 *
	 * @var array
	 */
	private $products;

	/**
	 * Cache for dependency check results to avoid redundant checks.
	 *
	 * @since 1.3.0
	 *
	 * @var array
	 */
	private $dependency_cache = [];

	/**
	 * Whether to use caching for dependency checks.
	 *
	 * @since 1.3.0
	 *
	 * @var bool
	 */
	private $use_cache;

	/**
	 * Class constructor.
	 *
	 * @since 1.2.0
	 * @since 1.3.0 Added $use_cache parameter.
	 *
	 * @param array $products  Products data. {@see ProductManager::get_product_schema()}.
	 * @param bool  $use_cache (optional) Whether to use caching for dependency checks. Default: true.
	 */
	public function __construct( array $products = [], bool $use_cache = true ) {
		$this->products  = $products;
		$this->use_cache = $use_cache;
	}

	/**
	 * Clears the dependency cache.
	 *
	 * @since 1.3.0
	 *
	 * @return void
	 */
	public function clear_cache(): void {
		$this->dependency_cache = [];
	}

	/**
	 * Gets a cached dependency result or null if not cached.
	 *
	 * @since 1.3.0
	 *
	 * @param string $product_text_domain The product text domain.
	 * @param mixed  $product_version     The product version.
	 *
	 * @return array|null
	 */
	private function get_cached_dependency_result( string $product_text_domain, $product_version ): ?array {
		if ( ! $this->use_cache ) {
			return null;
		}

		$cache_key = $this->get_cache_key( $product_text_domain, $product_version );

		return $this->dependency_cache[ $cache_key ] ?? null;
	}

	/**
	 * Sets a cached dependency result.
	 *
	 * @since 1.3.0
	 *
	 * @param string $product_text_domain The product text domain.
	 * @param mixed  $product_version     The product version.
	 * @param array  $result              The dependency check result.
	 *
	 * @return void
	 */
	private function set_cached_dependency_result( string $product_text_domain, $product_version, array $result ): void {
		if ( ! $this->use_cache ) {
			return;
		}

		$cache_key = $this->get_cache_key( $product_text_domain, $product_version );

		$this->dependency_cache[ $cache_key ] = $result;
	}

	/**
	 * Generates a cache key for a product and version.
	 *
	 * @since 1.3.0
	 *
	 * @param string $product_text_domain The product text domain.
	 * @param mixed  $product_version     The product version.
	 *
	 * @return string
	 */
	private function get_cache_key( string $product_text_domain, $product_version ): string {
		return $product_text_domain . '|' . ( $product_version ?? 'null' );
	}

	/**
	 * Checks if a product is a dependency of a single product.
	 *
	 * @since 1.2.0
	 *
	 * @param string $dependency_text_domain The text domain of the dependency.
	 * @param string $product_text_domain    The text domain of the product.
	 * @param bool   $active_product_only    (optional) Whether to check only active product. Default is false.
	 *
	 * @return array|false
	 */
	public function is_a_dependency_of_product( string $dependency_text_domain, string $product_text_domain, bool $active_product_only = false ) {
		if ( $dependency_text_domain === $product_text_domain ) {
			return false;
		}

		$product = $this->get_product( $product_text_domain );

		if ( ! $product ) {
			return false;
		}

		if ( $active_product_only && ! $product['active'] ) {
			return false;
		}

		$plugin_dependencies = $product['dependencies']['0.0.1']['plugin'] ?? [];

		foreach ( $plugin_dependencies as $dependency ) {
			if ( $dependency['text_domain'] === $dependency_text_domain ) {
				return [
					'name'        => $product['name'],
					'text_domain' => $product_text_domain,
				];
			}
		}

		return false;
	}

	/**
	 * Checks if a product is a dependency of any product(s).
	 *
	 * @since 1.2.0
	 *
	 * @param string $dependency_text_domain The text domain of the dependency.
	 * @param bool   $active_products_only   (optional) Whether to check only active products. Default is false.
	 *
	 * @return array|false
	 */
	public function is_a_dependency_of_any_product( string $dependency_text_domain, bool $active_products_only = false ) {
		$products = [];

		foreach ( $this->products as $product ) {
			if ( ! $dependency_text_domain && ! $product['text_domain'] ) {
				continue;
			}

			$required_by = $this->is_a_dependency_of_product( $dependency_text_domain, $product['text_domain'], $active_products_only );

			if ( $required_by ) {
				$products[] = $required_by;
			}
		}

		return ! empty( $products ) ? $products : false;
	}

	/**
	 * Returns dependencies for a product in an order that they should be installed/activated/updated.
	 *
	 * @since 1.2.0
	 *
	 * @param string      $product_text_domain The product text domain to check.
	 * @param string|null $product_version     (Optional) The product version to check. This is used to get the right set of dependencies as they are versioned. Default: null (i.e., the installed version will be used followed by the [remote] server version).
	 *
	 * @throws Exception Exception if a circular reference is detected.
	 *
	 * @return array
	 */
	public function get_product_dependency_resolution_sequence( $product_text_domain = '', $product_version = null ): array {
		$get_product_dependency_resolution_sequence = function ( $product_text_domain, $product_version = null, $checked_products = [], $ordered_dependencies = [] ) use ( &$get_product_dependency_resolution_sequence ) {
			$product = $this->get_product( $product_text_domain );

			if ( in_array( $product_text_domain, $checked_products, true ) ) {
				throw new Exception( self::FAILURE_CIRCULAR_REFERENCE );
			}

			$checked_products[] = $product_text_domain;

			// First check if all product dependencies are met.
			$dependencies_check = $this->check_dependencies( $product_text_domain, $product_version );

			if ( ! empty( Arr::get( $dependencies_check, 'unmet.system' ) ) ) {
				throw new Exception( self::FAILURE_UNMET_DEPENDENCY );
			}

			foreach ( Arr::get( $dependencies_check, 'unmet.plugin', [] ) as $unmet_dependency ) {
				if ( ! $unmet_dependency['resolvable'] ) {
					throw new Exception( self::FAILURE_UNMET_DEPENDENCY );
				}
			}

			// Then get the resolution sequence of all dependencies.
			foreach ( Arr::get( $dependencies_check, 'unmet.plugin', [] ) as $dependency ) {
				$ordered_dependencies += $get_product_dependency_resolution_sequence(
					$dependency['text_domain'],
					$dependency['required_version'],
					$checked_products,
					$ordered_dependencies
				);
			}

			switch ( true ) {
				case $product['active'] && $product['update_available']:
				case ! $product['active'] && $product['installed'] && $product['update_available']:
				case ! $product['active'] && ! $product['installed'] && $product['update_available']:
					$action  = 'update';
					$version = $product['server_version'];

					break;
				case ! $product['active'] && ! $product['installed']:
					$action  = 'install';
					$version = $product['server_version'];

					break;
				case ! $product['active'] && $product['installed']:
					$action  = 'activate';
					$version = $product['installed_version'];

					break;
				default:
					throw new Exception( self::FAILURE_UNKNOWN_PRODUCT_ACTION );
			}

			if ( ! in_array( $product_text_domain, $ordered_dependencies, true ) ) {
				$ordered_dependencies[ $product_text_domain ] = [
					'name'        => $product['name'],
					'text_domain' => $product['text_domain'],
					'action'      => $action,
					'version'     => $version,
				];
			}

			return $ordered_dependencies;
		};

		$result = $get_product_dependency_resolution_sequence( $product_text_domain, $product_version );

		array_pop( $result );

		return array_values( $result );
	}

	/**
	 * Checks if a product meets all dependencies.
	 *
	 * @since 1.2.0
	 *
	 * @param string      $product_text_domain The product text domain to check.
	 * @param string|null $product_version     (Optional) The product version to check. This is used to get the right set of dependencies as they are versioned. Default: null (i.e., the installed version will be used followed by the [remote] server version).
	 *
	 * @return array
	 */
	public function check_dependencies( $product_text_domain = '', $product_version = null ): array {
		// Clear cache at the start of each top-level product dependency check to prevent cross-product pollution.
		$this->clear_cache();

		$check_dependencies = function ( $product_text_domain, $product_version = null, $checked_dependencies = [], $unmet_dependencies = [], $is_top_level = true ) use ( &$check_dependencies ) {
			if ( empty( $unmet_dependencies ) ) {
				$unmet_dependencies = [
					'system' => [],
					'plugin' => [],
				];
			}

			if ( isset( $checked_dependencies[ $product_text_domain ] ) ) {
				return [
					'status' => true,
					'unmet'  => $unmet_dependencies,
				];
			}

			$checked_dependencies[ $product_text_domain ] = true;

			// Only use cache for recursive calls, not top-level calls.
			if ( ! $is_top_level ) {
				$instance_cached_result = $this->get_cached_dependency_result( $product_text_domain, $product_version );

				if ( null !== $instance_cached_result ) {
					// Merge cached unmet dependencies with current ones.
					foreach ( $instance_cached_result['unmet'] as $type => $unmet_items ) {
						$unmet_dependencies[ $type ] = array_merge( $unmet_dependencies[ $type ], $unmet_items );
					}

					return [
						'status' => $instance_cached_result['status'] && empty( $unmet_dependencies['system'] ) && empty( $unmet_dependencies['plugin'] ),
						'unmet'  => $unmet_dependencies,
					];
				}
			}

			$missing_name_placeholder = strtr(
				_x( "Product with '[text_domain]' text domain", 'Placeholders inside [] are not to be translated.', 'gk-gravityview' ),
				[ '[text_domain]' => $product_text_domain ]
			);

			$product = $this->get_product( $product_text_domain );

			if ( ! $product ) {
				$unmet_dependencies['plugin'][ $product_text_domain ] = [
					'name'             => $missing_name_placeholder,
					'required_version' => '',
					'text_domain'      => $product_text_domain,
					'icon'             => '',
					'reason'           => self::FAILURE_NOT_FOUND,
					'resolvable'       => false,
				];

				$result = [
					'status' => false,
					'unmet'  => $unmet_dependencies,
				];

				// Only cache recursive calls, not top-level calls.
				if ( ! $is_top_level ) {
					$this->set_cached_dependency_result( $product_text_domain, $product_version, $result );
				}

				return $result;
			}

			if ( ! $product_version ) {
				if ( ! empty( $product['installed_version'] ) ) {
					$product_version = $product['installed_version'];
				} elseif ( ! empty( $product['server_version'] ) ) {
					$product_version = $product['server_version'];
				}

				if ( ! $product_version ) {
					$unmet_dependencies['plugin'][ $product_text_domain ] = [
						'name'             => $product['name'],
						'required_version' => '',
						'text_domain'      => $product_text_domain,
						'icon'             => $product['icon'] ?? '',
						'reason'           => self::FAILURE_MISSING_PRODUCT_VERSION,
						'resolvable'       => false,
					];

					$result = [
						'status' => false,
						'unmet'  => $unmet_dependencies,
					];

					// Only cache recursive calls, not top-level calls.
					if ( ! $is_top_level ) {
						$this->set_cached_dependency_result( $product_text_domain, $product_version, $result );
					}

					return $result;
				}
			}

			$dependencies = $this->get_dependencies_for_product_version( $product_version, $product['dependencies'] );

			if ( is_null( $dependencies ) ) {
				$unmet_dependencies['plugin'][ $product_text_domain ] = [
					'name'             => $product['name'],
					'required_version' => $product_version,
					'text_domain'      => $product_text_domain,
					'icon'             => $product['icon'] ?? '',
					'reason'           => self::FAILURE_MISSING_DEPENDENCIES,
					'resolvable'       => false,
				];

				$result = [
					'status' => false,
					'unmet'  => $unmet_dependencies,
				];

				// Only cache recursive calls, not top-level calls.
				if ( ! $is_top_level ) {
					$this->set_cached_dependency_result( $product_text_domain, $product_version, $result );
				}

				return $result;
			}

			foreach ( ( $dependencies['plugin'] ?? [] ) as $dependency_data ) {
				$dependency_text_domain = $dependency_data['text_domain'];

				$dependency_product = $this->get_product(
					( implode( '|', [ $dependency_text_domain, $dependency_data['text_domain_legacy'] ?? '' ] ) ),
					$dependency_data['author'] ?? ''
				);

				if ( ! $dependency_product ) {
					$checked_dependencies[ $dependency_text_domain ] = true;

					$unmet_dependencies['plugin'][ $dependency_text_domain ] = [
						'name'             => $dependency_data['name'] ?? $missing_name_placeholder,
						'required_version' => $dependency_data['version'],
						'text_domain'      => $dependency_text_domain,
						'icon'             => '',
						'reason'           => self::FAILURE_NOT_FOUND,
						'resolvable'       => false,
					];

					continue;
				}

				// When a dependency product is found, it can be found under a different text domain given that we're merging regular and legacy text domains.
				// For this reason, we need to set the dependency text domain to the one found so that further checks are done with the correct text domain.
				$dependency_text_domain         = $dependency_product['text_domain'];
				$dependency_data['text_domain'] = $dependency_product['text_domain'];

				$dependencies_of_dependency = $this->get_dependencies_for_product_version( $dependency_data['version'], $dependency_product['dependencies'] );

				if ( ! empty( $dependency_product['dependencies'] && is_null( $dependencies_of_dependency ) ) ) {
					return [
						'status'  => true,
						'reason'  => self::FAILURE_MISSING_DEPENDENCIES,
						'product' => $dependency_product,
						'unmet'   => $unmet_dependencies,
					];
				}

				// Check for dependencies of the dependent product. Love me some recursion :D!
				if ( ! empty( $dependencies_of_dependency['plugin'] ) || ! empty( $dependencies_of_dependency['system'] ) ) {
					$dependencies_of_dependency = $check_dependencies( $dependency_text_domain, $dependency_data['version'], $checked_dependencies, $unmet_dependencies, false );

					if ( ! $dependencies_of_dependency['status'] ) {
						$unmet_dependencies = array_merge( $unmet_dependencies, $dependencies_of_dependency['unmet'] );
					}
				}

				$unmet_dependencies['plugin'] = $this->process_plugin_dependency( $product_text_domain, $dependency_data, $dependency_product, $unmet_dependencies['plugin'] );
			}

			foreach ( ( $dependencies['system'] ?? [] ) as $dependency_data ) {
				$unmet_dependencies['system'] = $this->process_system_dependency( $product_text_domain, $dependency_data, $unmet_dependencies['system'] );
			}

			$result = [
				'status' => empty( $unmet_dependencies['system'] ) && empty( $unmet_dependencies['plugin'] ),
				'unmet'  => $unmet_dependencies,
			];

			// Only cache recursive calls, not top-level calls.
			if ( ! $is_top_level ) {
				$this->set_cached_dependency_result( $product_text_domain, $product_version, $result );
			}

			return $result;
		};

		$result = $check_dependencies( $product_text_domain, $product_version );

		foreach ( $result['unmet'] as &$type ) {
			$type = array_values( $type );
		}

		return $result;
	}

	/**
	 * Checks if a plugin dependency is met.
	 *
	 * @since   1.2.0
	 *
	 * @used-by ProductDependencyChecker::check_all_dependencies()
	 *
	 * @param string $required_by_product_text_domain The text domain of the product that requires the dependency.
	 * @param array  $dependency_data                 The required dependency data.
	 * @param array  $dependency_product              The dependency product data.
	 * @param array  $unmet_dependencies              (optional) Object with unmet dependencies that will be updated. Default: empty array.
	 *
	 * @return array
	 */
	private function process_plugin_dependency( $required_by_product_text_domain, $dependency_data, $dependency_product, $unmet_dependencies = [] ): array {
		$dependency_text_domain = $dependency_data['text_domain'];

		// If there are multiple versions of the same dependency required by more than one product, use the highest version as the required version.
		$highest_required_version = Arr::get( $unmet_dependencies, "{$dependency_text_domain}.required_version" );
		$highest_required_version = $this->compare_product_versions( $highest_required_version ?? 0, $dependency_data['version'], '<' ) ? $dependency_data['version'] : $highest_required_version;

		$unmet_dependency = [
			'name'              => $dependency_product['name'],
			'text_domain'       => $dependency_text_domain,
			'icon'              => ( $dependency_data['icon'] ?? $dependency_product['icon'] ) ?? '',
			'active'            => $dependency_product['active'],
			'installed'         => $dependency_product['installed'],
			'installed_version' => $dependency_product['installed_version'],
			'server_version'    => $dependency_product['server_version'],
			'required_version'  => $highest_required_version,
			'required_by'       => array_merge(
				Arr::get( $unmet_dependencies, "{$dependency_text_domain}.required_by", [] ),
				[ $required_by_product_text_domain => $dependency_data['version'] ]
			),
		];

		if ( ! $dependency_product['installed'] ) {
			switch ( true ) {
				// Free but without download link.
				case $dependency_product['free'] && ! $dependency_product['download_link']:
					$unmet_dependency['reason']     = self::FAILURE_NO_DOWNLOAD_LINK;
					$unmet_dependency['resolvable'] = false;

					break;
				// Unlicensed product.
				case empty( $dependency_product['licenses'] ) && ! $dependency_product['free']:
					$unmet_dependency['reason']     = self::FAILURE_UNLICENSED;
					$unmet_dependency['resolvable'] = false;

					break;
				// Low version.
				case $this->compare_product_versions( $dependency_product['server_version'] ?? 0, $highest_required_version, '<' ):
					$unmet_dependency['reason']     = self::FAILURE_OLDER_VERSION;
					$unmet_dependency['resolvable'] = false;

					break;
				// Product is not and can't be installed because it's not licensed and doesn't have a download link (i.e., a free product).
				case empty( $dependency_product['licenses'] ) && ! $dependency_product['download_link']:
					$unmet_dependency['reason']     = self::FAILURE_NOT_INSTALLED;
					$unmet_dependency['resolvable'] = false;

					break;
				// Product is not installed but can be.
				default:
					$unmet_dependency['reason']     = self::FAILURE_NOT_INSTALLED;
					$unmet_dependency['resolvable'] = true;

					break;
			}
		}

		if ( $dependency_product['installed'] ) {
			switch ( true ) {
				/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found
				    // In the future, we might want to check if the product is licensed and if the license is valid. This was removed in 4fdd142.
					// Unlicensed product.
					case empty( $dependency_product['licenses'] ) && ! $dependency_product['free']:
						$unmet_dependency['reason']     = self::FAILURE_UNLICENSED;
						$unmet_dependency['resolvable'] = false;

						break;
				*/
				// Low version and no update available OR Update available but low version.
				case ! $dependency_product['update_available'] && $this->compare_product_versions( $dependency_product['installed_version'] ?? 0, $highest_required_version, '<' ):
				case $dependency_product['update_available'] && $this->compare_product_versions( $dependency_product['server_version'] ?? 0, $highest_required_version, '<' ):
					$unmet_dependency['reason']     = self::FAILURE_OLDER_VERSION;
					$unmet_dependency['resolvable'] = false;

					break;
				// Update available and the version is >= than the one required.
				case $dependency_product['update_available'] && ( $this->compare_product_versions( $dependency_product['installed_version'] ?? 0, $highest_required_version, '<' ) && $this->compare_product_versions( $dependency_product['server_version'] ?? 0, $highest_required_version, '>=' ) ):
					$unmet_dependency['reason']     = self::FAILURE_OLDER_VERSION;
					$unmet_dependency['resolvable'] = true;

					break;
				// Product is not active.
				case ! $dependency_product['active']:
					$unmet_dependency['reason']     = self::FAILURE_INACTIVE;
					$unmet_dependency['resolvable'] = true;
					break;
			}
		}

		if ( isset( $unmet_dependency['reason'] ) ) {
			$unmet_dependencies[ $dependency_text_domain ] = $unmet_dependency;
		}

		return $unmet_dependencies;
	}

	/**
	 * Checks if a system dependency is met.
	 *
	 * @since   1.2.0
	 *
	 * @used-by ProductDependencyChecker::check_all_dependencies()
	 *
	 * @param string $required_by_product_text_domain The text domain of the product that requires the dependency.
	 * @param array  $dependency_data                 The required dependency data.
	 * @param array  $unmet_dependencies              (optional) Object with unmet dependencies that will be updated. Default: empty array.
	 *
	 * @return array
	 */
	private function process_system_dependency( $required_by_product_text_domain, $dependency_data, $unmet_dependencies = [] ): array {
		global $wp_version;

		$dependency_name = $dependency_data['name'];

		// If there are multiple versions of the same dependency required by more than one product, use the highest version as the required version.
		$highest_required_version = Arr::get( $unmet_dependencies, "{$dependency_name}.required_version" );
		$highest_required_version = $this->compare_product_versions( $highest_required_version ?? 0, $dependency_data['version'], '<' ) ? $dependency_data['version'] : $highest_required_version;

		$unmet_dependency = [
			'name'              => $dependency_name,
			'icon'              => $dependency_data['icon'] ?? '',
			'available_version' => null,
			'required_version'  => $highest_required_version,
			'required_by'       => array_merge(
				Arr::get( $unmet_dependencies, "{$dependency_name}.required_by", [] ),
				[ $required_by_product_text_domain => $dependency_data['version'] ]
			),
		];

		switch ( $dependency_name ) {
			case 'PHP':
				if ( ! is_php_version_compatible( $highest_required_version ) ) {
					$unmet_dependency['available_version'] = PHP_VERSION;
					$unmet_dependency['reason']            = self::FAILURE_OLDER_VERSION;
					$unmet_dependency['resolvable']        = false;
				}

				break;
			case 'WordPress':
				if ( ! is_wp_version_compatible( $highest_required_version ) ) {
					$unmet_dependency['available_version'] = $wp_version;
					$unmet_dependency['reason']            = self::FAILURE_OLDER_VERSION;
					$unmet_dependency['resolvable']        = false;
				}

				break;
			default:
				unset( $unmet_dependency['available_version'] );
				$unmet_dependency['reason']     = self::FAILURE_NOT_FOUND;
				$unmet_dependency['resolvable'] = false;

				break;
		}

		if ( isset( $unmet_dependency['reason'] ) ) {
			$unmet_dependencies[ $dependency_name ] = $unmet_dependency;
		}

		return $unmet_dependencies;
	}

	/**
	 * Returns product data.
	 * If the product is not GK (as returned by our EDD API) product, it will be searched in the installed plugins.
	 *
	 * @since 1.2.0
	 * @since 1.2.12 Added $author_str parameter.
	 *
	 * @param string $text_domain_str Text domain(s). Optionally pipe-separated (e.g. 'gravityview|gk-gravityview').
	 * @param string $author_str      (optional) Product author(s). Optionally pipe-separated (e.g. 'GravityView|GravityKit|Katz Web Services, Inc.').
	 *
	 * @return array|null
	 */
	private function get_product( $text_domain_str = '', $author_str = '' ): ?array {
		if ( '' === $text_domain_str ) {
			return null;
		}

		$text_domains = array_filter( explode( '|', $text_domain_str ) );

		foreach ( $text_domains as $text_domain ) {
			$gk_product = Arr::first(
				$this->products,
				function ( $product ) use ( $text_domain ) {
					return $product['text_domain'] === $text_domain;
				}
			);

			if ( $gk_product ) {
				return ProductManager::get_instance()->normalize_product_data( $gk_product );
			}

			$non_gk_product = $this->get_product_by_text_domain( $text_domain, false, $author_str );

			if ( $non_gk_product ) {
				return ProductManager::get_instance()->normalize_product_data( $non_gk_product );
			}
		}

		return null;
	}

	/**
	 * Returns dependencies based on the version.
	 *
	 * @since 1.2.0
	 *
	 * @param string|null $product_version The product version.
	 * @param array       $dependencies    The dependencies data.
	 *
	 * @return mixed|null
	 */
	private function get_dependencies_for_product_version( $product_version, $dependencies ) {
		$dependencies_versions = array_keys( $dependencies );

		if ( empty( $dependencies_versions ) ) {
			return null;
		}

		$compatible_version = array_filter(
			$dependencies_versions,
			function ( $dependency_version ) use ( $product_version ) {
				return (bool) $this->compare_product_versions( $dependency_version, $product_version ?? '', '<=' );
			}
		);

		if ( empty( $compatible_version ) ) {
			return null;
		}

		$compatible_version = max( $compatible_version );

		return $dependencies[ $compatible_version ] ?? null;
	}

	/**
	 * Retrieves information about an installed plugin by its text domain.
	 * This is a thin wrapper around {@see CoreHelpers::get_installed_plugin_by_text_domain()} to facilitate test stubbing.
	 *
	 * @since 1.2.24
	 *
	 * @param string $text_domain Text domain.
	 * @param bool   $skip_cache  (optional) Whether to skip the cache. Default is false.
	 * @param string $author_str  (optional) Product author(s). Optionally pipe-separated (e.g. 'GravityView|GravityKit|Katz Web Services, Inc.').
	 *
	 * @return array|null
	 */
	protected function get_product_by_text_domain( string $text_domain, bool $skip_cache = false, string $author_str = '' ) {
		return CoreHelpers::get_installed_plugin_by_text_domain(
			$text_domain,
			$skip_cache,
			$author_str
		);
	}

	/**
	 * Compares product versions.
	 * This is a thin wrapper around {@see CoreHelpers::version_compare()} for backward compatibility with older Foundation versions and to facilitate test stubbing.
	 *
	 * @since 1.2.25
	 *
	 * @param mixed       $version1 First version to compare.
	 * @param mixed       $version2 Second version to compare.
	 * @param string|null $operator (optional) Comparison operator.
	 *
	 * @return int|bool Returns -1, 0, or 1 if no operator is given; otherwise, returns a boolean.
	 */
	protected function compare_product_versions( $version1, $version2, $operator = null ) {
		/** @phpstan-ignore-next-line */
		$compare = is_callable( [ 'GravityKit\GravityView\Foundation\Helpers\Core', 'version_compare' ] )
			? [ 'GravityKit\GravityView\Foundation\Helpers\Core', 'version_compare' ]
			: 'version_compare';

		return null !== $operator
			? call_user_func( $compare, $version1, $version2, $operator )
			: call_user_func( $compare, $version1, $version2 );
	}
}
