<?php
/**
 * Blocks support integration.
 *
 * @package SHC4WC
 */

// phpcs:disable Squiz.Commenting
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

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

final class SHC4WC_Blocks_Support extends AbstractPaymentMethodType {
	protected $name = 'shc4wc';

	public function initialize() {
		$this->settings = get_option( 'woocommerce_shc4wc_settings', array() );
	}

	public function is_active() {
		$enabled      = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'no';
		$is_connected = SHC4WC_Plugin::is_connected();
		return 'yes' === $enabled && $is_connected;
	}

	public function get_payment_method_script_handles() {
		$asset_path = SHC4WC_PLUGIN_DIR . 'assets/js/blocks.js';
		$handle     = 'shc4wc-blocks-integration';

		wp_register_script(
			$handle,
			SHC4WC_PLUGIN_URL . 'assets/js/blocks.js',
			array( 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n' ),
			file_exists( $asset_path ) ? filemtime( $asset_path ) : SHC4WC_VERSION,
			true
		);

		return array( $handle );
	}

	public function get_payment_method_data() {
		$title       = isset( $this->settings['title'] ) ? $this->settings['title'] : __( 'Pay with Square', 'payments-connect-square' );
		$settings    = get_option( 'woocommerce_shc4wc_settings', array() );
		$environment = ( isset( $settings['environment'] ) && 'live' === $settings['environment'] ) ? 'live' : 'sandbox';
		$description = SHC4WC_Gateway::format_checkout_description(
			isset( $this->settings['description'] ) ? $this->settings['description'] : '',
			$environment
		);

		return array(
			'title'       => $title,
			'description' => $description,
			'supports'    => array( 'products' ),
		);
	}
}
