import { EventEmitter } from './emitter.js';
import { find_program, get_proposal_id } from '../../lib/proposal.js';
import { add_membership, rem_membership } from '../../lib/policy.js';
import { parse_proposal } from '../../lib/parse.js';
export class EscrowProposal extends EventEmitter {
    constructor(data) {
        super();
        this._data = parse_proposal(data);
    }
    get copy() {
        return new EscrowProposal(this.data);
    }
    get data() {
        return this._data;
    }
    get id() {
        return get_proposal_id(this.data).hex;
    }
    _update(data) {
        this._data = parse_proposal(data);
        this.emit('update', this);
    }
    join(role, member, index) {
        const mdata = member.gen_membership(index);
        this.add_membership(mdata, role);
    }
    leave(member) {
        const mship = member.get_membership(this);
        this.rem_membership(mship.token);
    }
    get_program(query) {
        return find_program(query, this.data.programs);
    }
    add_membership(mship, role) {
        const prop = add_membership(mship, role, this.data);
        this._update(prop);
    }
    rem_membership(mship, leave = true) {
        const prop = rem_membership(mship, this.data, leave);
        this._update(prop);
    }
    toJSON() {
        return this.data;
    }
    toString() {
        return JSON.stringify(this.data);
    }
}
