<?php

namespace DigitalApps\PaymentsForAdyen\Blocks;

defined('ABSPATH') || exit;

// Minimal WooCommerce Blocks compatibility for classic gateway visibility
// Uses the CompatibilityRegistry so classic gateways can appear in Blocks Checkout

class Adyen_Blocks_Support {
	public static function init() {
		// Register compatibility when Blocks are available
		add_action('woocommerce_blocks_loaded', [__CLASS__, 'register_blocks_compat']);
	}

	public static function register_blocks_compat() {
		// Register the proper AbstractPaymentMethodType for WooCommerce Blocks
		add_action(
			'woocommerce_blocks_payment_method_type_registration',
			[__CLASS__, 'register_payment_method_type']
		);

		// Also register compatibility for classic gateway (fallback)
		if (class_exists('Automattic\\WooCommerce\\Blocks\\Payments\\Integrations\\CompatibilityRegistry')) {
			try {
				$registry = \Automattic\WooCommerce\Blocks\Payments\Integrations\CompatibilityRegistry::get_instance();
				$registry->register('digitalapps_payments_for_adyen_card');
			} catch (\Throwable $e) {
				// Fail silently; fallback is classic checkout
			}
		}
	}

	/**
	 * Register the Adyen Card payment method type with WooCommerce Blocks
	 */
	public static function register_payment_method_type($payment_method_registry) {
		// Load the payment method type class
		require_once DIGITALAPPS_PAYMENTS_FOR_ADYEN_PATH . 'includes/blocks/class-adyen-card-payment-method-type.php';

		// Register the payment method type
		$payment_method_registry->register(new \DigitalApps\PaymentsForAdyen\Blocks\Adyen_Card_Payment_Method_Type());
	}
}

Adyen_Blocks_Support::init();
