import{c as V,r as n,u as N,bC as R,a as P,bS as U,j as m,l as M}from"./index-DCNLpqIv.js";const w=V("iframe",{target:"evvcert0"})(({theme:r})=>({colorScheme:"normal",border:"none",padding:r.spacing.none,margin:r.spacing.none,width:"100%",aspectRatio:"16 / 9"}),""),j=M.getLogger("Video"),x={width:"100%"};function O({element:r,endpoints:i,elementMgr:p}){const o=n.useRef(null),{type:h,url:v,startTime:a,subtitles:y,endTime:s,loop:d,autoplay:f,muted:g}=r,u=N(v),c=R(y);let S=P(u);const T=n.useMemo(()=>{if(!r.id)return!0;const e=p.getElementState(r.id,"preventAutoplay");return e||p.setElementState(r.id,"preventAutoplay",!0),e??!1},[r.id,p]),E=n.useMemo(()=>JSON.stringify(c?c.map(e=>i.buildMediaURL(`${e.url}`)):[]),[c,i]);n.useEffect(()=>{const e=JSON.parse(E);e.length!==0&&e.forEach(t=>{i.checkSourceUrlResponse(t,"Video Subtitle")})},[E,i]),n.useEffect(()=>{o.current&&(o.current.currentTime=a)},[a]),n.useEffect(()=>{const e=o.current,t=()=>{e&&(e.currentTime=r.startTime)};return e&&e.addEventListener("loadedmetadata",t),()=>{e&&e.removeEventListener("loadedmetadata",t)}},[r]),n.useEffect(()=>{const e=o.current;if(!e)return;let t=!1;const l=()=>{s>0&&e.currentTime>=s&&(d?(e.currentTime=a||0,e.play()):t||(t=!0,e.pause()))};return s>0&&e.addEventListener("timeupdate",l),()=>{e&&s>0&&e.removeEventListener("timeupdate",l)}},[s,d,a]),n.useEffect(()=>{const e=o.current;if(!e)return;const t=()=>{d&&(e.currentTime=a||0,e.play())};return e.addEventListener("ended",t),()=>{e&&e.removeEventListener("ended",t)}},[d,a]);const b=e=>{const t=new URL(e);if(a&&!isNaN(a)&&t.searchParams.append("start",a.toString()),s&&!isNaN(s)&&t.searchParams.append("end",s.toString()),d){t.searchParams.append("loop","1");const l=t.pathname.split("/").pop();l&&t.searchParams.append("playlist",l)}return f&&t.searchParams.append("autoplay","1"),g&&t.searchParams.append("mute","1"),t.toString()};if(h===U.Type.YOUTUBE_IFRAME)return m.jsx(w,{className:"stVideo","data-testid":"stVideo",title:u,src:b(u),allow:"autoplay; encrypted-media",allowFullScreen:!0});const L=e=>{const t=e.currentTarget.src;j.error(`Client Error: Video source error - ${t}`),i.sendClientErrorToHost("Video","Video source failed to load","onerror triggered",t)};return m.jsx("video",{className:"stVideo","data-testid":"stVideo",ref:o,controls:!0,muted:g,autoPlay:f&&!T,src:i.buildMediaURL(u),style:x,crossOrigin:S,onError:L,children:c&&c.map((e,t)=>m.jsx("track",{kind:"captions",src:i.buildMediaURL(`${e.url}`),label:`${e.label}`,default:t===0,"data-testid":"stVideoSubtitle"},t))})}const $=n.memo(O);export{$ as default};
