import { Buff } from '@cmdcode/buff';
import { Signer, Wallet } from '@cmdcode/signer';
import { EscrowClient } from './client.js';
import deposit_api from '../api/depositor.js';
import member_api from '../api/member.js';
import proposal_api from '../api/proposal.js';
import request_api from '../api/request.js';
import witness_api from '../api/witness.js';
const DEFAULT_IDXGEN = () => Buff.now(4).num;
export class EscrowSigner {
    static create(config, seed, xpub) {
        const signer = new Signer({ seed });
        const wallet = (xpub !== undefined)
            ? new Wallet(xpub)
            : Wallet.generate({ seed, network: config.network });
        return new EscrowSigner({ ...config, signer, wallet });
    }
    static load(config, password, payload) {
        const bytes = Buff.bech32(payload);
        const encdata = bytes.subarray(0, 64);
        const xpub = bytes.subarray(64).b58chk;
        const pass = Buff.str(password);
        const signer = Signer.restore(pass, encdata);
        const wallet = new Wallet(xpub);
        return new EscrowSigner({ ...config, signer, wallet });
    }
    constructor(config) {
        this.deposit = deposit_api(this);
        this.membership = member_api(this);
        this.proposal = proposal_api(this);
        this.request = request_api(this);
        this.witness = witness_api(this);
        this._client = new EscrowClient(config);
        this._gen_idx = config.idxgen ?? DEFAULT_IDXGEN;
        this._host_pub = config.host_pub;
        this._signer = config.signer;
        this._wallet = config.wallet;
    }
    get client() {
        return this._client;
    }
    get host_pub() {
        return this._host_pub;
    }
    get pubkey() {
        return this._signer.pubkey;
    }
    save(password) {
        const pass = Buff.str(password);
        const encdata = this._signer.backup(pass);
        const xbytes = Buff.b58chk(this._wallet.xpub);
        const payload = Buff.join([encdata, xbytes]);
        return payload.to_bech32('cred');
    }
}
