<?php

defined('ABSPATH') || exit;

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

abstract class WC_Gateway_Abstract_iPag_Blocks extends AbstractPaymentMethodType
{
    protected $gateway;

    public function initialize()
    {
        $gateways = WC()->payment_gateways->payment_gateways();
        $this->gateway = $gateways[$this->name] ?? null;
    }

    public function is_active()
    {
        if (!function_exists('WC') || !WC()->payment_gateways) {
            return false;
        }

        if (!$this->gateway) {
            return false;
        }

        return $this->gateway->is_available();
    }

    public function get_payment_method_script_handles()
    {
        $style_url = WC_iPag_Gateway::plugin_url() . '/js/blocks/ipag-gateway-blocks.min.css?v=' . WC_iPag_Gateway::IPAG_VERSION;

        $dynamic_script_url = WC_iPag_Gateway::plugin_url() . "/js/blocks/{$this->name}-blocks.min.js?v=" . WC_iPag_Gateway::IPAG_VERSION;

        wp_register_style(
            'ipag-gateway-blocks-style',
            $style_url,
            [],
            WC_iPag_Gateway::IPAG_VERSION
        );

        wp_register_script(
            $this->name . "-blocks",
            $dynamic_script_url,
            ['wp-element', 'wp-i18n', 'wp-components', 'wc-blocks-registry'],
            WC_iPag_Gateway::IPAG_VERSION,
            true
        );

        wp_set_script_translations(
            $this->name . "-blocks",
            'ipag-gateway',
            WC_iPag_Gateway::plugin_abspath() . '/languages'
        );

        wp_enqueue_style('ipag-gateway-blocks-style');

        return [$this->name . "-blocks"];
    }

    public function get_payment_method_data()
    {
        return [
            'gateway_id' => $this->gateway->id ?? '',
            'supports' => $this->gateway->supports ?? ['products'],
            'assets_url' => WC_iPag_Gateway::plugin_url() . '/images/'
        ];
    }
}
