import { verify_endorsement } from '../../../lib/member.js';
import { get_proposal_id } from '../../../lib/proposal.js';
import { parse_proposal } from '../../../lib/parse.js';
import { verify_proposal } from '../../../validators/index.js';
import * as assert from '../../../assert.js';
function create_contract_api(client) {
    return async (session) => {
        const { members, proposal, signatures } = session;
        const prop = parse_proposal(proposal);
        verify_proposal(prop);
        if (signatures !== undefined) {
            const prop_id = get_proposal_id(prop);
            signatures.forEach(e => verify_endorsement(prop_id, e, true));
        }
        const url = `${client.host}/api/contract/create`;
        const init = {
            body: JSON.stringify({ members, proposal, signatures }),
            method: 'POST',
            headers: { 'content-type': 'application/json' }
        };
        return client.fetcher({ url, init });
    };
}
function read_contract_api(client) {
    return async (cid) => {
        assert.is_hash(cid);
        const url = `${client.host}/api/contract/${cid}`;
        return client.fetcher({ url });
    };
}
function read_vm_state_api(client) {
    return async (cid) => {
        assert.is_hash(cid);
        const url = `${client.host}/api/contract/${cid}/vmstate`;
        return client.fetcher({ url });
    };
}
function read_contract_digest_api(client) {
    return async (cid) => {
        assert.is_hash(cid);
        const url = `${client.host}/api/contract/${cid}/digest`;
        return client.fetcher({ url });
    };
}
function read_contract_status_api(client) {
    return async (cid) => {
        assert.is_hash(cid);
        const url = `${client.host}/api/contract/${cid}/status`;
        return client.fetcher({ url });
    };
}
function list_contract_api(client) {
    return async (pubkey, token) => {
        const url = `${client.host}/api/contract/list/${pubkey}`;
        const init = {
            method: 'GET',
            headers: { 'Authorization': 'Bearer ' + token }
        };
        return client.fetcher({ url, init });
    };
}
function list_funds_api(client) {
    return async (cid) => {
        assert.is_hash(cid);
        const url = `${client.host}/api/contract/${cid}/funds`;
        return client.fetcher({ url });
    };
}
function list_witness_api(client) {
    return async (cid) => {
        assert.is_hash(cid);
        const url = `${client.host}/api/contract/${cid}/witness`;
        return client.fetcher({ url });
    };
}
function cancel_contract_api(client) {
    return async (cid, token) => {
        assert.is_hash(cid);
        const url = `${client.host}/api/contract/${cid}/cancel`;
        const init = {
            method: 'GET',
            headers: { 'Authorization': 'Bearer ' + token }
        };
        return client.fetcher({ url, init });
    };
}
function submit_witness_api(client) {
    return async (cid, witness) => {
        assert.is_hash(cid);
        const url = `${client._host}/api/contract/${cid}/submit`;
        const init = {
            method: 'POST',
            body: JSON.stringify(witness),
            headers: { 'content-type': 'application/json' }
        };
        return client.fetcher({ url, init });
    };
}
export default function (client) {
    return {
        cancel: cancel_contract_api(client),
        create: create_contract_api(client),
        digest: read_contract_digest_api(client),
        funds: list_funds_api(client),
        list: list_contract_api(client),
        read: read_contract_api(client),
        status: read_contract_status_api(client),
        submit: submit_witness_api(client),
        vmstate: read_vm_state_api(client),
        witness: list_witness_api(client)
    };
}
