<?php
namespace Svea_Webpay_For_Woocommerce\Traits;

use Svea\WebPay\Response\SveaResponse;
use Svea_Webpay_For_Woocommerce\Gateways\WC_Gateway_Svea_Mobilepay;
use Svea_Webpay_For_Woocommerce\Helper;
use Svea_Webpay_For_Woocommerce\Service_Providers\Config_Production;
use Svea_Webpay_For_Woocommerce\Service_Providers\Config_Test;

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

/**
 * A typical check for a payment gateway to see if country and currency is enabled
 *
 */
trait Country_Currency_Availability {

	/**
	 * Check whether or not this payment gateway is available
	 *
	 * @return boolean
	 */
	public function is_available() {
		if ( ! parent::is_available() ) {
			return false;
		}

		if ( ! is_admin() ) {
			if ( empty( $this->merchant_id ) || ! isset( WC()->customer ) ) {
				return false;
			}

			$wc_currency = get_woocommerce_currency();
			$customer_country = strtoupper( WC()->customer->get_billing_country() );

			foreach ( $this->enabled_country_currencies as $country => $currency ) {
				if (
					$customer_country === $country &&
					$wc_currency === $currency
				) {
					return true;
				}
			}

			return false;
		}

		return true;
	}

}