"use strict";const s=require("../../common/vendor.js"),c=require("../../api/list.js"),v=require("../../uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js"),u={mixins:[v.MescrollMixin],data(){return{hlsPlayer:null,LIVE_TYPE:{LIVE:1,RECORD:2},list:[],liveId:null,downOption:{offset:80,use:!0,auto:!1},upOption:{use:!0,auto:!0,page:{num:0,size:10}},mescroll:null}},onLoad(){s.index.getStorageSync("AppToken")||s.index.navigateTo({url:"/pages/auth/login"}),this.hlsPlayer&&(this.hlsPlayer.destroy(),this.hlsPlayer=null)},onUnload(){this.list.forEach(e=>{this.$set(e,"isDestroyed",!0),this.pauseAndCleanVideo(e)})},methods:{initVideoPlayer(e){var t;if(!(e.liveType===1?(t=e.flvHlsUrl)==null?void 0:t.includes(".m3u8"):!!e.videoUrl)||!e.liveId)return;const o=`myVideo_${e.liveId}`;console.log(`[视口监听] 初始化:${o}`);const i=s.index.createIntersectionObserver(this,{thresholds:[.2],observeAll:!1});i.relativeTo(".list").observe(`#${o}`,l=>{l.intersectionRatio>0?(console.log(`[视口监听] 进入视口,触发按需播放:${o}`),this.$set(e,"isInView",!0),this.setupVideoPlayback(e)):(console.log(`[视口监听] 离开视口,暂停播放:${o}`),this.$set(e,"isInView",!1),this.pauseAndCleanVideo(e))}),this.$set(e,"observer",i),this.$set(e,"isDestroyed",!1)},pauseAndCleanVideo(e){e.videoContext&&(e.videoContext.pause(),this.$set(e,"videoContext",null)),e.hlsInstance&&(e.hlsInstance.destroy(),this.$set(e,"hlsInstance",null)),e.observer&&(e.observer.disconnect(),this.$set(e,"observer",null)),this.$set(e,"isDestroyed",!0)},setupVideoPlayback(e){const n=e.liveType===this.LIVE_TYPE.LIVE,r=n?e.flvHlsUrl:e.videoUrl;if(!r||e.isDestroyed||n&&!r.includes(".m3u8")){console.warn("[播放校验] 跳过无效播放:",e.liveId,"类型:",n?"直播":"录播");return}const o=`myVideo_${e.liveId}`;console.log(`[按需播放] 触发:${n?"直播":"录播"},ID:${o}`),this.pauseAndCleanVideo(e),n?this.initNativeVideo(e,o,n):this.initNativeVideo(e,o,n)},initNativeVideo(e,n,r){s.index.createSelectorQuery().in(this).select(`#${n}`).fields({context:!0}).exec(o=>{var t;if(!o||!((t=o[0])!=null&&t.context)){console.warn(`[原生视频] 未找到上下文:${n}`);return}const i=o[0].context;if(this.$set(e,"videoContext",i),r)i.play().catch(l=>{console.log(`[直播播放] 自动播放失败(需点击):${e.liveId}`,l)});else{const l=e.nowDuration||0;i.seek(l),i.play().catch(a=>{console.log(`[录播播放] 失败:${e.liveId}`,a)})}})},initHlsPlayer(e){var i;if(e.liveType!==this.LIVE_TYPE.LIVE||!((i=e.flvHlsUrl)!=null&&i.includes(".m3u8"))){console.error(`[HLS 错误] 非直播场景调用:${e.liveId}`);return}const n=e.flvHlsUrl;if(!s.Hls.isSupported()){console.error(`[HLS 错误] 浏览器不支持 HLS:${e.liveId}`);return}const r=document.getElementById(`myVideo_${e.liveId}`);if(!r){console.warn(`[HLS 错误] 未找到视频节点:myVideo_${e.liveId}`);return}const o=new s.Hls({enableWorker:!0,lowLatencyMode:!0,debug:!1,maxBufferLength:3,maxMaxBufferLength:10});o.attachMedia(r),o.loadSource(n),o.on(s.Hls.Events.MANIFEST_PARSED,()=>{console.log(`[HLS 直播] 解析成功:${e.liveId}`),r.play().catch(t=>{console.log(`[HLS 播放] 自动播放失败(需点击):${e.liveId}`,t)})}),o.on(s.Hls.Events.ERROR,(t,l)=>{if(console.error(`[HLS 错误] ${e.liveId}:`,l),l.fatal)switch(l.type){case s.Hls.ErrorTypes.NETWORK_ERROR:o.startLoad();break;default:o.destroy(),this.$set(e,"hlsInstance",null);break}}),this.$set(e,"hlsInstance",o)},mescrollInit(e){this.mescroll=e},downCallback(e){this.list.forEach(n=>{this.pauseAndCleanVideo(n)}),this.list=[],e.resetUpScroll(),this.mescroll.triggerUpScroll()},upCallback(e){const n=e.num;let o={pageSize:e.size,page:n};c.liveList(o).then(i=>{if(i.code==200){let t=i.rows||[],l=i.total||0;n===1&&(this.list=[]),this.list=this.list.concat(t),this.$nextTick(()=>{t.forEach(a=>{this.initVideoPlayer(a)})}),e.endBySize(t.length,l)}else e.endErr(),s.index.showToast({title:i.msg,icon:"none"})}).catch(i=>{e.endErr(),console.log("请求异常:"+JSON.stringify(i))})},goLive(e){this.liveId=e.liveId,console.log("要传的liveId",this.liveId),s.index.navigateTo({url:`/pages/home/living?liveId=${e.liveId}&immediate=true`})}}};Array||s.resolveComponent("mescroll-body")();const h=()=>"../../uni_modules/mescroll-uni/components/mescroll-body/mescroll-body.js";Math||h();function f(e,n,r,o,i,t){return{a:s.f(i.list,(l,a,I)=>s.e({a:l.flvHlsUrl&&l.liveId},l.flvHlsUrl&&l.liveId?{b:"myVideo_"+l.liveId,c:l.flvHlsUrl,d:s.o((...d)=>e.videoError&&e.videoError(...d),a)}:{},{e:l.videoUrl},l.videoUrl?{f:l.videoUrl,g:s.o((...d)=>e.videoError&&e.videoError(...d),a)}:{},{h:s.t(l.liveName),i:s.o(d=>t.goLive(l),a),j:a})),b:s.sr("mescrollRef","7bd28468-0"),c:s.o(t.mescrollInit),d:s.o(t.downCallback),e:s.o(t.upCallback),f:s.p({bottom:"0",down:i.downOption,up:i.upOption})}}const p=s._export_sfc(u,[["render",f],["__scopeId","data-v-7bd28468"]]);wx.createPage(p);