import { resolve_json } from '../../lib/oracle.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/witness.js';
const DEFAULT_HOST = 'http://localhost:3000';
const DEFAULT_ORACLE = 'http://172.21.0.3:3000';
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;
    }
    get fetcher() {
        return this._fetcher;
    }
    get host() {
        return this._host;
    }
    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('token', token);
        }
        const res = await fetcher(req);
        return resolve_json(res);
    };
}
