<?php
namespace ccxt;

// ----------------------------------------------------------------------------

// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

// -----------------------------------------------------------------------------
include_once PATH_TO_CCXT . '/test/exchange/base/test_currency.php';

function test_fetch_currencies($exchange, $skipped_properties) {
    $method = 'fetchCurrencies';
    $currencies = $exchange->fetch_currencies();
    // todo: try to invent something to avoid undefined undefined, i.e. maybe move into private and force it to have a value
    $num_inactive_currencies = 0;
    $max_inactive_currencies_percentage = $exchange->safe_integer($skipped_properties, 'maxInactiveCurrenciesPercentage', 50); // no more than X% currencies should be inactive
    $required_active_currencies = ['BTC', 'ETH', 'USDT', 'USDC'];
    $features = $exchange->features;
    $features_spot = $exchange->safe_dict($features, 'spot', array());
    $fetch_currencies = $exchange->safe_dict($features_spot, 'fetchCurrencies', array());
    $is_fetch_currencies_private = $exchange->safe_value($fetch_currencies, 'private', false);
    if (!$is_fetch_currencies_private) {
        $values = is_array($currencies) ? array_values($currencies) : array();
        assert_non_emtpy_array($exchange, $skipped_properties, $method, $values);
        $currencies_length = count($values);
        // ensure exchange returns enough length of currencies
        $skip_amount = (is_array($skipped_properties) && array_key_exists('amountOfCurrencies', $skipped_properties));
        assert($skip_amount || $currencies_length > 5, $exchange->id . ' ' . $method . ' must return at least several currencies, but it returned ' . ((string) $currencies_length));
        // allow skipped exchanges
        $skip_active = (is_array($skipped_properties) && array_key_exists('activeCurrenciesQuota', $skipped_properties));
        $skip_major_currency_check = (is_array($skipped_properties) && array_key_exists('activeMajorCurrencies', $skipped_properties));
        // loop
        for ($i = 0; $i < $currencies_length; $i++) {
            $currency = $values[$i];
            test_currency($exchange, $skipped_properties, $method, $currency);
            // detailed check for deposit/withdraw
            $active = $exchange->safe_bool($currency, 'active');
            if ($active === false) {
                $num_inactive_currencies = $num_inactive_currencies + 1;
            }
            // ensure that major currencies are active and enabled for deposit and withdrawal
            $code = $exchange->safe_string($currency, 'code', null);
            $withdraw = $exchange->safe_bool($currency, 'withdraw');
            $deposit = $exchange->safe_bool($currency, 'deposit');
            if ($exchange->in_array($code, $required_active_currencies)) {
                assert($skip_major_currency_check || ($withdraw && $deposit), 'Major currency ' . $code . ' should have withdraw and deposit flags enabled');
            }
        }
        // check at least X% of currencies are active
        $inactive_currencies_percentage = ($num_inactive_currencies / $currencies_length) * 100;
        assert($skip_active || ($inactive_currencies_percentage < $max_inactive_currencies_percentage), 'Percentage of inactive currencies is too high at ' . ((string) $inactive_currencies_percentage) . '% that is more than the allowed maximum of ' . ((string) $max_inactive_currencies_percentage) . '%');
        detect_currency_conflicts($exchange, $currencies);
    }
    return true;
}


function detect_currency_conflicts($exchange, $currency_values) {
    // detect if there are currencies with different ids for the same code
    $ids = array();
    $keys = is_array($currency_values) ? array_keys($currency_values) : array();
    for ($i = 0; $i < count($keys); $i++) {
        $key = $keys[$i];
        $currency = $currency_values[$key];
        $code = $currency['code'];
        if (!(is_array($ids) && array_key_exists($code, $ids))) {
            $ids[$code] = $currency['id'];
        } else {
            $is_different = $ids[$code] !== $currency['id'];
            assert(!$is_different, $exchange->id . ' fetchCurrencies() has different ids for the same code: ' . $code . ' ' . $ids[$code] . ' ' . $currency['id']);
        }
    }
    return true;
}
