<?php

namespace WKO\Controllers\Checkout;

class PaymentGatewayManager
{
    public function __construct()
    {
        add_filter('woocommerce_available_payment_gateways', [$this, 'update_payment_gateways']);
    }

    public function update_payment_gateways($available_gateways)
    {
        if (!is_checkout() || is_admin()) {
            return $available_gateways;
        }

        $country = WC()->customer->get_billing_country();
        $transacting_countries = get_option('wc_koin_settings_transacting_countries', array());

        if ($country !== 'BR') {
            unset($available_gateways['wc-koin-pix']);
        }

        foreach ($transacting_countries as $country_enable) {
            if ($country == $country_enable) {
                return $available_gateways;
            }
        }
        $available_gateways = $this->remove_specific_gateways($available_gateways);
        return $available_gateways;
    }

    private function remove_specific_gateways($available_gateways)
    {
        unset($available_gateways['wc-koin-billet']);
        unset($available_gateways['wc-koin-credit']);
        unset($available_gateways['wc-koin-pix']);

        return $available_gateways;
    }
}
