var at=Object.defineProperty,ot=Object.defineProperties;var ct=Object.getOwnPropertyDescriptors;var $=Object.getOwnPropertySymbols;var lt=Object.prototype.hasOwnProperty,dt=Object.prototype.propertyIsEnumerable;var N=(n,e,t)=>e in n?at(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,T=(n,e)=>{for(var t in e||(e={}))lt.call(e,t)&&N(n,t,e[t]);if($)for(var t of $(e))dt.call(e,t)&&N(n,t,e[t]);return n},L=(n,e)=>ot(n,ct(e));var p=(n,e,t)=>N(n,typeof e!="symbol"?e+"":e,t);var h=(n,e,t)=>new Promise((s,r)=>{var i=c=>{try{o(t.next(c))}catch(d){r(d)}},a=c=>{try{o(t.throw(c))}catch(d){r(d)}},o=c=>c.done?s(c.value):Promise.resolve(c.value).then(i,a);o((t=t.apply(n,e)).next())});import{i as Q,g as X,a as J,l as S,b as I,_,W as Z,c as W,d as ut}from"./638a3ec7e6/widget-loader.dl3yrYf6.js";import{s as tt,g as pt,a as ht,b as gt,c as St,d as _t,e as yt,f as ft,O as A,h as H,E as j}from"./638a3ec7e6/error.utils.WR7nOLYh.js";import{C as Et,g as mt}from"./638a3ec7e6/chat-bubble-widget-settings.BwfzFx--.js";import{g as It}from"./638a3ec7e6/page.util.DrgUDMuu.js";import{g as et}from"./638a3ec7e6/font.utils.1AcYFt-O.js";import{z as f}from"./638a3ec7e6/index.DF2-LJG7.js";const At={PRODUCT_TILE:"tolstoy-product-tile"},ne="./styles/product-tile.css",ie="w-full",wt="tolstoy-collection-page-tile",re="./styles/collection-page-tile.css",ae={display:"flex",width:"100%",height:"100%",position:"relative"},Tt=(n,e)=>h(void 0,null,function*(){var i,a;const s=tt()&&Q()?X():J(),r=new URLSearchParams;r.set("productId",n),r.set("appKey",e);try{const o=yield fetch(`${s}/settings/widget/get-product-gallery-config?${r}`);S({windowShopify:(i=window.Shopify)==null?void 0:i.shop});const c=[...document.querySelectorAll("script[data-shop]")].map(y=>{var E;return(E=y==null?void 0:y.dataset)==null?void 0:E.shop}).find(Boolean);S({appDataShop:c});const d=c||((a=window.Shopify)==null?void 0:a.shop),u=yield o.json();return u.projects&&(u.projects=u.projects.filter(y=>y.appUrl===d)),u}catch(o){return I(o,"Error getting collection page tile config"),null}}),bt=()=>{var e;const n=window.Shopify;return(e=n==null?void 0:n.theme)==null?void 0:e.theme_store_id};function Ct(n){function e(){r&&(t.length>0&&/^[~+>]$/.test(t[t.length-1])&&t.push(" "),t.push(r))}var t=[],s,r,i,a=[0],o=0,c,d=/(?:[^\\]|(?:^|[^\\])(?:\\\\)+)$/,u=/^\s+$/,y=[/\s+|\/\*|["'>~+[(]/g,/\s+|\/\*|["'[\]()]/g,/\s+|\/\*|["'[\]()]/g,null,/\*\//g];for(n=n.trim();;)if(r="",i=y[a[a.length-1]],i.lastIndex=o,s=i.exec(n),s)if(c=o,o=i.lastIndex,c0)a.pop();else if(/^(?:\s+|[~+>])$/.test(s[0])&&(t.length>0&&!u.test(t[t.length-1])&&a[a.length-1]===0&&t.push(" "),a[a.length-1]===1&&t.length===5&&t[2].charAt(t[2].length-1)==="="&&(t[4]=" "+t[4]),u.test(s[0])))continue;t.push(s[0])}else t[t.length-1]+=r,d.test(t[t.length-1])&&(a[a.length-1]===4&&(t.length<2||u.test(t[t.length-2])?t.pop():t[t.length-1]=" ",s[0]=""),a.pop()),t[t.length-1]+=s[0];else{r=n.substr(o),e();break}return t.join("").trim()}function st(n,e=document,t=null){return Pt(n,!0,e,t)}function Pt(n,e,t,s=null){return n=Ct(n),t.querySelector(n),document.head.createShadowRoot||document.head.attachShadow?G(n,",").reduce((i,a)=>{const o=G(a.replace(/^\s+/g,"").replace(/\s*([>+~]+)\s*/g,"$1")," ").filter(E=>!!E).map(E=>G(E,">")),c=o.length-1,d=o[c][o[c].length-1],u=vt(d,t,s),y=Ot(o,c,t);return i=i.concat(u.filter(y)),i},[]):t.querySelectorAll(n)}function Ot(n,e,t){return s=>{let r=e,i=s,a=!1;for(;i&&!Lt(i);){let o=!0;if(n[r].length===1)o=i.matches(n[r]);else{const c=[].concat(n[r]).reverse();let d=i;for(const u of c){if(!d||!d.matches(u)){o=!1;break}d=Y(d,t)}}if(o&&r===0){a=!0;break}o&&r--,i=Y(i,t)}return a}}function G(n,e){return n.match(/\\?.|^$/g).reduce((t,s)=>(s==='"'&&!t.sQuote?(t.quote^=1,t.a[t.a.length-1]+=s):s==="'"&&!t.quote?(t.sQuote^=1,t.a[t.a.length-1]+=s):!t.quote&&!t.sQuote&&s===e?t.a.push(""):t.a[t.a.length-1]+=s,t),{a:[""]}).a}function Lt(n){return n.nodeType===Node.DOCUMENT_FRAGMENT_NODE||n.nodeType===Node.DOCUMENT_NODE}function Y(n,e){const t=n.parentNode;return t&&t.host&&t.nodeType===11?t.host:t===e?null:t}function vt(n=null,e,t=null){let s=[];if(t)s=t;else{const r=function(i){for(let a=0;ar.matches(n)):s}const Ut="#tolstoy-video-preview",Rt="thumbnail.0000000000",Dt="_ai-preview_",Bt=".product__media, .product-single__thumbnails, .gallery, .product-gallery, .slider, .carousel, section.product",Wt=n=>[...Bt.split(",").map(e=>e.trim()).filter(Boolean),"body"].map(e=>`${e} :has(${n})`).join(","),Nt=n=>{var e,t,s;return((e=n==null?void 0:n.alt)==null?void 0:e.includes(Ut))||((t=n==null?void 0:n.src)==null?void 0:t.includes(Rt))||((s=n==null?void 0:n.src)==null?void 0:s.includes(Dt))},oe=(n,e,t)=>{const s=e.split(",").flatMap(i=>[i,`${i} img`]).join(",");return[...t?st(s):document.querySelectorAll(s)].some(i=>i.contains(n))};function ce(n,e,t){const s=t||Wt(n),r=document.querySelector(s);return r?Array.from(e?st(n,r):r.querySelectorAll(n)):[]}const Gt=()=>{var s,r,i;const n=(s=document.querySelector("script[data-product-id]"))==null?void 0:s.getAttribute("data-product-id"),e=(r=document.querySelector("script[data-app-key]"))==null?void 0:r.getAttribute("data-app-key"),t=(i=document.querySelector("script[data-product-gallery-projects]"))==null?void 0:i.getAttribute("data-product-gallery-projects");return{productId:n,appKey:e,productGalleryProjects:t}},z=(n,e)=>h(void 0,null,function*(){if(n){const{initThumbnailAnchorImageDetection:s}=yield _(()=>h(void 0,null,function*(){const{initThumbnailAnchorImageDetection:r}=yield import("./638a3ec7e6/thumbnail-anchor-image-detection.utils.BJH0_loZ.js");return{initThumbnailAnchorImageDetection:r}}),[]);return s(e)}const{initFallbackImageDetection:t}=yield _(()=>h(void 0,null,function*(){const{initFallbackImageDetection:s}=yield import("./638a3ec7e6/fallback-image-detection.utils.16FXCy6G.js");return{initFallbackImageDetection:s}}),[]);return t(e)}),Vt=()=>h(void 0,null,function*(){var n,e,t,s,r;try{let i;S("init product gallery");const{productId:a,appKey:o,productGalleryProjects:c}=Gt();if(!a||!o)return;try{const g=JSON.parse(c);if(S({productGalleryProjects:g}),!(g!=null&&g.length))return}catch(g){I({error:g.message});return}if(i=yield Tt(a,o),!((n=i==null?void 0:i.product)!=null&&n.images)||!((e=i==null?void 0:i.projects)!=null&&e.length)||!((t=i==null?void 0:i.vodAssets)!=null&&t.length)&&!Object.keys((s=i==null?void 0:i.vodAssets)!=null?s:{}).length)return;const d=i.product.images.map(g=>g.src),u=bt();if(S("themeId",u),u){const{themeContainerClasses:g}=yield _(()=>h(void 0,null,function*(){const{themeContainerClasses:C}=yield import("./638a3ec7e6/theme-classes-extractor.BVNtrPL4.js");return{themeContainerClasses:C}}),[]),w=g[u];if(w){const{variantSelector:C,mainGallerySelector:P,thumbnailSelector:O}=w;S("config from theme",{themeId:u,variantSelector:C,mainGallerySelector:P,thumbnailSelector:O});const m=((r=i.projects[0])==null?void 0:r.playerSettings)||{};if(m.useNativePlayer)return;m.galleryImageSelector=m.galleryImageSelector||P,m.galleryThumbnailSelector=m.galleryThumbnailSelector||O}}const y=new URLSearchParams(location.search).get("test-gallery");if(y){const{renderTestGalleryIfNeeded:g}=yield _(()=>h(void 0,null,function*(){const{renderTestGalleryIfNeeded:w}=yield import("./638a3ec7e6/render-test-gallery.DKSc0oMM.js");return{renderTestGalleryIfNeeded:w}}),[]);g(y,d)}const E=i.product.images.some(Nt);S({hasThumbnailAnchorImage:E});let B=()=>{};const F=!Array.isArray(i==null?void 0:i.vodAssets)&&Object.keys(i.vodAssets).length>0;if(S({hasConfigVariants:F,vodAssets:i.vodAssets}),F){const g=()=>h(void 0,null,function*(){try{const P=new URLSearchParams(location.search).get("variant"),O=document.querySelector('form[action="/cart/add"] [name="id"]'),m=parseInt(P||(O==null?void 0:O.value)||"NaN",10);if(S({message:"Current variant:",value:m}),B(),i!=null&&i.vodAssets[m]){const q=L(T({},i),{variant:m,vodAssets:i==null?void 0:i.vodAssets[m],appKey:o});S({clientConfig:q}),B=yield z(E,q)}else B=()=>{}}catch(P){I({error:P})}});let w=window.location.search;const C=new MutationObserver(()=>{window.location.search!==w&&(w=window.location.search,g())});return C.observe(document,{subtree:!0,childList:!0}),g(),()=>{C.disconnect(),B()}}else return yield z(E,L(T({},i),{appKey:o}))}catch(i){I(i,"Error initializing product gallery video")}}),nt="shop-assistant-container",it={flexGrow:1,width:"100%"},Kt="https://rubkik.gotolstoy.com",v={appKey:"app_key",appUrl:"app_url",productId:"product_id",collectionId:"collection_id",aiShopperWidget:"ai_shopper_widget"},Mt=["on-you","top-questions","search-bar"],rt={isPreviewSite:()=>typeof window!="undefined"&&window.location.href.includes(Kt),getSettings:()=>{const n=new URLSearchParams(typeof window!="undefined"?window.location.search:"");return!rt.isPreviewSite()||!Mt.includes(n.get(v.aiShopperWidget))?{}:{appKey:n.get(v.appKey),appUrl:n.get(v.appUrl),productId:n.get(v.productId),collectionId:n.get(v.collectionId),aiShopperWidget:n.get(v.aiShopperWidget)}}},D=class D{constructor(){p(this,"initialized",!1);p(this,"initializedPromise",null);p(this,"accountSettings",null);p(this,"appKey");p(this,"appUrl");p(this,"productId");p(this,"collectionId");p(this,"shouldUseCache");p(this,"cacheVersion");p(this,"aiShopperWidget");if(D.instance)return D.instance;D.instance=this;const e=rt.getSettings();this.appKey=e.appKey||pt()||"",this.appUrl=e.appUrl||ht(),this.productId=e.productId||gt(),this.collectionId=e.collectionId||St(),this.aiShopperWidget=e.aiShopperWidget,this.shouldUseCache=tt(),this.cacheVersion=_t()}loadAccountSettingsFromV1Widget(){return h(this,null,function*(){try{let e=0;for(;e<5;){if(window.widget_V1_AccountSettingsPromise)return yield window.widget_V1_AccountSettingsPromise;e++,yield new Promise(t=>setTimeout(t,200))}return null}catch(e){return I("[TolstoySettings:loadAccountSettingsFromV1Widget] error loading account settings:",e),null}})}initAccountSettings(){return h(this,null,function*(){var e,t;if(!this.accountSettings){if(this.accountSettings=yield this.loadAccountSettingsFromV1Widget(),!this.accountSettings)try{const s=this.shouldUseCache&&Q()?X():J(),r=new URL(`${s}/actions/accounts/${this.appKey}`);this.cacheVersion&&r.searchParams.set("v",this.cacheVersion),this.appUrl&&r.searchParams.set("appUrl",this.appUrl);const i=yield fetch(r.toString());this.accountSettings=yield i.json()}catch(s){I("[TolstoySettings:initAccountSettings] error loading account settings:",s);return}this.shouldUseCache=this.shouldUseCache||((e=this.accountSettings)==null?void 0:e.shouldUseCache)||!1,this.cacheVersion=this.cacheVersion||((t=this.accountSettings)==null?void 0:t.cacheVersion)||null}})}get shopAssistantEnabled(){if(!this.initialized||!this.accountSettings)return!1;const e=yt();return S("[TolstoySettings:shopAssistantEnabled] shopify metadata",e),typeof e=="boolean"?e:(S("[TolstoySettings:shopAssistantEnabled] account settings",this.accountSettings.hasLiveShopAssistant),this.accountSettings.hasLiveShopAssistant)}get searchBarWidgetEnabled(){if(!this.initialized||!this.accountSettings)return!1;if(this.aiShopperWidget)return this.aiShopperWidget==="search-bar";const e=ft();return S("[TolstoySettings:searchBarWidgetEnabled] shopify metadata",e),typeof e=="boolean"?e:!1}init(){return h(this,null,function*(){if(!this.initialized){if(this.initializedPromise){yield this.initializedPromise;return}if(!this.appKey){I("[TolstoySettings:init] appKey is missing",{appKey:this.appKey,appUrl:this.appUrl});return}this.initializedPromise=this.initAccountSettings(),yield this.initializedPromise,this.initialized=!0}})}};p(D,"instance",null);let M=D;const l=new M,V="assistant-agent-root",kt=()=>h(void 0,null,function*(){var n,e;try{if(yield l.init(),!l.shopAssistantEnabled){A.log("AssistantAgentController: Shop assistant is disabled");return}if(!l.appUrl){A.log("AssistantAgentController: Invalid app url",l.appUrl);return}const t=yield Et.getShopAssistantConfig({appUrl:l.appUrl});if(!(t!=null&&t.widgetSettings)){A.log("Invalid shop assistant config",t);return}if(!t.widgetSettings.enabled){A.log("Shop assistant is disabled in config");return}const{displayOnPages:s}=t.widgetSettings;S("displayOnPages",s);const r=It({productId:l.productId,collectionId:l.collectionId});if(!(s!=null&&s.includes(r))){A.log(`Shop assistant not configured to display on ${r} pages`);return}let i=document.getElementById(V);i||(i=document.createElement("div"),i.id=V,document.body.appendChild(i));const[{default:a},{default:o},{default:c}]=yield Promise.all([_(()=>import("./638a3ec7e6/shop-assistant-app.DU2JmKs3.js"),[]),_(()=>import("./638a3ec7e6/render.utils.kaXty2Kk.js").then(u=>u.a),[]),_(()=>import("./638a3ec7e6/shop-assistant.DgNkEc_V.js"),[])]),d={appKey:l.appKey,appUrl:l.appUrl,productId:l.productId||void 0,agentImage:t.agentSettings.agentImage,brandLogo:((n=t==null?void 0:t.brandSettings)==null?void 0:n.brandLogo)||"",hidePoweredBy:t==null?void 0:t.hidePoweredBy,floatingWidgetSettings:(e=t==null?void 0:t.widgetSettings)==null?void 0:e.floatingWidgetSettings,preloadedStyles:c};yield o(a,L(T({},d),{element:i,hasStyledComponents:!0,widgetKey:Z.SHOP_ASSISTANT_BUBBLE,containerClasses:nt,containerStyles:it,fontFamily:et(document.body),preloadedStyles:c})),i.setAttribute("data-status",W.SUCCESS)}catch(t){console.error("Error initializing Shop Assistant:",t),I("Error initializing Shop Assistant",t);const s=document.getElementById(V);s&&s.setAttribute("data-status",W.FAILED)}}),Ft=f.object({agentSettings:f.object({agentImage:f.string()}),brandSettings:f.object({brandLogo:f.string()}),hidePoweredBy:f.boolean(),skills:f.array(f.string())}),qt=f.object({placeholder:f.string()});f.object({settings:qt,shoppingAssistantConfig:Ft});class $t{constructor(){p(this,"getSearchBarConfig",e=>h(this,null,function*(){const t=new URLSearchParams(T({appUrl:e.appUrl,appKey:e.appKey},e.productId&&{productId:e.productId})),s=yield fetch(`${mt()}/on-you/search-bar/getSearchBarSettings?${t.toString()}`);if(!s.ok)throw new H(j.GETTING_SEARCH_BAR_CONFIG_FAILED,{details:"Failed to get search bar config",params:e,response:s});const r=yield s.json();if(!r||!r.settings)throw new H(j.GETTING_SEARCH_BAR_CONFIG_FAILED,{details:"Invalid search bar config response format",params:e,response:s,data:r});return r}))}}const Ht=new $t,K="search-bar-root",jt=()=>h(void 0,null,function*(){var n,e,t;try{if(yield l.init(),l.aiShopperWidget&&l.aiShopperWidget!=="search-bar")return;if(!l.searchBarWidgetEnabled){A.log("SearchBarWidgetController: Search bar is disabled");return}if(!l.appUrl){A.log("SearchBarWidgetController: Invalid app url",l.appUrl);return}const s=yield Ht.getSearchBarConfig({appKey:l.appKey,appUrl:l.appUrl,productId:l.productId||void 0});if(!(s!=null&&s.settings)){A.log("Invalid search bar config",s);return}if(!s.settings){A.log("Search bar settings are missing");return}let r=document.getElementById(K);r||(r=document.createElement("div"),r.id=K,document.body.appendChild(r));const[{default:i},{default:a},{default:o}]=yield Promise.all([_(()=>import("./638a3ec7e6/search-bar-widget-app.DjD6TYDw.js"),[]),_(()=>import("./638a3ec7e6/render.utils.kaXty2Kk.js").then(d=>d.a),[]),_(()=>import("./638a3ec7e6/search-bar.DgNkEc_V.js"),[])]),c={appKey:l.appKey,appUrl:l.appUrl,productId:l.productId||void 0,placeholder:s.settings.placeholder,agentImage:s.shoppingAssistantConfig.agentSettings.agentImage,brandLogo:((e=(n=s.shoppingAssistantConfig)==null?void 0:n.brandSettings)==null?void 0:e.brandLogo)||"",hidePoweredBy:(t=s.shoppingAssistantConfig)==null?void 0:t.hidePoweredBy,preloadedStyles:o};yield a(i,L(T({},c),{element:r,hasStyledComponents:!0,widgetKey:Z.SEARCH_BAR,containerClasses:nt,containerStyles:it,fontFamily:et(document.body),preloadedStyles:o})),r.setAttribute("data-status",W.SUCCESS)}catch(s){console.error("Error initializing Search Bar:",s),I("Error initializing Search Bar",s);const r=document.getElementById(K);r&&r.setAttribute("data-status",W.FAILED)}}),Yt={PRODUCT_TILE:{key:At.PRODUCT_TILE,controller:()=>_(()=>import("./638a3ec7e6/product-tiles-controller.5UsbaJ_O.js"),[])},COLLECTION_PAGE_TILE:{key:wt,controller:()=>_(()=>import("./638a3ec7e6/collection-page-tile-controller.CB_nMNgu.js"),[])},TOP_QUESTIONS:{key:"top-questions",controller:()=>_(()=>import("./638a3ec7e6/top-questions-controller.CLZBAqIr.js"),[])},SEE_IT_ON_YOU:{key:"see-it-on-you",controller:()=>_(()=>import("./638a3ec7e6/see-it-on-you-controller.7KBWCdKJ.js"),[])}},zt={PRODUCT_GALLERY_VIDEO:{init:Vt},SHOP_ASSISTANT:{init:kt},SEARCH_BAR:{init:jt}},U={SUBSCRIBE:"TOLSTOY_WIDGET_V2_SUBSCRIBE",MESSAGE:"TOLSTOY_WIDGET_V2_MESSAGE"},R={ADD_TO_CART:"tolstoy_add_to_cart",SPOTLIGHT_QUICK_SHOP:"tolstoy_spotlight_carousel_quick_shop_click",PRODUCT_CARD_CLICK:"tolstoy_product_card_click"},b=class b{constructor(){p(this,"subscribers");p(this,"eventsStatus");p(this,"handleMessage",e=>{const{detail:t}=e;if(t!=null&&t.eventName)switch(t.eventName){case R.ADD_TO_CART:case R.SPOTLIGHT_QUICK_SHOP:case R.PRODUCT_CARD_CLICK:this.subscribe(t.eventName,t.callback);break;default:this.notifySubscribers(t.eventName,t)}});this.subscribers=new Map,this.eventsStatus={[R.ADD_TO_CART]:!1,[R.SPOTLIGHT_QUICK_SHOP]:!1,[R.PRODUCT_CARD_CLICK]:!1}}static getInstance(){return b.instance||(b.instance=new b),b.instance}formatMessage(e){return L(T({},e),{transmissionId:e.transmissionId||"widget-v2",timestamp:Date.now()})}updateEventStatus(e,t){e in this.eventsStatus&&(this.eventsStatus[e]=t)}isEventSubscribed(e){return this.eventsStatus[e]||!1}notifySubscribers(e,t){const s=this.subscribers.get(e);s==null||s.forEach(r=>{try{r(t)}catch(i){console.error(`Error in ${e} callback:`,i)}})}subscribe(e,t){var s;this.subscribers.has(e)||this.subscribers.set(e,new Set),(s=this.subscribers.get(e))==null||s.add(t),this.updateEventStatus(e,!0)}unsubscribe(e,t){const s=this.subscribers.get(e);s==null||s.delete(t),(s==null?void 0:s.size)===0&&this.updateEventStatus(e,!1)}postMessage(e){const t=this.formatMessage(e),s=new CustomEvent(U.MESSAGE,{detail:t});window.dispatchEvent(s)}init(){window.addEventListener(U.SUBSCRIBE,this.handleMessage),window.addEventListener(U.MESSAGE,this.handleMessage);const e=new CustomEvent(U.SUBSCRIBE);window.addEventListener("tolstoyWidgetReady",()=>{window.dispatchEvent(e)},{once:!0})}destroy(){window.removeEventListener(U.SUBSCRIBE,this.handleMessage),window.removeEventListener(U.MESSAGE,this.handleMessage)}};p(b,"instance");let k=b;const xt=()=>{k.getInstance().init()},x=()=>h(void 0,null,function*(){try{xt(),yield ut(Yt,zt)}catch(n){I(n)}});document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{x()}):x();export{re as C,k as E,Bt as G,ne as P,nt as S,l as T,ie as a,ae as b,it as c,Ft as d,rt as e,Ut as f,ce as g,oe as h,Nt as i,st as q};