(function(f,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(f=typeof globalThis<"u"?globalThis:f||self,h(f["editor-script"]={}))})(this,function(f){"use strict";(function(){if(window.smart_editor_api_url||!document.getElementById("shopify-smart-editor-embedd"))return;const j=()=>{window.routes||(window.routes={cart_add_url:"/cart/add",cart_change_url:"/cart/change",cart_update_url:"/cart/update",cart_url:"/cart",predictive_search_url:"/search/suggest"})};window.smart_editor_api_url="https://smart-editor.app.426.agency",j();const u={productDetailAnchor:".durst-editpro-product-design-btn",editDesignCartClass:".durst-editpro-cart-edit-design-btn",thumbnailCartClass:".durst-editpro-cart-thumbnail-img"},b="productDataEditorForm",g=e=>{const n=[`a[href*="${e}"]`,`a[href*='variant=${e}']`,`input[id*="${e}"]`],a=["tr","[data-product-cart-line]",'[class*="row"]'];for(const i of n){const r=document.querySelectorAll(i);for(const o of r)if(!o.className.includes("ajax"))for(const t of a){const d=o.closest(t);if(d)return d}}};function D(e,n){const a=v(),i=document.querySelector(u.productDetailAnchor),r={}.VITE_FORM_ID;E(e,n,r,a).then(t=>{t&&i&&(t.style.display="none"),i===null&&t?x(e.calculate_in_editor):i.addEventListener("click",d=>{d.preventDefault(),d.stopPropagation(),t.submit()})})}function T(){const n=v().querySelector('[type="submit"]').className,a=["js-"];return n.split(" ").filter(i=>a.find(r=>i.includes(r))===void 0).join(" ")}function O(e,n){const a=g(n.variantId),i=g(n.imageVariantId);if(a&&!e.calculate_in_editor){const r=a.querySelectorAll('[href*="quantity=0"]');for(const o of r)o.style.display="none";r.length==0&&console.error("remove button not found")}if(i){const r=i.getElementsByTagName("input");for(const o of r)o&&!o.closest("form").id.includes(b)&&(o.readOnly=!0,o.style.cursor="no-drop")}}function F(e,n){const a=document.querySelector(u.editDesignCartClass+`[data-item-id*="${n.imageVariantId}"]`),i=document.querySelectorAll(u.editDesignCartClass+`[data-item-id*="${n.variantId}"]`);for(const s of i)s.style.display="none";const r=g(n.imageVariantId),o=(r==null?void 0:r.querySelector("[class*=title]"))||(r==null?void 0:r.querySelector("[class*='details']"));let t=r;o&&(t=o),t||console.error("could not anchor the editor to a product"),E(e,n,b+n.imageVariantId,t,!0).then(s=>{s&&a&&(s.style.display="none"),a.style.display="block",a.addEventListener("click",c=>{c.preventDefault(),c.stopPropagation(),s.submit()})})}function V(e,n){const a=n.querySelector("img"),i=document.querySelectorAll(u.thumbnailCartClass);console.log(i);let r=a;for(const o of i)o.getAttribute("data-item-id")&&e.createdProductId.includes(o.getAttribute("data-item-id"))&&(r=o);if(r&&r.querySelector("img")===null){let o=document.createElement("img");const t=Y();if(t?(o=t.cloneNode(!0),o.src=e.thumbnail):(o.loading="lazy",o.width=150,o.height=75,o.src=e.thumbnail),r.getAttribute("data-item-id")&&e.createdProductId.includes(r.getAttribute("data-item-id"))){a&&(a.style.display="none"),r.src=e.thumbnail;return}r.appendChild(o)}}function N(){const e=document.querySelectorAll('a[href*="quantity=0"]');let n;for(const r of e)(r==null?void 0:r.querySelector("svg, img"))==null&&(!n||n.length>r.className.length)&&(n=r.className);if(n)return n.split(" ").filter(r=>!r.includes("hide")).join(" ");const a=new Map;a.set("btn--secondary","btn btn--secondary btn--small");const i=document.getElementsByTagName("button");for(let r of a.keys())for(let o of i)if(o.className.includes(r))return a.get(r);return"underlined-link"}const x=e=>{if(document.querySelector("quantity-input")){const a=document.querySelector("quantity-input").parentElement;a.style.display=e?"none":"block"}document.querySelectorAll(`form[action="${routes.cart_add_url}"]`).forEach(a=>{a.querySelectorAll("button").forEach(r=>{r.style.display=e?"none":"block"})})},B=()=>{var n,a,i;const e=[(a=(n=window==null?void 0:window.Shopify)==null?void 0:n.currency)==null?void 0:a.active];return e.push((i=document.querySelector('meta[property="og:price:currency"]'))==null?void 0:i.content),e.find(r=>r!==void 0)},E=async(e,n,a,i,r=!1)=>{if(!e.active){C(),document.getElementById(a).remove();return}let o=document.createElement("form");o.id=a,o.action=`${e.editorUrl}?option=com_expresseditor&controller=expresseditor&task=editor_api2`,o.method="POST",o.style="margin-bottom:1rem;margin-top:1rem";let t=document.createElement("input");t.type="hidden",t.name="api2",t.value="1",o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="callback_url",t.value=`${location.origin}/apps/editor-button/`,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="exit_url",t.value=location.href,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="editor_id",t.value=e.editor_id,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="PrintjobID",t.value=r?e.print_job_id:e.template_id,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="article_number",t.value=n.variantId,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="product_name",t.value=n.title,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.className="smart-editor-quantity",t.name="quantityAPI2",t.value=n.quantity,o.appendChild(t),e.calculate_in_editor&&r||(t=document.createElement("input"),t.type="hidden",t.className="smart-editor-unit-price",t.name="priceFromFrontend",t.value=n.productPrice,o.appendChild(t)),t=document.createElement("input"),t.type="hidden",t.name="widthAPI2",t.value=e.width,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="heightAPI2",t.value=e.height,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="sel",t.value=e.sel_parameters,o.appendChild(t),t=document.createElement("input"),t.type="hidden",t.name="currency",t.value=B(),o.appendChild(t);const s={token:(await S()).token,origin:location.origin,reopen:r,imageVariantId:n.imageVariantId||null,locale:Shopify.locale,calculateInEditor:e.calculate_in_editor};t=document.createElement("input"),t.type="hidden",t.name="additionalData",t.value=encodeURIComponent(JSON.stringify(s)),o.appendChild(t);const c=document.getElementById(a);if(c&&c.remove(),r){const l=document.createElement("a");l.href="javascript:;",l.onclick=()=>(o.submit(),!1),l.className=N(),l.textContent=e.text,o.appendChild(l)}else{const l=document.createElement("button");l.className=`smart-editor-link ${T()}`,l.style="width:100%;",l.textContent=e.text,o.appendChild(l)}return r?i.appendChild(o):i.parentElement.insertBefore(o,i.nextSibling),o},C=()=>{const e=document.querySelector(u.productDetailAnchor);e&&(e.style.display="none")},_=()=>{var a,i,r;let e=new FormData;e.append("shopId",Shopify.shop);let n=(i=(a=window==null?void 0:window.meta)==null?void 0:a.product)==null?void 0:i.gid;n||(n=(r=document.querySelector('input[name="product-id"]'))==null?void 0:r.value),n&&(e.append("productId",n),e.append("locale",Shopify.locale),e.append("variantId",P()),fetch(`${smart_editor_api_url}/api/editor-button`,{method:"POST",body:e,headers:{Accept:"application/json"}}).then(async o=>{var p;const t=await o.json();let d=P();if(!d){C();return}let s=(p=window==null?void 0:window.meta)==null?void 0:p.product.variants.find(m=>m.id==d),c=W();c||(c=1);const l={quantity:c,variantId:d,productPrice:(s==null?void 0:s.price)/100,title:X()};D(t,l)}))},w=async()=>{const e=await S();(await M(e.token)).forEach(a=>{var s;const i=a.createdProductId.split("gid://shopify/ProductVariant/")[1];let r=e.items.findIndex(c=>a.variantId.includes(c.variant_id)&&JSON.stringify(c.properties).includes(i)),o=e.items.findIndex(c=>a.createdProductId.includes(c.variant_id)&&JSON.stringify(c.properties).includes(i));const t=e.items[r],d=o!=-1?e.items[o]:null;if(d!=null&&a.createdProductId){let c=g(d.variant_id);if(!c){console.error("created product not found");return}const l=document.querySelectorAll(`a[href*="${d.handle}"]`);t&&l.forEach(y=>{y.href=y.href.replace(d.handle,t.handle)}),V(a,c);const p=(s=c.querySelector('a[href*="quantity=0"]'))==null?void 0:s.getAttribute("href"),m=document.querySelector(`a[href="${p}"]`);m&&(m.href="javascript:;",m.onclick=async y=>{if(y.preventDefault(),y.stopPropagation(),await $(e.token,d.variant_id),t){const A={updates:{}};A.updates[t.key]=0,await fetch(routes.cart_update_url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(A)})}return location.reload(),!1})}})},q=async()=>{const e=await S();let n=new FormData;n.append("shopId",Shopify.shop),n.append("token",e.token),n.append("locale",Shopify.locale),fetch(`${smart_editor_api_url}/api/reopen-editor`,{method:"POST",body:n,headers:{Accept:"application/json"}}).then(async a=>{const i=await a.json();document.querySelectorAll(u.editDesignCartClass).forEach(o=>o.style.display="none"),i.forEach(o=>{var p;const t=o.variantId.split("gid://shopify/ProductVariant/")[1],d=(p=o.imageVariantID)==null?void 0:p.split("gid://shopify/ProductVariant/")[1];let s=t;o.calculate_in_editor&&(s=d);const c=e.items.find(m=>m.variant_id==s);if(!c)return;const l={quantity:c.quantity,variantId:t,productPrice:c.price/100,title:c.title,imageVariantId:d};O(o,l),F(o,l)})})},$=async(e,n)=>{let a=new FormData;return a.append("token",e),a.append("variantId",n),await(await fetch(`${smart_editor_api_url}/api/remove/from/cart`,{method:"POST",body:a,headers:{Accept:"application/json"}})).json()};function L(){const e=document.querySelectorAll('form[action*="/cart"]'),n=new MutationObserver(function(a){a.forEach(i=>{(i.id="shopping-cart-line-item-status")&&setTimeout(()=>{w(),q()},150)})});for(const a of e)n.observe(a,{attributeFilter:["aria-hidden"],subtree:!0})}const R=async(e,n)=>{let a=new FormData;a.append("backup_token",e),a.append("token",n),await fetch(`${smart_editor_api_url}/api/update-token`,{method:"POST",body:a,headers:{Accept:"application/json"}})},M=async e=>{let n=new FormData;return n.append("token",e),await(await fetch(`${smart_editor_api_url}/api/cart/lineItemInfo`,{method:"POST",body:n,headers:{Accept:"application/json"}})).json()},J=()=>{let e=new FormData;const n=new URLSearchParams(location.search),a=n.get("calculate-in-editor")==1,i=n.get("createdVariantId").split("gid://shopify/ProductVariant/")[1];a?e=U(n,e,i,n.get("variantId")):(e.append("items[0][id]",n.get("variantId")),e.append("items[0][quantity]",n.get("quantity")),e.append("items[0][properties][_cart-smart-editor-design-product]",i),e.append("items[0][properties][_cart-smart-editor-calculate-in-editor]",a),e.append("items[1][id]",i),e.append("items[1][quantity]",1),e.append("items[1][properties][_cart-smart-editor-design-img]",i),e.append("items[1][properties][_cart-smart-editor-calculate-in-editor]",a)),fetch(Shopify.routes.root+"cart/add.js",{method:"POST",body:e}).then(async()=>{const r=await S();n.has("removedCreatedVariantID")&&await z(n,r),r.token!=n.get("token")&&await R(n.get("backup_token"),r.token),location.href=location.origin+location.pathname})},U=(e,n,a,i)=>(n.append("items[0][id]",a),n.append("items[0][quantity]",e.get("quantity")??1),n.append("items[0][properties][_cart-smart-editor-design-img]",a),n.append("items[0][properties][_cart-smart-editor-associated-product]",i),n.append("items[0][properties][_cart-smart-editor-calculate-in-editor]",!0),n),z=async(e,n)=>{const a=e.get("removedCreatedVariantID").split("gid://shopify/ProductVariant/")[1],i=n.items.find(r=>JSON.stringify(r.properties).includes(a));if(i){const r=new FormData;r.append(`updates[${i.key}]`,0),await fetch(Shopify.routes.root+"cart/update.js",{method:"POST",body:r,headers:{Accept:"application/json"}})}};function H(){location.search.includes("smart-editor-cart")?J():(L(),w(),q())}const K=async e=>{let n=new FormData;return n.append("orderToken",e),n.append("locale",Shopify.locale),await(await fetch(`${smart_editor_api_url}/api/order/info`,{method:"POST",body:n,headers:{Accept:"application/json"}})).json()};function Q(){const e=location.pathname.split("/account/orders/")[1];K(e).then(n=>{const a=n.orderMedia;a&&a.forEach(i=>{const r=document.createElement("div"),o=document.createElement("img");o.src=i.thumbnail,o.style="max-height:150px; max-width: 150px",r.appendChild(o),document.querySelector(`[id*="${i.created_line_item_id}"]`).appendChild(r)})})}const v=()=>{var e;return(e=document.querySelector('form[action*="/cart/add"] button'))==null?void 0:e.closest("form")},I=(e,n,a)=>{e&&e.addEventListener(n,()=>{setTimeout(a,100)})};function G(){_();const e=v();e==null||e.querySelectorAll("input").forEach(i=>{I(i,"change",_)});const n=document.querySelector("variant-radios");I(n,"click",_);const a=document.querySelector('[name="quantity"]');I(a,"change",_)}const P=()=>document.querySelector('[name="id"]').value,W=()=>document.querySelector('[name="quantity"]').value,X=()=>document.querySelector('[property="og:title"]').getAttribute("content"),S=async()=>fetch(Shopify.routes.root+"cart.js").then(e=>e.json()),Y=()=>{const e=document.querySelectorAll('form[action="/cart"] img');for(const n of e)if(!n.className.includes(u.thumbnailCartClass))return n},k=()=>{document.querySelector('meta[property="og:type"]').getAttribute("content")==="product"?G():location.pathname=="/cart"?H():location.pathname.includes("account/orders")&&Q()};document.readyState!=="loading"?k():document.addEventListener("DOMContentLoaded",function(e){k()})})();const h=Object.freeze(Object.defineProperty({__proto__:null},Symbol.toStringTag,{value:"Module"}));f.default_theme=h,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});