import { __awaiter } from "tslib";
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', (sock, e) => __awaiter(this, void 0, void 0, function* () {
            const sess = this.sipSessionBySipId(e.sip_id);
            if (!sess) {
                return;
            }
            if (sock === this.rpc.instanceId) {
                if (!sess.answered) {
                    yield sess.handleAnswerFromOffer(e.sdp);
                    this.emitSessionMedia(sess);
                }
            }
            else {
                this.emit('localStreams', sess, []);
                this.emit('peerStreams', sess, []);
                yield 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', (call) => __awaiter(this, void 0, void 0, function* () {
            yield this.removeSession(call.sipId || call.id);
        }));
    }
    emitSessionMedia(sess) {
        this.emit('localStreams', sess, sess.getLocalMedia());
        this.emit('peerStreams', sess, sess.getPeerMedia());
    }
    callOption(req = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            return req;
        });
    }
    answer(id, req) {
        return __awaiter(this, void 0, void 0, function* () {
            return false;
        });
    }
    call(req) {
        return __awaiter(this, void 0, void 0, function* () {
            const sess = new Session(this, this.rpc);
            yield sess.call(req.destination);
            this.sessions.push(sess);
            this.emit('newSession', sess);
        });
    }
    register() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield this.rpc.request(`sip_register`, {});
                // tslint:disable-next-line: no-empty
            }
            catch (e) { }
            this.setRegister(true);
            this.emit('connected');
        });
    }
    unregister() {
        return __awaiter(this, void 0, void 0, function* () {
            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;
    }
    removeSession(id) {
        return __awaiter(this, void 0, void 0, function* () {
            for (let i = 0; i < this.sessions.length; i++) {
                if (this.sessions[i].id === id || this.sessions[i].callId === id) {
                    yield 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