import Chat from"./common/chat/chat";import store from"./store/index";import api from"./common/api/index";import util from"./common/utils/utils";let chat;const checkParams=(e,t)=>{for(let a=0,o=t.length;a<o;a++){const o=t[a];if(o.indexOf("|")>-1){const t=o.split("|");if(Object.keys(e).find((e=>e===t[0]||e===t[1])))return}if(void 0===e[o])throw new Error(`缺少参数${o}`)}};function initChat(e={}){e={...store.get("main"),...e};const{userName:t,channelId:a,avatarUrl:o,channelDetail:s,userId:n,param4:r,param5:i,onlyPlayback:c,sessionId:l}=e,{userId:d}=store.get("app"),p={userId:n||d,userName:t,roomName:a,roomId:s.roomId,sessionId:!0===c?l:s.sessionId,accountId:s.userId,pic:o,userType:s.isPPT?"slice":"student",chatToken:s.chatToken,param4:r,param5:i,onlyPlayback:c};return chatClose(),chat=new Chat({...p,micUserId:d}),chat.setup("sdk"),store.set({"main.chat":chat}),chat}function chatClose(){chat&&(chat.disconnectSocket(),chat=null)}const destroy=()=>{chatClose(),store.reset()},logVersion=()=>{console.log("VERSION: 2.5.0")};function setApp(e){if(console.log("VERSION: 2.5.0"),"object"!=typeof e)throw Error("请传入正确格式参数");checkParams(e,["apiId","apiSecret|verifyUrl"]),store.set("app",e)}const _dealDetail=async e=>{const t="Y"===e.playbackEnabled&&e.hasPlayback;e.isPPT="ppt"===e.scene;try{if(t){const{data:t}=await api.getPlayBackVideos({channelId:e.channelId});if(200!==t.code)throw t;e.playbackList=t.data.contents}}catch(e){console.error(e)}return e},_getPolyvUserId=e=>new Promise(((t,a)=>{const o=store.get("main.openId"),s=store.get("app.userId");e===o&&s?t({data:s}):api.getUserId(e).then((e=>{200!==e.data.code&&a(e),t(e.data)}))})),_requestDetail=e=>api.getChannelDetail(e).then((e=>{if(200!==e.data.code)throw e;return e.data}));function init(e){return new Promise(((t,a)=>{util.isObj(e)||a(Error("options must be object!")),checkParams(e,["openId|userId","channelId","userName","avatarUrl"]),e&&e.pptDelayTime||(e.pptDelayTime=3e3),store.set("main",{...e}),store.set({"main.userInfo":{...e}});const{openId:o,channelId:s,immediateChat:n=!0,param4:r,param5:i,onlyPlayback:c,sessionId:l}=e;Promise.all([_getPolyvUserId(o),_requestDetail(s)]).then((async a=>{const[o,s]=a,d=o.data,p=await _dealDetail(s.data);p.useVideo=e.useVideo||e.forceVideo||!1,p.globalInterval=e.globalInterval||!0,p.muted=e.muted||!1,store.set({"app.userId":d,"main.channelDetail":p}),n&&initChat({param4:r,param5:i,onlyPlayback:c,sessionId:l}),t({detail:p,chat:chat})})).catch((e=>{"[object Object]"===Object.prototype.toString.call(e)&&a(e.data),a(e)}))}))}export default{setApp:setApp,init:init,destroy:destroy,initChat:initChat,api:api};