import { EventEmitter } from 'ee-ts';
import { Session } from './session';
const version = '0.0.1';
export class SipPhone extends EventEmitter {
    constructor(rpc) {
        super();
        this.rpc = rpc;
        this.type = 'webrtc';
        this.sessions = [];
        this.registered = false;
        this.ua = {};
        this.rpc.on('sdp', async (sock, e) => {
            const sess = this.sipSessionBySipId(e.sip_id);
            if (!sess) {
                return;
            }
            if (sock === this.rpc.instanceId) {
                if (!sess.answered) {
                    await sess.handleAnswerFromOffer(e.sdp);
                    this.emitSessionMedia(sess);
                }
            }
            else {
                this.emit('localStreams', sess, []);
                this.emit('peerStreams', sess, []);
                await this.removeSession(sess.id);
            }
        });
        this.rpc.on('call_receive', (call) => {
            if (call.direction === 'inbound' || call.originate) {
                const sess = new Session(this, this.rpc);
                sess.callId = call.id;
                this.sessions.push(sess);
                call.setSip(sess);
            }
        });
        this.rpc.on('call_hangup', async (call) => {
            await this.removeSession(call.sipId || call.id);
        });
    }
    emitSessionMedia(sess) {
        this.emit('localStreams', sess, sess.getLocalMedia());
        this.emit('peerStreams', sess, sess.getPeerMedia());
    }
    async callOption(req = {}) {
        return req;
    }
    async answer(id, req) {
        return false;
    }
    async call(req) {
        const sess = new Session(this, this.rpc);
        await sess.call(req.destination);
        this.sessions.push(sess);
        this.emit('newSession', sess);
    }
    async register() {
        try {
            await this.rpc.request(`sip_register`, {});
            // tslint:disable-next-line: no-empty
        }
        catch (e) { }
        this.setRegister(true);
        this.emit('connected');
    }
    async unregister() {
        this.setRegister(false);
    }
    isRegistered() {
        return this.registered;
    }
    sipSessionByCallId(id) {
        for (const v of this.sessions) {
            if (v.callId === id) {
                return v;
            }
        }
        return null;
    }
    sipSessionBySipId(id) {
        for (const v of this.sessions) {
            if (v.id === id) {
                return v;
            }
        }
        return null;
    }
    async removeSession(id) {
        for (let i = 0; i < this.sessions.length; i++) {
            if (this.sessions[i].id === id || this.sessions[i].callId === id) {
                await this.sessions[i].destroy();
                this.sessions.splice(i, 1);
                // break
            }
        }
    }
    setRegister(val) {
        this.registered = val;
        if (this.registered) {
            this.emit('registered');
        }
        else {
            this.emit('unregistered');
        }
    }
}
SipPhone.userAgent = `Webitel-Phone/${version}`;
SipPhone.sipVersion = version;
//# sourceMappingURL=client.js.map