import { endorse_proposal } from '../../lib/proposal.js';
import { sign_witness } from '../../lib/witness.js';
import { EscrowContract } from '../../client/class/contract.js';
import { EscrowProposal } from '../../client/class/proposal.js';
import { validate_witness } from '../../validators/program.js';
import { validate_proposal, verify_proposal } from '../../validators/proposal.js';
export function endorse_proposal_api(client) {
    return (proposal) => {
        if (proposal instanceof EscrowProposal) {
            proposal = proposal.data;
        }
        validate_proposal(proposal);
        verify_proposal(proposal);
        return endorse_proposal(proposal, client._signer);
    };
}
export function endorse_request_api(client) {
    return (url, body = '{}', method = 'GET') => {
        const content = method + url + body;
        return client._signer.gen_token(content);
    };
}
export function endorse_witness_api(client) {
    return (contract, witness) => {
        if (contract instanceof EscrowContract) {
            contract = contract.data;
        }
        const cred = client.get_membership(contract.terms);
        validate_witness(contract, witness);
        return sign_witness(cred.signer, witness);
    };
}
export default function (client) {
    return {
        proposal: endorse_proposal_api(client),
        request: endorse_request_api(client),
        witness: endorse_witness_api(client)
    };
}
