import aesjs from"../utils/aes-js";import{Base64}from"../utils/js-base64";import EventEmitter from"../chat/events";import api from"../api/index";const AgoraMiniappSDK=require("../utils/agora-miniapp-sdk"),key="polyvliveSDKAuth",iv="1111000011110000";class VideoChat{videoChatEvent(){return[{type:"stream-added",handler(e){this.stream[`chat${e.uid}`]=e,this.trigger("stream-added",e.uid)}},{type:"stream-removed",handler(e){this.trigger("stream-removed",e)}},{type:"peer-leave",handler(e){this.trigger("peer-leave",e)}},{type:"stream-published",handler(e){this.trigger("stream-published",e)}},{type:"error",handler(e){this.trigger("contactError",e)}}]}initEvent(){this.videoChatEvent().forEach((e=>{this.channel.client.on(e.type,e.handler.bind(this))}))}decryptChannelKey(e,t,i){const s=aesjs.utils.utf8.toBytes(t),n=aesjs.utils.utf8.toBytes(i),r=aesjs.utils.hex.toBytes(e),a=new aesjs.ModeOfOperation.cbc(s,n).decrypt(r),h=aesjs.utils.utf8.fromBytes(a);return JSON.parse(Base64.decode(h))}static getUid(e){return parseInt(e/1%Math.pow(2,32))}async initClient(){const e=new AgoraMiniappSDK.Client,t=await this.getChannelKey(this.channelId);t.uid=this.uid;await new Promise((i=>{e.init(t.connect_appId,(()=>{i()}))}));return this.channel={client:e,data:t}}joinChannel(e){const{client:t,data:i}=e;return new Promise(((e,s)=>{t.join(i.connect_channel_key,i.channelId,i.uid,(t=>{e(!0)}),(e=>{s(e),109===e&&this.getChannelKey(this.channelId).then((e=>{t.renewChannelKey(e.connect_channel_key)}))}))}))}leave(){return new Promise(((e,t)=>{this.unPublishStream(),this.channel.client.leave((()=>{e()}),(e=>{t(e)}))}))}addStream(e=function(){}){this.streamStatus="STREAMINIT",this.trigger("stream-init"),e(this.channel.client)}publishStream(){const{client:e}=this.channel;return new Promise(((t,i)=>{e.publish((e=>{this.trigger("stream-published"),t(e)}),(e=>{i(e)})),this.streamStatus="PUBLISH"}))}unPublishStream(){const{client:e}=this.channel;e.unpublish(),this.streamStatus="UNPUBLIST"}subscribe(e){this.channel.client.subscribe(e,(t=>{this.trigger("stream-subscribed",{url:t,uid:e})}))}unsubscribe(e){this.channel.client.unsubscribe(uid)}muteLocal(e){this.channel.client.muteLocal(e,(()=>{this.trigger("mute-local",{status:"success",type:e})}),(()=>{this.trigger("mute-local",{status:"fail",type:e})}))}unmuteLocal(e){this.channel.client.unmuteLocal(e,(()=>{this.trigger("unmute-local",{status:"success",type:e})}),(()=>{this.trigger("unmute-local",{status:"fail",type:e})}))}getChannelKey(){return api.getChannelKey(this.channelId).then((e=>this.decryptChannelKey(e.data.data,key,iv)))}static requestPermissions(){return new Promise(((e,t)=>{wx.getSetting({success(i){i.authSetting["scope.record"]?e():wx.authorize({scope:"scope.record",success(){e()},fail(e){t("获取摄像头失败")}})}})}))}constructor(e){this.channelId=e.roomName,this.micUserId=e.micUserId,this.uid=VideoChat.getUid(e.micUserId),this.options=e,this.channel={},this.stream={},this.streamStatus="NOT_READY";const t=this;var i=this.observer=new EventEmitter;i.trigger=function(e,...s){return i.emit(e,...s),t},i.off=function(e,...s){return i.removeListener(e,...s),t},this.on=function(e,s){return i.on(e,s),t},this.off=i.off.bind(i),this.trigger=i.trigger.bind(i)}}export default VideoChat;