import { EventEmitter } from './emitter.js';
export class NostrSub extends EventEmitter {
    constructor(socket, config) {
        super();
        this._echo = config.selfsub ?? false;
        this._socket = socket;
        this._sub_id = config.sub_id;
        this._envelope = config.envelope ?? {};
        this._filter = config.filter ?? this._socket.opt.filter;
        this._init = false;
    }
    get envelope() {
        return this._envelope;
    }
    get filter() {
        return this._filter;
    }
    get id() {
        return this._sub_id;
    }
    get is_echo() {
        return this._echo;
    }
    get is_ready() {
        return this._init;
    }
    cancel() {
        this._socket.cancel(this.id);
    }
    on_msg(subject, handler) {
        this.on('message', msg => {
            if (msg.subject === subject) {
                handler(msg);
            }
        });
    }
    send(subject, body, envelope) {
        envelope = { ...this.envelope, ...envelope };
        return this._socket.send(subject, body, envelope);
    }
    async update(filter) {
        filter = { ...this.filter, ...filter };
        const envelope = this.envelope;
        const sub_id = this.id;
        const config = { envelope, filter, sub_id };
        return this._socket.subscribe(config);
    }
    when_cancel() {
        return this._socket.when_cancel(this.id);
    }
    when_ready() {
        return this._socket.when_sub(this.id);
    }
}
