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