const THUMBNAIL_CACHE=new Map,THUMBNAIL_REGISTRY=new Map,VIDEO_SERVICES=new Map,DEFAULT_ALLOW="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; fullscreen; web-share",DEFAULT_SANDBOX="allow-scripts allow-same-origin allow-popups allow-forms allow-presentation";async function checkImage(e){if(THUMBNAIL_CACHE.has(e))return THUMBNAIL_CACHE.get(e);const t=new AbortController,n=setTimeout(()=>t.abort(),2e3);try{const o=await fetch(e,{method:"HEAD",signal:t.signal});clearTimeout(n);const s=o.ok;return THUMBNAIL_CACHE.set(e,s),s}catch{return clearTimeout(n),THUMBNAIL_CACHE.set(e,!1),!1}}function parseUrl(e){try{return new URL(e)}catch{return null}}class VideoServiceProvider{constructor(){this.name="generic"}canHandle(){return!1}getVideoId(){return null}getEmbedUrl(){return""}getThumbnailUrls(e,t,n){const s=n.getAttribute("thumbnail");return s?[s]:[]}parseParams(){return{}}getIframeAttributes(e){return{frameborder:e.getAttribute("frameborder")||"0",allow:e.getAttribute("allow")||DEFAULT_ALLOW,sandbox:e.getAttribute("sandbox")||DEFAULT_SANDBOX}}getDefaults(){return{autoload:!1}}}class YouTubeProvider extends VideoServiceProvider{constructor(){super(),this.name="youtube",this.THUMBNAIL_QUALITIES={maxres:"maxresdefault.jpg",sd:"sddefault.jpg",hq:"hqdefault.jpg",mq:"mqdefault.jpg",default:"default.jpg"},this.URL_PATTERNS=[/youtube\.com\/embed\/([a-zA-Z0-9_-]{11})/,/youtube\.com\/watch\?v=([a-zA-Z0-9_-]{11})/,/youtu\.be\/([a-zA-Z0-9_-]{11})/]}canHandle(e){return e&&/youtube\.com|youtu\.be/.test(e)}getVideoId(e){if(!e)return null;const t=parseUrl(e);if(t){if(t.pathname.startsWith("/embed/")||t.hostname==="youtu.be"){const e=t.pathname.split("/");return e[e.length>2?2:1]}const e=t.searchParams.get("v");if(e)return e}for(const n of this.URL_PATTERNS){const t=e.match(n);if(t?.[1])return t[1]}return null}getEmbedUrl(e,t={},n){const o=n.getAttribute("no-cookie")!=="false",i=o?"youtube-nocookie.com":"youtube.com";let s=`https://www.${i}/embed/${e}?autoplay=1`;for(const[e,n]of Object.entries(t))e!=="autoplay"&&e&&n&&(s+=`&${e}=${encodeURIComponent(n)}`);return s}getThumbnailUrls(e,t,n){const i=n.getAttribute("thumbnail");if(i)return[i];const o=`https://img.youtube.com/vi/${e}`,s=[];return t&&this.THUMBNAIL_QUALITIES[t]?s.push(`${o}/${this.THUMBNAIL_QUALITIES[t]}`):window.matchMedia("(max-width: 767px)").matches?s.push(`${o}/${this.THUMBNAIL_QUALITIES.hq}`):s.push(`${o}/${this.THUMBNAIL_QUALITIES.maxres}`),s.includes(`${o}/${this.THUMBNAIL_QUALITIES.hq}`)||s.push(`${o}/${this.THUMBNAIL_QUALITIES.hq}`),s.includes(`${o}/${this.THUMBNAIL_QUALITIES.default}`)||s.push(`${o}/${this.THUMBNAIL_QUALITIES.default}`),s}parseParams(e){const t={},n=parseUrl(e);if(!n)return t;for(const[e,s]of n.searchParams.entries())t[e]=s;return(t.t||t.start)&&(t.start=t.t||t.start),t.list&&(t.playlist=t.list),t}}class BitchuteProvider extends VideoServiceProvider{constructor(){super(),this.name="bitchute",this.URL_PATTERNS=[/bitchute\.com\/video\/([a-zA-Z0-9_-]+)/,/bitchute\.com\/embed\/([a-zA-Z0-9_-]+)/]}canHandle(e){return e&&/bitchute\.com/.test(e)}getVideoId(e){if(!e)return null;const t=parseUrl(e);if(t){const e=t.pathname.split("/").filter(Boolean);for(let t=0;t{const[n,s]=t.split("|");n&&n.removeEventListener&&n.removeEventListener(s,e)}),this._handlers.clear()}_setupObserver(){if(!window.IntersectionObserver)return;this._cleanupObserver(),this._observer=new IntersectionObserver(e=>{e[0].isIntersecting&&!this._loaded&&(this._loadVideo(),this._cleanupObserver())},{rootMargin:"300px",threshold:.1}),this._observer.observe(this)}_updateThumbnail(){const e=this._placeholder?.querySelector('[part="thumbnail"]');if(!e)return;const t=this.getAttribute("thumbnail");if(t){e.src=t;const n=this._placeholder.querySelector('[part="fallback-thumbnail"]');n&&n.remove();return}const n=this._placeholder.dataset.videoId;if(n&&this._videoService){const s=this.getAttribute("thumbnail-quality"),t=this._videoService.getThumbnailUrls(n,s,this);t.length>0?this._loadThumbnail(t,e):this._createFallbackThumbnail()}}_createFallbackThumbnail(){if(!this._placeholder||this._placeholder.querySelector('[part="fallback-thumbnail"]'))return;const e=document.createElement("div");if(e.setAttribute("part","fallback-thumbnail"),this._videoService){const t=this._videoService.name;e.innerHTML=`
${t.charAt(0).toUpperCase()+t.slice(1)}
Click to play video
`}else e.textContent="No thumbnail available";this._placeholder.appendChild(e)}async _createPlaceholder(){const e=this.getAttribute("src");this._videoService=this._getServiceProvider(e);const t=this._videoService?.getVideoId(e);if(!t){this.shadowRoot.innerHTML=`

Error: Can't find video ID. Check the 'src' attribute.

`;return}this._videoParams=this._videoService.parseParams(e);const s=this.getAttribute("title")||"Video",n=document.createElement("style");n.textContent=LazyVideo.styles;const o=this._buildPlaceholder(t,s);this.shadowRoot.innerHTML="",this.shadowRoot.append(n,o),this._updateStyles()}_buildPlaceholder(e,t){const n=document.createElement("div");n.setAttribute("part","placeholder"),n.setAttribute("role","button"),n.setAttribute("aria-label",`Play: ${t}`),n.setAttribute("tabindex","0"),n.dataset.videoId=e,n.dataset.service=this._videoService.name,this._placeholder=n;const c=this.getAttribute("thumbnail-quality"),a=this._videoService.getThumbnailUrls(e,c,this),s=document.createElement("img");if(s.setAttribute("part","thumbnail"),s.alt=`Thumbnail for ${t}`,s.loading="lazy",s.decoding="async",s.fetchPriority="low",s.style.backgroundColor="#111",n.appendChild(s),a.length>0?this._setupThumbnailObserver(s,a):this._createFallbackThumbnail(),!this.hasAttribute("hide-title")){const e=document.createElement("div");e.setAttribute("part","title-bar"),e.textContent=t,n.appendChild(e)}const r=document.createElement("div");r.setAttribute("part","play-button"),n.appendChild(r);const i=parseInt(this._videoParams.start||this._videoParams.t,10);if(!(i!=i)&&i>0){const e=document.createElement("div");e.setAttribute("part","timestamp"),e.textContent=this._formatTime(i),n.appendChild(e)}const o=e=>{(e.type==="click"||e.key==="Enter"||e.key===" ")&&(e.type!=="click"&&e.preventDefault(),this._loadVideo())};return n.addEventListener("click",o),n.addEventListener("keydown",o),this._handlers.set(`${n}|click`,o),this._handlers.set(`${n}|keydown`,o),n}_setupThumbnailObserver(e,t){if(!window.IntersectionObserver){this._loadThumbnail(t,e);return}this._thumbnailLoadAttempted=!1;const n=new IntersectionObserver(async s=>{if(s[0].isIntersecting&&!this._thumbnailLoadAttempted){this._thumbnailLoadAttempted=!0;try{await this._loadThumbnail(t,e)}catch{this._thumbnailLoadAttempted=!1}finally{n.disconnect()}}},{rootMargin:"300px",threshold:.1});n.observe(e)}async _loadThumbnail(e,t){if(e.length===1&&e[0]===this.getAttribute("thumbnail"))return t.src=e[0],!0;const o=this._placeholder?.dataset?.videoId,i=this._placeholder?.dataset?.service,n=o&&i?`${i}:${o}`:null;if(n&&THUMBNAIL_REGISTRY.has(n))try{const e=await THUMBNAIL_REGISTRY.get(n);if(e)return t.src=e,!0}catch{THUMBNAIL_REGISTRY.delete(n)}let s=null;try{const n=await Promise.all(e.map(e=>checkImage(e).then(t=>({url:e,valid:t})).catch(()=>({valid:!1})))),t=n.find(e=>e.valid);t&&(s=t.url)}catch{for(const t of e)try{if(await checkImage(t)){s=t;break}}catch{}}return s?(t.src=s,n&&THUMBNAIL_REGISTRY.set(n,Promise.resolve(s)),!0):(this._createFallbackThumbnail(),n&&THUMBNAIL_REGISTRY.set(n,Promise.resolve(null)),!1)}_formatTime(e){const t=Math.floor(e/3600),n=Math.floor(e%3600/60),s=e%60;return t>0?`${t}:${n.toString().padStart(2,"0")}:${s.toString().padStart(2,"0")}`:`${n}:${s.toString().padStart(2,"0")}`}_updateStyles(){const e=this.getAttribute("width"),t=this.getAttribute("height"),n=e=>e&&/[a-z%$]/.test(e);if(e?this.style.setProperty("width",n(e)?e:`${e}px`):this.style.removeProperty("width"),t?this.style.setProperty("height",n(t)?t:`${t}px`):this.style.removeProperty("height"),e&&t){const n=parseFloat(e),s=parseFloat(t);!(n!=n)&&!(s!=s)&&this.style.setProperty("--lv-aspect-ratio",`${n} / ${s}`)}}_loadVideo(){if(this._loaded||!this._placeholder)return;const t=document.createElement("div");t.setAttribute("part","loading"),t.textContent="Loading...",this.shadowRoot.appendChild(t);const n=this._placeholder.dataset.videoId,o=this.getAttribute("title")||"Video";if(!this._videoService){const e=this._placeholder.dataset.service;this._videoService=VIDEO_SERVICES.get(e)||VIDEO_SERVICES.get("youtube")}const i=this._videoService.getEmbedUrl(n,this._videoParams,this),e=document.createElement("iframe");e.setAttribute("part","iframe"),e.loading="lazy",e.src=i,e.title=o,e.setAttribute("credentialless","");const a=this._videoService.getIframeAttributes(this);for(const[t,n]of Object.entries(a))e.setAttribute(t,n);const s=()=>t.parentNode?.removeChild(t);e.addEventListener("load",s,{once:!0}),this._handlers.set(`${e}|load`,s),this._placeholder.replaceWith(e),this._loaded=!0,this._placeholder=null,this.dispatchEvent(new CustomEvent("video-loaded",{bubbles:!0,detail:{videoId:n,service:this._videoService.name}}))}_setupStyleObserver(){if(this._styleObserver)return;if(this._styleObserver=new MutationObserver(()=>{this._updateAlignmentFromCSS()}),this._styleObserver.observe(this,{attributes:!0,attributeFilter:["style"]}),window.getComputedStyle){let e;const t=()=>{e=requestAnimationFrame(()=>{this._updateAlignmentFromCSS(),e=requestAnimationFrame(t)})};t(),this._styleFrameId=e}}_updateAlignmentFromCSS(){if(this.hasAttribute("container-fit"))return;const t=window.getComputedStyle(this),e=t.getPropertyValue("--lv-align").trim();this.classList.remove("lv-align-left","lv-align-right","lv-align-center"),e==="left"?this.classList.add("lv-align-left"):e==="right"?this.classList.add("lv-align-right"):e==="center"&&this.classList.add("lv-align-center")}}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>customElements.define("lazy-video",LazyVideo)):customElements.define("lazy-video",LazyVideo)