import{useRef as e,useMemo as r,useEffect as t}from"react";import{useCurrentDevice as i,getResponsiveValueByScreen as o}from"@gem-sdk/core";import{getAspectRatio as n,getYoutubeAspectRatio as u,setVideoIframeAlign as l,resetVideoIframeStyle as m,resetVideoWrapperStyle as a,setVideoWrapperStyle as f,getResponsiveSetting as s,updateVideoIframeSize as c,getContainerRatio as d}from"../common/helpers.js";let v=({videoType:v,sizeSetting:y,background:b,videoObjectFit:p,heroBannerWrapperRef:E,isYoutubeVideoMigrated:w})=>{let L=i(),q=e(null),A=r(()=>n(y,b),[y,b]),R=r(()=>u(b,p,A),[b,p,A]),S=s(p||{}),g=o(S,L),j=o(b,L),F=j?.loop;return t(()=>{if("youtube"!==v)return;let e=q.current;if(!e)return;let r=e?.querySelector("iframe");if(!r)return;let t=o(b,L),i=t?.videoAlign;l(r,i)},[b,L,v]),t(()=>{let e=q.current,r=E?.current,t=()=>e?.querySelector("iframe"),i=i=>{let o=i||e;if(!o)return;let n=o.querySelector("iframe");c({iframeEl:n||t(),containerRatio:d(r),videoObjectFit:g||"cover",background:b,currentDevice:L,isYoutubeVideoMigrated:w})},o=e=>{let r=e.detail?.iframe;if(r){let e=r.closest("article"),t=e?.parentElement;setTimeout(()=>{i(t)},50)}};if(r&&r.addEventListener("youtube-iframe-ready",o),!e)return()=>{r&&r.removeEventListener("youtube-iframe-ready",o)};if("youtube"!==v){let i=t();return i&&m(i),a(e),()=>{r&&r.removeEventListener("youtube-iframe-ready",o)}}f(e),t()&&(window.requestAnimationFrame(()=>i()),setTimeout(()=>{i()},100));let n=window.ResizeObserver;if(!n)return()=>{r&&r.removeEventListener("youtube-iframe-ready",o)};let u=new n(()=>i());return r&&u.observe(r),()=>{if(u.disconnect(),r&&r.removeEventListener("youtube-iframe-ready",o),e){let r=t();r&&m(r),a(e)}}},[v,g,L,A,F]),{youtubeIframeWrapperRef:q,resolvedAspectRatio:A,youtubeAspectRatio:R}};export{v as default};
