import{template as e,RenderIf as t,isLocalEnv as o,baseAssetURL as i}from"@gem-sdk/core";import r from"../../image/components/NextImage.liquid.js";import{getSettingPreloadData as s}from"../../helpers.js";import{createAttr as a,createClass as d,createStyle as l,If as n}from"@gem-sdk/system";import{getWrapThumbnailClasses as m,getButtonThumbnailClasses as c,getImageThumbnailClasses as u,getVideoYoutubeClasses as p}from"../common/classes.js";import{getPreloadImageStyle as v}from"../common/styles.js";import{getHtml5Attr as y}from"../common/attrs.js";let HTML5Embed=f=>{let g=`gp-video-${f.uid}`,{thumbnail:$,muted:L,autoplay:h,loop:b,controls:E,src:A,style:P,title:w,className:T,lazy:j,poster:k,wrapperStyles:S}=f,V=h&&L&&!!$,x=!j&&f.isVideoComponent,C=!$||h&&L,I=x?A:void 0,M=a({...y({videoId:g,title:w,autoplay:h,preloadThumbnail:V,offLazyLoad:x,src:A,thumbnail:$})}),N=d({...m(C)}),z=l({...v()}),B=d({...c()}),D=d({...u()}),G=d({...p(T,x)}),H=()=>V?e`${r({src:$,alt:w,priority:k?.preload,enableLazyLoadImage:!0,style:z})}
    ${n(h,`<script>
        document.addEventListener("DOMContentLoaded", (event) => {
          const eventList = ["keydown", "mousemove", "wheel", "touchmove", "touchstart", "touchend"];
          const autoLoadTimeout = setTimeout(runAutoPlayVideo, 0);
          eventList.forEach(function(event) {
              window.addEventListener(event, triggerAutoPlay, { passive: true })
          });
          function triggerAutoPlay() {
              runAutoPlayVideo();
              clearTimeout(autoLoadTimeout);
              eventList.forEach(function(event) {
                  window.removeEventListener(event, triggerAutoPlay, { passive: true });
              });
          }
          function runAutoPlayVideo() {
              var video = document.getElementById('${g}');
              if (!video) return;
              video.setAttribute('autoplay','true');
              video.setAttribute('playsinline','true');
              var playPromise = video.play();
              if (playPromise !== undefined) {
                playPromise.catch(function(error) {
                  if (error.name === 'AbortError') {
                    video.addEventListener('loadeddata', function onLoaded() {
                      video.removeEventListener('loadeddata', onLoaded);
                      video.play();
                    });
                  }
                });
              }
          }
        });
      </script>`)}
      `:"",O=()=>e`
     <div
        role="presentation"
        style="${P}"
        class="${N}"
        >
           ${r({id:"video-thumbnail",src:$??"",alt:k?.altText,title:k?.imageTitle,className:D,isDisableAspectStyle:!0,priority:k?.preload,enableLazyLoadImage:!0})}
          <button type="button" class="${B}" aria-label="Play">
            <svg class="gp-w-5 gp-text-white" viewBox="0 0 24 24">
              <path
                fill="currentColor"
                d="M5 5.274c0-1.707 1.826-2.792 3.325-1.977l12.362 6.726c1.566.853 1.566 3.101 0 3.953L8.325 20.702C6.826 21.518 5 20.432 5 18.726V5.274Z"
              />
            </svg>
          </button>
        </div>
  `;return e`
  <gp-lite-html5-embed is-check-loaded="${f.isCheckLoaded??!1}" style="${S}">
    ${H()}
    <video
    ${{...M}}
      style="${P}"
      class="${G}"
      ${E?"controls":void 0}
      ${b?"loop":void 0}
      ${L?"muted":void 0}
      ${h?"autoplay":void 0}
      playsinline
      ${I?`src="${I}"`:""}
    ></video>
    ${n(!C,O())}
  </gp-lite-html5-embed>
        ${t(o,`<script ${s('class="gps-link" delay',"src")}="{{ 'gp-lite-html5-embed-v7-5.js' | asset_url }}" defer="defer"></script>`,`<script ${s('class="gps-link" delay',"src")}="${i}/assets-v2/gp-lite-html5-embed-v7-5.js?v={{ shop.metafields.GEMPAGES.ASSETS_VERSION }}" defer="defer"></script>`)}
  `};export{HTML5Embed as default};
