import { resolve_json } from '../../lib/oracle.js';
import { parse_network } from '../../lib/parse.js';
import contract_api from '../../client/api/contract.js';
import deposit_api from '../../client/api/deposit.js';
import oracle_api from '../../client/api/oracle.js';
import witness_api from '../../client/api/wid.js';
const DEFAULT_HOST = 'http://localhost:3000';
const DEFAULT_ORACLE = 'http://172.21.0.3:3300';
const DEFAULT_CHAIN = 'regtest';
export class EscrowClient {
    constructor(config) {
        this.contract = contract_api(this);
        this.deposit = deposit_api(this);
        this.oracle = oracle_api(this);
        this.witness = witness_api(this);
        this._fetcher = get_fetcher(config.fetcher ?? fetch);
        this._host = config.hostname ?? DEFAULT_HOST;
        this._oracle = config.oracle ?? DEFAULT_ORACLE;
        this._network = (config.network !== undefined)
            ? parse_network(config.network)
            : DEFAULT_CHAIN;
    }
    get fetcher() {
        return this._fetcher;
    }
    get host() {
        return this._host;
    }
    get network() {
        return this._network;
    }
    toJSON() {
        return { host: this.host };
    }
    toString() {
        return JSON.stringify(this.toJSON());
    }
}
export function get_fetcher(fetcher) {
    return async (config) => {
        const { init, token, url } = config;
        const req = new Request(url, init);
        if (token !== undefined) {
            req.headers.append('Authorization', 'Token ' + token);
        }
        const res = await fetcher(req);
        return resolve_json(res);
    };
}
