import { now, sleep } from '../../lib/util.js';
import { validate_deposit } from '../../validators/deposit.js';
import { EventEmitter } from './emitter.js';
import { update_deposit } from '../../lib/deposit.js';
const DEFAULT_CONFIG = {
    refresh_ival: 10,
    verbose: true
};
export class EscrowDeposit extends EventEmitter {
    static async create(client, account, utxo, config) {
        const req = { ...account, utxo };
        const res = await client.deposit.register(req);
        if (!res.ok)
            throw new Error(res.error);
        const dat = res.data.deposit;
        return new EscrowDeposit(client, dat, config);
    }
    static async fetch(client, dpid, config) {
        const res = await client.deposit.read(dpid);
        if (!res.ok)
            throw new Error(res.error);
        const dat = res.data.deposit;
        return new EscrowDeposit(client, dat, config);
    }
    constructor(client, deposit, config) {
        super();
        this._client = client;
        this._opt = { ...DEFAULT_CONFIG, ...config };
        this._data = deposit;
        this._init = false;
        this._updated = deposit.updated_at;
    }
    get dpid() {
        return this.data.dpid;
    }
    get client() {
        return this._client;
    }
    get data() {
        return this._data;
    }
    get opt() {
        return this._opt;
    }
    get is_stale() {
        const ival = this.opt.refresh_ival;
        return this._updated < now() - ival;
    }
    get status() {
        return this.data.status;
    }
    get updated_at() {
        return this._updated;
    }
    async _fetch(force = false) {
        try {
            if (this.is_stale || force) {
                const res = await this._status();
                if (res.updated) {
                    this._update(res.deposit);
                }
                else {
                    this._updated = now();
                }
                this.emit('fetch', this);
            }
            if (!this._init) {
                this._init = true;
                this.emit('status', this.status);
            }
        }
        catch (err) {
            this.emit('error', err);
        }
        return this;
    }
    async _read() {
        const api = this.client.deposit;
        const res = await api.read(this.dpid);
        if (!res.ok)
            throw new Error(res.error);
        const dep = res.data.deposit;
        validate_deposit(dep);
        return dep;
    }
    async _status() {
        const api = this.client.deposit;
        const res = await api.status(this.dpid);
        if (!res.ok)
            throw new Error(res.error);
        return res.data;
    }
    async _update(updated) {
        const changed = (updated.status !== this.status);
        try {
            const data = this.data;
            this._data = await update_deposit(data, updated);
            this._updated = now();
            if (changed) {
                if (!this._init)
                    this._init = true;
                this.emit('status', updated.status);
            }
            this.emit('update', this);
        }
        catch (err) {
            this.emit('error', err);
        }
    }
    async close(signer, txfee) {
        const api = this.client.deposit;
        const req = signer.account.close(this.data, txfee);
        const res = await api.close(this.dpid, req);
        if (!res.ok)
            throw new Error(res.error);
        this._update(res.data.deposit);
        return this;
    }
    async fetch(force) {
        return this._fetch(force);
    }
    async lock(contract, signer) {
        const api = this.client.deposit;
        const req = signer.account.lock(contract, this.data);
        const res = await api.lock(this.dpid, req);
        if (!res.ok)
            throw new Error(res.error);
        this._update(res.data.deposit);
        return this;
    }
    async poll(status, retries) {
        for (let i = 0; i < retries; i++) {
            await this.fetch(true);
            if (this.status === status) {
                return this;
            }
            await sleep(this.opt.refresh_ival * 1000);
        }
        throw new Error('polling timed out');
    }
    toJSON() {
        return this.data;
    }
    toString() {
        return JSON.stringify(this.data);
    }
}
