import { create_txhex } from './tx.js';
import { now, sort_record } from './util.js';
import { DEFAULT_DEADLINE } from '../config.js';
import { init_vm } from '../vm/main.js';
import { get_path_names, get_path_vouts, get_pay_total, get_proposal_id, } from './proposal.js';
export function create_contract(config) {
    const terms = config.proposal;
    const fees = config.fees ?? [];
    const published = config.published ?? now();
    const signatures = config.signatures ?? [];
    return sort_record({
        ...config.agent,
        activated: null,
        balance: 0,
        cid: config.cid,
        deadline: get_deadline(terms, published),
        expires_at: null,
        fees: fees,
        moderator: config.moderator ?? null,
        outputs: get_spend_outputs(terms, fees),
        pending: 0,
        prop_id: get_proposal_id(terms).hex,
        pubkeys: signatures.map(e => e.slice(0, 64)),
        published: published,
        settled: false,
        settled_at: null,
        signatures: signatures,
        spent: false,
        spent_at: null,
        spent_txid: null,
        status: 'published',
        terms: terms,
        total: terms.value + get_pay_total(fees),
        updated_at: published,
        vm_state: null
    });
}
export function activate_contract(contract, activated = now()) {
    const { cid, published, terms } = contract;
    const { expires, paths, programs, schedule } = terms;
    return {
        ...contract,
        activated,
        expires_at: activated + expires,
        status: 'active',
        vm_state: init_vm({ cid, paths, programs, published, schedule })
    };
}
function get_deadline(proposal, created) {
    const { deadline, effective } = proposal;
    if (effective !== undefined) {
        return effective - created;
    }
    else {
        return created + (deadline ?? DEFAULT_DEADLINE);
    }
}
export function get_spend_outputs(prop, fees) {
    const { payments, paths } = prop;
    const total_fees = [...payments, ...fees];
    const path_names = get_path_names(paths);
    const outputs = [];
    for (const name of path_names) {
        const vout = get_path_vouts(name, paths, total_fees);
        const txhex = create_txhex(vout);
        outputs.push([name, txhex]);
    }
    return outputs;
}
