import { validate_registration } from '../../validators/index.js';
import * as assert from '../../assert.js';
function request_deposit_api(client) {
    return async (req) => {
        const arr = Object.entries(req);
        const qry = new URLSearchParams(arr).toString();
        const url = `${client.host}/api/deposit/request?${qry}`;
        return client.fetcher({ url });
    };
}
function register_deposit_api(client) {
    return async (agent_id, return_tx) => {
        const tmpl = { agent_id, return_tx };
        validate_registration({ agent_id, return_tx });
        const url = `${client.host}/api/deposit/register`;
        const init = {
            method: 'POST',
            body: JSON.stringify(tmpl),
            headers: { 'content-type': 'application/json' }
        };
        return client.fetcher({ url, init });
    };
}
function register_funds_api(client) {
    return async (agent_id, return_tx, covenant) => {
        assert.ok(covenant !== undefined, 'covenant is undefined');
        const templ = { agent_id, return_tx, covenant };
        validate_registration(templ);
        const url = `${client.host}/api/deposit/register`;
        const init = {
            method: 'POST',
            body: JSON.stringify(templ),
            headers: { 'content-type': 'application/json' }
        };
        return client.fetcher({ url, init });
    };
}
function read_deposit_api(client) {
    return async (dpid) => {
        assert.is_hash(dpid);
        const url = `${client.host}/api/deposit/${dpid}`;
        return client.fetcher({ url });
    };
}
function list_deposit_api(client) {
    return async (pubkey, token) => {
        const url = `${client.host}/api/deposit/list?pubkey=${pubkey}`;
        return client.fetcher({ url, token });
    };
}
function commit_funds_api(client) {
    return async (dpid, covenant) => {
        const url = `${client.host}/api/deposit/${dpid}/commit`;
        const body = JSON.stringify(covenant);
        const init = {
            body,
            method: 'POST',
            headers: { 'content-type': 'application/json' }
        };
        return client.fetcher({ url, init });
    };
}
function close_deposit_api(client) {
    return async (req) => {
        const dpid = req.dpid;
        const url = `${client._host}/api/deposit/${dpid}/close`;
        const body = JSON.stringify(req);
        const init = {
            body,
            headers: { 'content-type': 'application/json' },
            method: 'POST'
        };
        return client.fetcher({ url, init });
    };
}
export default function (client) {
    return {
        read: read_deposit_api(client),
        list: list_deposit_api(client),
        commit: commit_funds_api(client),
        fund: register_funds_api(client),
        register: register_deposit_api(client),
        request: request_deposit_api(client),
        close: close_deposit_api(client)
    };
}
