<?php
/**
 * PeachPay Authorize.net Credit / Debit card gateway block.
 *
 * @package PeachPay
 */

defined( 'PEACHPAY_ABSPATH' ) || exit;

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;

/**
 * Authorize.net card gateway blocks support.
 */
final class PeachPay_Authnet_Card_Gateway_Blocks_Support extends AbstractPaymentMethodType {

	/**
	 * The gateway instance.
	 *
	 * @var PeachPay_Authnet_Card_Gateway
	 */
	private $gateway;

	/**
	 * Payment method name/id/slug.
	 *
	 * @var string
	 */
	protected $name = 'peachpay_authnet_card';

	/**
	 * Initializes the payment method type.
	 */
	public function initialize() {
		$this->gateway  = peachpay_get_payment_gateway( $this->name );
		if ( ! $this->gateway && class_exists( 'PeachPay_Authnet_Card_Gateway', false ) ) {
			$this->gateway = new PeachPay_Authnet_Card_Gateway();
		}
		$this->settings = get_option( 'peachpay_' . $this->name . '_settings', array() );
	}

	/**
	 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
	 *
	 * @return boolean
	 */
	public function is_active() {
		if ( ! $this->gateway || ! is_object( $this->gateway ) ) {
			return false;
		}
		return $this->gateway->is_available( true );
	}

	/**
	 * Returns an array of scripts/handles to be registered for this payment method.
	 *
	 * @return array
	 */
	public function get_payment_method_script_handles() {
		// AcceptUI v3 performs a CDN self-validation check on its own script tag src.
		// Registering it through WordPress adds a ?ver= query string which can
		// cause AcceptCore.js to log "Library is not coming from Accept CDN".
		// We pass the URL via get_payment_method_data() and inject the script
		// tag dynamically from the TSX component instead.
		PeachPay::register_webpack_script(
			$this->name . '-blocks',
			'wordpress/' . $this->name . '-blocks',
			array(),
			true
		);

		PeachPay::register_webpack_style(
			$this->name . '-blocks',
			'wordpress/' . $this->name . '-blocks'
		);
		wp_enqueue_style( $this->name . '-blocks' );

		return array( $this->name . '-blocks' );
	}

	/**
	 * Returns an array of key=>value pairs of data made available to the payment methods script.
	 *
	 * @return array
	 */
	public function get_payment_method_data() {
		if ( ! $this->gateway || ! is_object( $this->gateway ) ) {
			return array();
		}
		$mode = PeachPay_Authnet_Integration::mode();

		return array(
			'authnet'     => array(
				'login_id'          => PeachPay_Authnet_Integration::login_id( $mode ),
				'public_client_key' => PeachPay_Authnet_Integration::public_client_key( $mode ),
				'accept_src'        => 'test' === $mode
					? 'https://jstest.authorize.net/v3/AcceptUI.js'
					: 'https://js.authorize.net/v3/AcceptUI.js',
			),
			'icon'        => $this->gateway->get_icon_url(),
			'title'       => $this->gateway->get_title(),
			'description' => $this->gateway->description,
			'supports'    => array_filter( $this->gateway->supports, array( $this->gateway, 'supports' ) ),
		);
	}
}
