import{initializeApp}from"https://www.gstatic.com/firebasejs/11.6.0/firebase-app.js";import{getRemoteConfig,fetchAndActivate,getString,setCustomSignals,getBoolean}from"https://www.gstatic.com/firebasejs/11.6.0/firebase-remote-config.js";import{getAnalytics}from"https://www.gstatic.com/firebasejs/11.6.0/firebase-analytics.js";const defaultLivestreamsConfig={desktop:{bottom:"100px",left:"100px"},mobile:{bottom:"104px",left:"16px"}},firebaseConfig={apiKey:"AIzaSyCLlA8NHd2Oi_1zARkkx5E97HRSSGlSkXY",authDomain:"live-d3be0.firebaseapp.com",projectId:"live-d3be0",storageBucket:"live-d3be0.firebasestorage.app",messagingSenderId:"968972992941",appId:"1:968972992941:web:249d3fb791439f5742e124"};let token,liveId,isLive=!1,isRenderedLivestreams=!1,livestreamIframeURL="",floatingImageUrl="",floatingPosition=defaultLivestreamsConfig,isClickedCloseFloatingImage=!1,urlsToHideLivestreams="?modal=deposit,/account",URL_FULLSTREAM="/livestream",VIDEO_POSTER_URL="/bmp/load-img-live.webp",isShowFloatingIcon=!0,isOpenInNewTab=!1,isRedirectIfNoEvents=!0,API_URL="";function isMobile(){return[/Android/i,/webOS/i,/iPhone/i,/iPad/i,/iPod/i,/BlackBerry/i,/Windows Phone/i].some(a=>navigator.userAgent.match(a))}function getTokenFromURL(a){const b=new URL(a);return b.searchParams.get("token")}function getLiveIdFromURL(a){const b=new URL(a);return b.searchParams.get("liveId")}const checkIsLivestreamsById=async({token:a,liveId:b})=>{let c;return a&&(c=`https://player-backend.restream.io/public/status-connection-data/${a}`),b&&(c=`/api/v1/livestreamstatus?liveId=${b}`),!!c&&(await fetch(c,{method:"GET"}).then(a=>a.json()).then(a=>a?.eventStatus||0(console.error(a),!1)))},observeUrlChange=a=>{let b=document.location.href;const c=document.querySelector("body");a();const d=new MutationObserver(()=>{b!==document.location.href&&(b=document.location.href,a())});d.observe(c,{childList:!0,subtree:!0})};let intervalCheckIsLive=null;const liveplugin=async()=>{const a=initializeApp(firebaseConfig,"livestreams");getAnalytics(a);const b=getRemoteConfig(a);b.settings={minimumFetchIntervalMillis:6e4},await setCustomSignals(b,{domain:window.location.hostname});try{if(await fetchAndActivate(b),console.log("Remote Config fetched and activated"),API_URL=(await getString(b,"API_URL"))||API_URL,livestreamIframeURL=await getString(b,"livestream_iframe_url"),floatingImageUrl=await getString(b,"floatingImg"),floatingPosition=JSON.parse((await getString(b,"defaultFloatingPosition"))||"null")||defaultLivestreamsConfig,urlsToHideLivestreams=(await getString(b,"urlsToHideLivestreams"))||"",URL_FULLSTREAM=(await getString(b,"URL_FULLSTREAM"))||"/livestream",VIDEO_POSTER_URL=await getString(b,"VIDEO_POSTER_URL"),token=getTokenFromURL(livestreamIframeURL),liveId=getLiveIdFromURL(livestreamIframeURL),isLive=await checkIsLivestreamsById({token,liveId}),console.debug("Is Live:",isLive),isShowFloatingIcon=(await getBoolean(b,"isShowFloatingIcon"))||isShowFloatingIcon,isOpenInNewTab=(await getBoolean(b,"isOpenInNewTab"))||isOpenInNewTab,isRedirectIfNoEvents=await getBoolean(b,"isRedirectIfNoEvents"),intervalCheckIsLive&&clearInterval(intervalCheckIsLive),intervalCheckIsLive=setInterval(async()=>{isLive=await checkIsLivestreamsById({token,liveId}),isLive?!isRenderedLivestreams&&initializeLivestreams({floatingImageUrl,livestreamIframeURL,floatingPosition}):document.location.pathname===URL_FULLSTREAM&&window.location.reload()},3e5),isLive)return void initializeLivestreams({floatingImageUrl,livestreamIframeURL,floatingPosition});observeUrlChange(()=>{isRedirectIfNoEvents&&document.location.pathname===URL_FULLSTREAM&&(window.location.href="/")})}catch(a){console.error("Error fetching Remote Config:",a)}};liveplugin();let iframeContainer,floatingImageContainer;async function getUserData(){try{const a=await fetch(`${API_URL}/api/v1/refresh`),b=await a.json();return b?.data}catch(a){return null}}const initializeLivestreams=({floatingImageUrl:a,livestreamIframeURL:b,floatingPosition:c})=>{if(!token&&!liveId)return void console.error("Token or LiveId not found in URL",token,liveId);if(!b)return void console.error("Missing Livestream URL");console.log("Initializing livestreams...");const d=document.createElement("script");d.src="https://cdn.jsdelivr.net/npm/jwt-decode@4.0.0/build/cjs/index.min.js",document.head.appendChild(d);const e=isMobile()?c.mobile:c.desktop,f=document.createElement("style");f.textContent=` .livestreams-floating-image { position: fixed; z-index: 999; bottom: 100px; left: 100px; cursor: grab; } .livestreams-floating-image img { width: 120px; } .livestreams-floating-image .close-button { z-index: 1000; position: absolute; right: 0; top: 0; } .livestreams-floating-image .close-button svg { width: 24px; height: 24px; } .livestreams-more-button { position: absolute; bottom: -24px; left: -1px; border: none; cursor: pointer; width: 343px; height: 24px; z-index: 10001; padding: 4px 0; border-radius: 0 0 8px 8px; display: flex; justify-content: center; align-items: center; color: rgb(255, 199, 89); border: 1px solid var(--stroke-filled, #FFAA20); border-width: 0 1px 1px 1px; gap: 4px; background: rgba(0, 0, 0, 0.6); @media (max-width: 768px) { width: 220px; } } .livestreams-more-button a { color: rgb(255, 199, 89); display: flex; align-items: center; gap: 4px; } .livestreams-container { box-sizing: border-box; position: fixed; width: 343px; aspect-ratio: 16 / 9; height: fit-content; z-index: 1000; border-radius: 8px 8px 0 0; border: 1px solid var(--stroke-filled, #FFAA20); background: rgba(0, 0, 0, 0.8); box-shadow: rgba(0, 0, 0, 0.2) 0px 4px 8px; display: flex; justify-content: center; align-items: center; @media (max-width: 768px) { width: 220px; } } .livestreams-container iframe { width: 100%; height: 100%; border: none; border-radius: 8px 8px 0 0; background-image: url("${VIDEO_POSTER_URL}"); } .livestreams-container .close-button, .livestream-floating-container .close-button { position: absolute; top: 5px; right: 5px; border: none; cursor: pointer; z-index: 10001; } .livestreams-container .close-mobile { top: -20px; right: -20px; box-shadow: rgba(0, 0, 0, 0.2) 0px 4px 8px; border-radius: var(--Rounding-circle, 999px); background: rgba(0, 0, 0, 0.2); padding: 2px; } .livestreams-container .close-button svg, .livestream-floating-container .close-button svg { width: 20px; height: 20px; } .livestreams-container .bet-now-button { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); border: none; cursor: pointer; z-index: 10001; padding: 4px 0; display: flex; justify-content: center; align-items: center; color: var(--Active, #94310C); gap: 4px; padding: 10px 50px; font-weight: 600; border-radius: var(--Rounding-circle, 999px); background: linear-gradient(90deg, var(--Active-bg-left, #FFEEC6) 0%, var(--Active-bg-right, #FFC759) 100%); box-shadow: 0px 7px 4px 0px var(--dropdown-1, rgba(194, 0, 0, 0.05)), 0px 3px 3px 0px var(--dropdown-2, rgba(194, 0, 0, 0.09)), 0px 1px 2px 0px var(--dropdown-3, rgba(194, 0, 0, 0.10)); } .fullscreen { position: fixed; top: 0px !important; left: 0px !important; width: 100vw; height: 100dvh; border-radius: 0px; border: none; background: black; z-index: 92147483648; padding: 40px 0px 80px; } .livestreams-page { width: 100%; height: 100%; } `,document.head.appendChild(f),console.log("Livestreams initialized"),floatingImageContainer=document.createElement("div"),floatingImageContainer.id="livestream-floating-container",floatingImageContainer.className="livestreams-floating-image",floatingImageContainer.style.bottom=e.bottom,floatingImageContainer.style.left=e.left;const g=document.createElement("img");g.src=a,g.alt="Floating Image";const h=document.createElement("div");h.textContent="Close",h.className="close-button",h.innerHTML=` `,h.addEventListener("click",()=>{document.body.removeChild(floatingImageContainer),isClickedCloseFloatingImage=!0});const i=document.createElement("div");i.style.position="relative",i.appendChild(h),i.appendChild(g),isShowFloatingIcon&&floatingImageContainer.appendChild(i);let j,k,l=!1;const m=(a,b)=>{floatingImageContainer.style.left=`${a}px`,floatingImageContainer.style.top=`${b}px`,iframeContainer.style.left=`${a}px`,iframeContainer.style.top=`${b}px`};floatingImageContainer.addEventListener("mousedown",a=>{l=!0,j=a.clientX-floatingImageContainer.offsetLeft,k=a.clientY-floatingImageContainer.offsetTop,floatingImageContainer.style.cursor="grabbing",a.preventDefault()}),document.addEventListener("mousemove",a=>{if(l){const b=a.clientX-j,c=a.clientY-k;m(b,c),a.preventDefault()}}),document.addEventListener("mouseup",()=>{l=!1,floatingImageContainer.style.cursor="grab"}),document.addEventListener("mousemove",a=>{if(l){const b=a.clientX-j,c=a.clientY-k;m(b,c)}}),document.addEventListener("mouseup",()=>{l=!1,iframeContainer.style.cursor="grab"});const n=a=>{let b,c;a.addEventListener("touchstart",d=>{l=!0;const e=d.touches[0];b=e.clientX-a.offsetLeft,c=e.clientY-a.offsetTop}),a.addEventListener("touchmove",a=>{if(l){const d=a.touches[0],e=d.clientX-b,f=d.clientY-c;m(e,f),a.preventDefault()}}),a.addEventListener("touchend",()=>{l=!1})};console.log("Livestreams loaded");const o=document.createElement("div");o.className="livestreams-more-button";const p=document.createElement("a");p.textContent="More",p.innerHTML=` Xem thêm `,isOpenInNewTab?(p.href="#",p.addEventListener("click",async a=>{a.preventDefault();const b=await getUserData(),c=b?.token;if(c){const a=jwtDecode(c,{payload:!0});window.open(`${URL_FULLSTREAM}login?token=${encodeURIComponent(a.token)}`,"_blank")}else window.open(URL_FULLSTREAM,"_blank")})):p.href=URL_FULLSTREAM,o.appendChild(p),iframeContainer=document.getElementById("livestream-iframe-container");const q=document.createElement("iframe");if(q.src=b,q.allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture",q.allowFullscreen=!0,q.frameBorder="0",q.style["background-image"]=`url("${VIDEO_POSTER_URL}")`,q.style["background-repeat"]="no-repeat",q.style["background-size"]="cover",!iframeContainer){iframeContainer=document.createElement("div"),iframeContainer.id="livestream-iframe-container",iframeContainer.className="livestreams-container",iframeContainer.style.bottom=e.bottom,iframeContainer.style.left=e.left,iframeContainer.appendChild(q);const a=document.createElement("div");a.textContent="Close",a.className="close-button close-mobile",a.innerHTML=` `,a.addEventListener("click",()=>{document.body.removeChild(iframeContainer),document.body.appendChild(floatingImageContainer)}),iframeContainer.appendChild(a);const b=document.createElement("div");b.textContent="Close",b.className="close-button",b.innerHTML=` `,b.addEventListener("click",()=>{iframeContainer.classList.remove("fullscreen"),iframeContainer.appendChild(o),iframeContainer.appendChild(a),iframeContainer.style.bottom=e.bottom,iframeContainer.style.left=e.left,iframeContainer.style.top="unset";try{iframeContainer.removeChild(b)}catch(a){}}),iframeContainer.appendChild(o),iframeContainer.addEventListener("mousedown",a=>{l=!0,j=a.clientX-iframeContainer.offsetLeft,k=a.clientY-iframeContainer.offsetTop,iframeContainer.style.cursor="grabbing"}),n(iframeContainer)}n(floatingImageContainer),g.addEventListener("click",()=>{document.body.removeChild(floatingImageContainer),iframeContainer.classList.remove("fullscreen"),iframeContainer.appendChild(o),iframeContainer.appendChild(q),document.body.appendChild(iframeContainer)});const r=()=>{const a=document.location.href;return urlsToHideLivestreams.split(",").filter(Boolean).some(b=>a.includes(b))},s=()=>{try{document.body.removeChild(floatingImageContainer)}catch{}try{document.body.removeChild(iframeContainer)}catch{}},t=()=>{if(console.error("showFullSreen",document.location.href),document.location.pathname===URL_FULLSTREAM){const a=document.getElementById("livestream");q.classList.add("livestreams-page"),a.appendChild(q)}else r()?(s(),q.classList.remove("livestreams-page")):(isClickedCloseFloatingImage||document.body.appendChild(floatingImageContainer),q.classList.remove("livestreams-page"))};observeUrlChange(t),isRenderedLivestreams=!0};