import { RTC } from './rtc';
export class Session {
    constructor(cli, rpc) {
        this.cli = cli;
        this.rtc = new RTC();
        this.id = '';
        this.answered = false;
        this.incoming = false;
        this.rpc = rpc;
    }
    async call(destination = '') {
        await this.rtc.initLocalMediaStream();
        await this.rtc.createOffer();
        const result = await this.rpc.request(`call_sdp`, {
            destination,
            sdp: this.rtc.localSdp,
        });
        this.id = result.sip_id;
    }
    async answer(s) {
        return new Promise(async () => {
            const { callId } = s;
            if (!this.callId) {
                this.callId = callId;
            }
            const result = await this.rpc.request(`call_sdp_remote`, {
                id: this.callId,
            });
            await this.rtc.initLocalMediaStream();
            await this.rtc.createAnswer(result.sdp);
            await this.rpc.request(`call_sdp_answer`, {
                id: this.callId,
                sdp: this.rtc.localSdp,
            });
            this.cli.emitSessionMedia(this);
        });
    }
    async recovery(callId) {
        await this.rtc.initLocalMediaStream();
        await this.rtc.createOffer();
        const result = await this.rpc.request(`call_sdp_recovery`, {
            id: callId,
            sdp: this.rtc.localSdp,
        });
        this.id = result.sip_id;
        return this.id;
    }
    get localStream() {
        return this.rtc.localStream;
    }
    get peerStream() {
        return this.rtc.peerStream;
    }
    getLocalMedia() {
        if (this.localStream) {
            return [this.localStream];
        }
        return [];
    }
    getPeerMedia() {
        if (this.peerStream) {
            return [this.peerStream];
        }
        return [];
    }
    async destroy() {
        this.rtc.clean();
    }
    async handleAnswerFromOffer(answer) {
        if (this.answered) {
            throw new Error('Answered');
        }
        this.answered = true;
        await this.rtc.handleAnswerFromOffer(answer);
    }
}
//# sourceMappingURL=session.js.map