<?php
/**
 * @package     Splitit_WooCommerce_Plugin
 *
 * File - class-splitit-flexfields-payment-blocks-support.php
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // @Exit if accessed directly
}

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Automattic\WooCommerce\StoreApi\Payments\PaymentResult;
use Automattic\WooCommerce\StoreApi\Payments\PaymentContext;

/**
 * WC_SplitIt_Blocks_Support class.
 *
 * @extends AbstractPaymentMethodType
 */
class WC_SplitIt_Blocks_Support extends AbstractPaymentMethodType {
	/**
	 * Initializes the payment method type.
	 */
	public function initialize() {

		$gateway        = WC_SplitIt_FlexFields_Payment_Gateway::get_instance();

		$payment_method_title = $gateway->title;
		$splitit_environment  = $gateway->settings['splitit_environment'] ?? 'sandbox';
		$splitit_settings_3d  = $gateway->settings['splitit_settings_3d'] ?? 0;

		$sandbox = ! ( 'production' === $splitit_environment );

		if ( $sandbox ) {
			wp_register_script( 'flex_field_js', 'https://flex-form.sandbox.splitit.com/flex-form.js', null, null, true );
		} else {
			wp_register_script( 'flex_field_js', 'https://flex-form.production.splitit.com/flex-form.js', null, null, true );
		}
		wp_enqueue_script( 'flex_field_js' );

		wp_register_script(
			'splitit-block-payment-script',
			plugins_url( '../assets/js/splitit-blocks.js', __FILE__ ),
			array( 'wc-blocks-registry', 'wp-element', 'wp-components', 'wp-data', 'wp-hooks' ),
			'1.0.0',
			true
		);
		wp_localize_script(
			'splitit-block-payment-script',
			'splititPaymentMethodData',
			array(
				'payment_method_title' => $payment_method_title,
				'debug3ds'             => (bool) $splitit_settings_3d,
				'debug'                => $sandbox,
				'culture'              => str_replace( '_', '-', get_locale() ),
				'order_id'             => null,
			)
		);
		wp_enqueue_script( 'splitit-block-payment-script' );

		wp_register_script(
			'splitit-payment-script',
			plugins_url( '../assets/js/splitit-payment.js', __FILE__ ),
			array( 'jquery' ),
			'1.0.0',
			true
		);
		wp_localize_script(
			'splitit-payment-script',
			'splititPaymentData',
			array(
				'debug3ds' => (bool) $splitit_settings_3d,
				'debug'    => $sandbox,
				'culture'  => str_replace( '_', '-', get_locale() ),
				'order_id' => null,
			)
		);
		wp_enqueue_script( 'splitit-payment-script' );

	}
}
