"use strict";var core=require("@gem-sdk/core"),helpers=require("../../helpers.js"),translate=require("../settings/configs/translate.js"),_const=require("./const.js");const overwriteDefaultRadius=e=>{let t={...e},r=!0;return t&&Object.keys(t??{}).forEach(e=>{if("bblr"===e||"bbrr"===e||"btlr"===e||"btrr"===e){let i=t[e]??{};"0px"!==i&&(r=!1)}}),r?core.getCornerCSSFromGlobal(t):{}},getAspectRatio=({styles:e,setting:t})=>{let{srcSet:r,image:i}=t??{},{customAspectRadio:a,shape:s}=e??{},o={desktop:composeAspectRatio({device:"desktop",aspect:e?.aspectRatio,auto:t?.isAdaptive?r?.desktop:i,custom:a}),tablet:composeAspectRatio({device:"tablet",aspect:e?.aspectRatio,auto:t?.isAdaptive?r?.tablet:i,custom:a}),mobile:composeAspectRatio({device:"mobile",aspect:e?.aspectRatio,auto:t?.isAdaptive?r?.mobile:i,custom:a})};return s?core.getAspectRatioGlobalSize(s):o},composeAspectRatio=({device:e,aspect:t,custom:r,auto:i})=>{let a=t?.[e];return"auto"===a?i?.height?`${i.width}/${i.height}`:void 0:"custom"===a?r?.[e]?.height?`${r?.[e]?.width}/${r?.[e]?.height}`:void 0:a},getResponsiveObjectFit=({layout:e,isDisableObjectFit:t})=>t?{}:core.makeStyleResponsive("objf",e),getAspectRatioNextImage=({isDisableAspectStyle:e,aspectRatio:t,objectFits:r,width:i,height:a})=>{if(e)return{};let s={};return core.DEVICES.forEach(e=>{let o=`--objf${"desktop"!==e?`-${e}`:""}`,l=r?.[o]||r?.["--objf"];if(t?.[e]==="auto"&&"contain"===l){s[e]="auto";return}s[e]=t?.[e]!=="auto"&&t?.[e]?t[e]:`${i}/${a}`}),s},safeStringify=e=>JSON.stringify(e||{}).replace(/"/g,"&quot;"),shimmer=(e,t)=>`<svg width="${e}" height="${t}" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
      <linearGradient id="g-${e}-${t}">
        <stop stop-color="rgba(51, 51, 51, 0)" offset="20%" />
        <stop stop-color="rgba(51, 51, 51, 0)" offset="50%" />
        <stop stop-color="rgba(51, 51, 51, 0)" offset="70%" />
      </linearGradient>
    </defs>
    <rect width="${e}" height="${t}" fill="rgba(51, 51, 51, 0)" />
    <rect id="r" width="${e}" height="${t}" fill="url(#g-${e}-${t})" />
    <animate xlink:href="#r" attributeName="x" from="-${e}" to="${e}" dur="1s" repeatCount="indefinite"  />
  </svg>`,toBase64=e=>"undefined"==typeof window?Buffer.from(e).toString("base64"):window.btoa(e),createBlurDataURL=(e,t)=>`data:image/svg+xml;base64,${toBase64(shimmer(e,t))}`,IMAGE_PLACEHOLDER="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",FALLBACK_IMAGE="https://cdn.shopify.com/s/assets/no-image-2048-5e88c1b20e087fb7bbe9a3771824e743c244f437e4f8ba93bbf7b11b53f7824c_large.gif",NEXT_IMAGE_CONFIG={DEFAULT_SIZES:"100vw",DEFAULT_ALT:"Alt image",DEFAULT_SRC:"https://cdn.shopify.com/s/files/1/2005/9307/files/placeholder-1.png?v=1751355990",DEFAULT_WIDTH_PROPS:800,DEFAULT_HEIGHT_PROPS:533},getAdaptiveImageData=e=>{let{srcSet:t,fallbackImg:r}=e,i=e=>{let{isLiquid:i}=e??{},a=getImageSrc(t?.desktop,"desktop",!i)||r||FALLBACK_IMAGE,s=getImageSrc(t?.tablet,"tablet",!i)||getImageSrc(t?.desktop,"tablet",!i)||r||FALLBACK_IMAGE,o=getImageSrc(t?.mobile,"mobile",!i)||getImageSrc(t?.tablet,"mobile",!i)||getImageSrc(t?.desktop,"mobile",!i)||r||FALLBACK_IMAGE;return{srcTablet:s,srcMobile:o,srcDesktop:a}};return{getDeviceSource:i}},getSettingTranslatedImageAndSeo=({setting:e,uid:t,pageContext:r})=>{if(!e||!translate.Image)return{};let i=core.cloneDeep(e);for(let e in i.srcSet){let a=i.srcSet[e];if(a&&a.src){let i=helpers.getDynamicSourceLocales({val:a.src,uid:t,settingId:translate.Image?.srcSet?.id?`${translate.Image.srcSet.id}_${e}_src`:"",pageContext:r??{}});a.src=String(i)}}let a=helpers.getDynamicSourceLocales({val:i.alt,uid:t,settingId:helpers.createSettingId({id:translate.Image?.alt?.id}),pageContext:r??{}});i.alt=String(a);let s=helpers.getDynamicSourceLocales({val:i.title,uid:t,settingId:helpers.createSettingId({id:translate.Image?.title?.id}),pageContext:r??{}});return i.title=String(s),i},getSrcSet=(e,t,r)=>e?getSrcSetLiquid(e,t):getSrcSetLink(r),isCannotResizeImage=e=>_const.REGEX_IS_NON_RESIZABLE_IMAGE.test(e),getSrcSetLiquid=(e,t)=>{let r=replaceBackupFileKeyFileContent(e);return isCannotResizeImage(e)?"FILE_CONTENT"==t?`{{ '${r}' | file_url }} 768w, {{ '${r}' | file_url }} 1024w, {{ '${r}' | file_url }} 1440w`:`{{ '${e}' | asset_url }} 768w, {{ '${e}' | asset_url }} 1024w, {{ '${e}' | asset_url }} 1440w`:"FILE_CONTENT"==t?`{{ '${r}' | file_img_url: '768x' }} 768w, {{ '${r}' | file_img_url: '1024x' }} 1024w, {{ '${r}' | file_img_url: 'master' }} 1440w`:`{{ '${e}' | asset_img_url: '768x' }} 768w, {{ '${e}' | asset_img_url: '1024x' }} 1024w, {{ '${e}' | asset_img_url: 'master' }} 1440w`},getSrcSetLink=e=>e,getImageSrc=(e,t,r)=>{if(e?.metafieldDefinitionKey)return getImageMetafieldDefinitionKey(e?.metafieldDefinitionKey);let i=e?.backupFilePath??e?.src??"";if(r)i=e?.src||"";else{let t=e?.src??"";if(core.hasTranslatedLiquid(t))return t;if(e?.backupFileKey){let t=getNewBackupFileKey(e.backupFileKey,e?.src??"");"THEME"!==e.storage&&e.storage?"FILE_CONTENT"===e.storage&&(i=`{{ "${replaceBackupFileKeyFileContent(t)}" | file_url }}`):i=`{{ "${t}" | asset_url }}`}}if(t&&i?.includes("cdn.shopify")&&!isCannotResizeImage(i))switch(t){case"tablet":i=i?.includes("?")?`${i}&width=1024`:`${i}?width=1024`;break;case"mobile":i=i?.includes("?")?`${i}&width=768`:`${i}?width=768`}return i},getImageMetafieldDefinitionKey=e=>`{{${e} | file_url}}`,replaceBackupFileKeyFileContent=e=>e.replace(/\.jpeg|\.jfif/g,".jpg"),getNewBackupFileKey=(e,t)=>{let r=t?.match(/\/files\/([^\/]+\.(jpg|jpeg|gif|png|webp|svg))/)?.[1];return r&&r!=e?r:e};function resizeImage(e,t){let r=replaceRegexLastMatch({str:e,reg:/_(pico|icon|thumb|small|compact|medium|large|grande|original|1024x1024|2048x2048|master)+\./g,newString:"."});if(isCannotResizeImage(r))return r;let i=replaceRegexLastMatch({str:r,reg:/\.jpg|\.png|\.jpeg/g,newString:t,isAdd:!0});return i}const replaceRegexLastMatch=e=>{let t=e.str,r=e.str.match(e.reg)?.length;if(r){let i,a=1;for(;null!==(i=e.reg.exec(e.str));){if(a===r){let r=e.reg.lastIndex,a=r-i[0].length;e.isAdd&&(e.newString="_"+e.newString+i),t=t.slice(0,a)+e.newString+t.slice(r,t.length)}a++}}return t},getItemWidth=(e,t)=>{let r=getCurrentDevice(),i=t?.[r]||"auto";if(!i||"auto"===i||i.includes("%"))return e;let a=Number(i.replace("px",""));return a>e?e:Number(i.replace("px",""))},getImageWidthFollowByQuality=e=>{let{itemWidthParam:t,imageWidthSetting:r,qualityType:i,qualityPercent:a}=e,s=getCurrentDevice(),o=getItemWidth(t,r),l=core.getResponsiveValueByScreen(i,s,"finest"),g=core.getResponsiveValueByScreen(a,s,100);switch(l){case"finest":default:return"auto";case"high":return Math.round(1.5*o)||1;case"medium":return Math.round(o)||1;case"custom":return Math.round(o*(Number(g)/100))||1}},getWidthOfFeatureImageSetting=e=>{let t={desktop:"100%",tablet:"100%",mobile:"100%"};return core.DEVICES.forEach(r=>{let i=core.getResponsiveValueByScreen(e,r)?.width||"100%";t[r]=i}),t},getWidthOfImageListSetting=e=>{let t={desktop:"100%",tablet:"100%",mobile:"100%"};return core.DEVICES.forEach(r=>{let i=core.getResponsiveValueByScreen(e,r)?.width||"100%";t[r]=i}),t},getWidthOfListImageInDom=e=>{let t=document.querySelectorAll(`.gem-slider-item-${e}.gp-gallery-image-item`),r=0;return t.forEach(e=>{e.offsetWidth>r&&(r=e.offsetWidth)}),r},getWidthOfFeatureImageInDom=e=>{let t=document.querySelectorAll(`.gem-slider-item-${e}.gp-ft-image-item`),r=0;return t.forEach(e=>{e.offsetWidth>r&&(r=e.offsetWidth)}),r},handleSetSrcForProductImageList=e=>{let{shapeByLayout:t,qualityType:r,qualityPercent:i,builderPropUID:a}=e,s=document.querySelectorAll(`.gem-slider-item-${a}.gp-gallery-image-item`),o=getWidthOfImageListSetting(t),l=getWidthOfListImageInDom(a);s.forEach(e=>{handleSetSrcForGalleryImage({item:e,settingWidth:o,width:l,qualityType:r,qualityPercent:i})})},handleSetSrcForFeatureProductImage=e=>{let{ftShape:t,qualityType:r,qualityPercent:i,builderPropUID:a}=e,s=document.querySelectorAll(`.gem-slider-item-${a}.gp-ft-image-item`),o=getWidthOfFeatureImageSetting(t),l=getWidthOfFeatureImageInDom(a);s.forEach(e=>{handleSetSrcForGalleryImage({item:e,settingWidth:o,width:l,qualityType:r,qualityPercent:i})})},handleSetSrcForGalleryImage=e=>{let{item:t,settingWidth:r,width:i,qualityType:a,qualityPercent:s}=e,o=getCurrentDevice(),l=t.querySelector("img");if(l){let e=getImageWidthFollowByQuality({itemWidthParam:i,imageWidthSetting:r,qualityType:a,qualityPercent:s}),t=l.getAttribute("base-src");if(!t)return;let g=getImageSrcByQuality(e,t),n=getImageSrcSet(t,g,o);l.setAttribute("srcset",n)}},getImageSrcByQuality=(e,t)=>"auto"===e?t:resizeImage(t,`${e}x`),getImageSrcSet=(e,t,r)=>`${"mobile"===r&&e!==t?t:resizeImage(e,"768x")} 767w, ${"tablet"===r&&e!==t?t:resizeImage(e,"1024x")} 1023w, ${"desktop"===r&&e!==t?t:resizeImage(e,"1920x")} 1440w`,getCurrentDevice=()=>window.innerWidth<768?"mobile":window.innerWidth>=768&&window.innerWidth<1025?"tablet":"desktop";exports.FALLBACK_IMAGE=FALLBACK_IMAGE,exports.IMAGE_PLACEHOLDER="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",exports.NEXT_IMAGE_CONFIG=NEXT_IMAGE_CONFIG,exports.composeAspectRatio=composeAspectRatio,exports.createBlurDataURL=createBlurDataURL,exports.getAdaptiveImageData=getAdaptiveImageData,exports.getAspectRatio=getAspectRatio,exports.getAspectRatioNextImage=getAspectRatioNextImage,exports.getCurrentDevice=getCurrentDevice,exports.getImageSrc=getImageSrc,exports.getImageWidthFollowByQuality=getImageWidthFollowByQuality,exports.getNewBackupFileKey=getNewBackupFileKey,exports.getResponsiveObjectFit=getResponsiveObjectFit,exports.getSettingTranslatedImageAndSeo=getSettingTranslatedImageAndSeo,exports.getSrcSet=getSrcSet,exports.handleSetSrcForFeatureProductImage=handleSetSrcForFeatureProductImage,exports.handleSetSrcForProductImageList=handleSetSrcForProductImageList,exports.isCannotResizeImage=isCannotResizeImage,exports.overwriteDefaultRadius=overwriteDefaultRadius,exports.replaceBackupFileKeyFileContent=replaceBackupFileKeyFileContent,exports.resizeImage=resizeImage,exports.safeStringify=safeStringify,exports.shimmer=shimmer,exports.toBase64=toBase64;
