import { Buff } from '@cmdcode/buff';
import { EscrowClient } from './client.js';
import { Seed, Signer, Wallet } from '@cmdcode/signer';
import account_api from '../api/signer/account.js';
import fetch_api from '../api/signer/fetch.js';
import member_api from '../api/signer/member.js';
import draft_api from '../api/signer/draft.js';
import request_api from '../api/signer/request.js';
import statement_api from '../api/signer/statement.js';
import wallet_api from '../api/signer/wallet.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 generate(config, xpub) {
        const seed = Buff.random(32);
        return EscrowSigner.create(config, seed, xpub);
    }
    static import(config, xpub) {
        return {
            from_phrase: (phrase, salt) => {
                const seed = Seed.import.from_char(phrase, salt);
                return EscrowSigner.create(config, seed, xpub);
            },
            from_words: (words, password) => {
                const seed = Seed.import.from_words(words, password);
                return EscrowSigner.create(config, seed, xpub);
            }
        };
    }
    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 });
    }
    static { this.util = {
        gen_seed: Seed.generate.bytes,
        gen_words: Seed.generate.words
    }; }
    constructor(config) {
        this.account = account_api(this);
        this.credential = member_api(this);
        this.draft = draft_api(this);
        this.fetch = fetch_api(this);
        this.request = request_api(this);
        this.wallet = wallet_api(this);
        this.witness = statement_api(this);
        this._client = new EscrowClient(config);
        this._gen_idx = config.idxgen ?? DEFAULT_IDXGEN;
        this._host_pub = config.host_pubkey;
        this._signer = config.signer;
        this._wallet = config.wallet;
    }
    get client() {
        return this._client;
    }
    get host_pub() {
        return this._host_pub;
    }
    get network() {
        return this._client.network;
    }
    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');
    }
}
