import request from"../utils/request";import EVENT from"./EVENT";const{HIDE_PRE_BTN:HIDE_PRE_BTN,CHANGE_IMG_SRC:CHANGE_IMG_SRC,HIDE_NEXT_BTN:HIDE_NEXT_BTN,SHOW_PRE_BTN:SHOW_PRE_BTN,SHOW_NEXT_BTN:SHOW_NEXT_BTN,PPT_PAGE_CHANGE:PPT_PAGE_CHANGE}=EVENT;class PPT{constructor({isShowBtn:t=!0}={}){this.subFn=[],this.pptID=0,this.maxTeacherOp={autoId:this.pptID,pageId:0},this.changeCurrentPage(0),this.isShowBtn=t}init(t,e=!1){if(!t)return Promise.reject(new Error("初始化ppt时，pptID为空"));if(this.pptID===t)return Promise.resolve(!0);let s=e?`https://doc-2.polyv.net/data/${t}.json`:`https://doc.polyv.net/get/oneJson?autoId=${t}`;return s=s.replace("https://","https://router.polyv.net/proxy/"),this.changeCurrentPage(0),this.isNew=e,this.pptID=t,this.getPPTMsg(s,e)}getPPTMsg(t,e){return request(t).then((t=>{const{statusCode:s,data:r}=t;if(200!==s)return Promise.reject(new Error("获取ppt数据失败"));const o=r,i=e?o.convertFileJson:JSON.parse(o.jsonContent);return this.operation=this.staticPhotoControl(i.images),!0})).catch(console.error)}staticPhotoControl(t){if(!t)return void console.warn("请求ppt接口，返回数据为空");const e=this.total=t.length;if(!(t&&e>0))return;let s=0;t=t.reduce(((t,e)=>(t.push(PPT.getUrlPrefix(e)),t)),[]),this.emit({EVENT:CHANGE_IMG_SRC,src:t[0]});const r=this;return({op:t,fun:e,args:s=[]})=>{switch(t){case"gotoNextStep":o({fun:e});break;case"gotoPreviousStep":o({fun:e,dir:-1});break;case"gotoSlide":o({fun:e,page:s[0]})}};function o({fun:o,dir:i=1,page:n}={}){isNaN(parseInt(n))?-1===i?s<=0?s=e-1:s--:s>=e-1?s=0:s++:s=parseInt(n),r.emit({EVENT:CHANGE_IMG_SRC,src:t[s]}),r.changeCurrentPage(s),o&&o()}}subscribe(t){t instanceof Function&&this.subFn.push(t)}emit(t){this.subFn.forEach((e=>e(t)))}static getUrlPrefix(t){return t.replace(/^[^:]*:/,"https:")}gotNextPage(){this.operation&&this.operation({op:"gotoNextStep"})}gotPreviousPage(){this.operation&&this.operation({op:"gotoPreviousStep"})}gotoPage(t){parseInt(this.currentPageNum)!==parseInt(t)&&this.operation&&(this.changeCurrentPage(t),this.operation({op:"gotoSlide",args:[t]}))}changeCurrentPage(t){this.currentPageNum=t;const{autoId:e,pageId:s}=this.maxTeacherOp;(this.pptID!==e||t>s)&&(this.maxTeacherOp={autoId:this.pptID,pageId:t}),this.emit({EVENT:PPT_PAGE_CHANGE,page:t}),this.isShowBtn&&(0===t?this.emit({EVENT:HIDE_PRE_BTN}):t===this.total-1?this.emit({EVENT:HIDE_NEXT_BTN}):(this.emit({EVENT:SHOW_PRE_BTN}),this.emit({EVENT:SHOW_NEXT_BTN})))}}export default PPT;