import { broadcast_tx, fee_estimates, get_fee_target, get_utxo_data, get_tx_data, get_address_utxos } from '../../../lib/oracle.js';
import * as assert from '../../../assert.js';
import { sleep } from '../../../lib/util.js';
function broadcast_tx_api(client) {
    return async (txhex) => {
        assert.is_hex(txhex);
        return broadcast_tx(client._oracle, txhex);
    };
}
function fee_estimates_api(client) {
    return async () => {
        return fee_estimates(client._oracle);
    };
}
function get_fee_target_api(client) {
    return async (target) => {
        return get_fee_target(client._oracle, target);
    };
}
function get_txdata_api(client) {
    return async (txid) => {
        assert.is_hash(txid);
        return get_tx_data(client._oracle, txid);
    };
}
function get_utxo_api(client) {
    return async (query) => {
        assert.is_hash(query.txid);
        return get_utxo_data(client._oracle, query);
    };
}
function get_addr_utxos_api(client) {
    return async (address) => {
        return get_address_utxos(client._oracle, address);
    };
}
function poll_address_api(client) {
    return async (address, interval, retries, verbose = false) => {
        let tries = 0, utxos = [];
        for (let i = 0; i < retries; i++) {
            if (utxos.length > 0) {
                return utxos;
            }
            else {
                utxos = await get_address_utxos(client._oracle, address);
                tries += 1;
                if (verbose) {
                    const msg = `[${tries}/${retries}] checking address in ${interval} seconds...`;
                    console.log(msg);
                }
                await sleep(interval * 1000);
            }
        }
        throw new Error('polling timed out');
    };
}
export default function (client) {
    return {
        broadcast_tx: broadcast_tx_api(client),
        fee_estimates: fee_estimates_api(client),
        fee_target: get_fee_target_api(client),
        get_txdata: get_txdata_api(client),
        get_utxo: get_utxo_api(client),
        get_address_utxos: get_addr_utxos_api(client),
        poll_address: poll_address_api(client)
    };
}
