<?php
/**
 * PeachPay Stripe P24 WooCommerce Blocks support file.
 *
 * @package PeachPay
 */

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

/**
 * .
 */
final class PeachPay_Stripe_P24_Payment_Blocks_Support extends AbstractPaymentMethodType {

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

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

	/**
	 * Initializes the payment method type.
	 */
	public function initialize() {
		$this->gateway  = peachpay_get_payment_gateway( $this->name );
		$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() {
		// Check if gateway exists before calling methods on it
		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() {
		// Gateway Script
		PeachPay::register_webpack_script(
			$this->name . '-blocks',
			'wordpress/' . $this->name . '-blocks',
			array(),
			true
		);

		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() {
		// Return empty array if gateway is not available
		if ( ! $this->gateway || ! is_object( $this->gateway ) ) {
			return array();
		}

		return array(
			'connect_id'                 => PeachPay_Stripe_Integration::connect_id(),
			'currency_fallback_required' => $this->gateway->get_fallback_currency(),
			'description'                => $this->gateway->get_description(),
			'icon_url'                   => $this->gateway->get_icon_url(),
			'public_key'                 => PeachPay_Stripe_Integration::public_key(),
			'setup_intent_nonce'         => wp_create_nonce( 'peachpay-stripe-setup-intent' ),
			'setup_intent_url'           => WC_AJAX::get_endpoint( 'pp-stripe-setup-intent' ),
			'supports'                   => array_filter( $this->gateway->supports, array( $this->gateway, 'supports' ) ),
			'title'                      => $this->gateway->get_title(),
		);
	}
}
