!function(t){"function"==typeof define&&define.amd?define(t):t()}((function(){"use strict";var t,e,s,i,r,n,a=Object.defineProperty,o=Object.defineProperties,l=Object.getOwnPropertyDescriptors,c=Object.getOwnPropertySymbols,u=Object.prototype.hasOwnProperty,h=Object.prototype.propertyIsEnumerable,g=(t,e,s)=>e in t?a(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,p=(t,e)=>{for(var s in e||(e={}))u.call(e,s)&&g(t,s,e[s]);if(c)for(var s of c(e))h.call(e,s)&&g(t,s,e[s]);return t},d=(t,e,s)=>(g(t,"symbol"!=typeof e?e+"":e,s),s),f=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},m=(t,e,s)=>(f(t,e,"read from private field"),s?s.call(t):e.get(t)),b=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},y=(t,e,s,i)=>(f(t,e,"write to private field"),i?i.call(t,s):e.set(t,s),s),A=(t,e,s)=>new Promise(((i,r)=>{var n=t=>{try{o(s.next(t))}catch(e){r(e)}},a=t=>{try{o(s.throw(t))}catch(e){r(e)}},o=t=>t.done?i(t.value):Promise.resolve(t.value).then(n,a);o((s=s.apply(t,e)).next())}));function w(t,e,s=!1){return!(!t||!e)&&("string"!=typeof t&&(t=String(t)),s?t.includes(e):t.toLowerCase().includes(e.toLowerCase()))}function v(t){if(!t)return!1;if("number"==typeof t)return!0;return/^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/.test(t.toString())}function S(t){return function(t,e){let s=t.replace("\\","/").replace(/\\\//g,"/").trim(),i=window.location.hostname,r=window.location.protocol,n=s.split("."),a=!1;if(function(t){const e=t.toLowerCase(),s=["png","jpg","jpeg","gif","bmp","webp","eps","raw","svg","pdf","tiff","mp4","css","js","json","doc","csv","ppt","txt","psd","ai"];for(const i of s)if(e.includes(`.${i}`))return!0;return!1}(s))a=!0;else for(let o of[".php",".htm",".shtm",".asp"])if(s.toLowerCase().includes(o)){a=!0;break}"/"===s.substr(0,1)&&"//"!==s.substr(0,2)&&(s=s.substr(1));return"http"===s.substr(0,4)?s:"/"===s||s.length<1?`${r}//${s.substr(1)}${i}`:1===n.length||2===n.length&&a?`${r}//${i}${"/"===s.substr(0,1)?s:`/${s}`}`:"//"===s.substr(0,2)?r+s:`${r}//${s}`}(I(t))}function I(t,e=!1,s=!0){if(!t)return"";let i=t.split("#")[0].split("?")[0];return s||(i.startsWith("//")?i=i.slice(2):i.startsWith("/")&&(i=i.slice(1))),e&&i.endsWith("/")&&(i=i.slice(0,-1)),i}function C(t){if(t.currentSrc){const e=t.currentSrc;return e!==t.src&&(t.src=e),e}let e=t.getAttribute("src"),s=t.getAttribute("srcset"),i=t.getAttribute("data-srcset");if(!s&&i&&(s=S(i)),(!e||e.length<5)&&s&&s.length>=5){const i=s.split(","),r=i[Math.round(i.length/2)];if(r&&r.length>=5){const[s,i]=r.trim().split(" ");e=s.trim(),i&&w(i,"w")&&(e=s.trim()),t.src=e,t.removeAttribute("srcset")}}return t.src}class O{constructor(e){b(this,t,void 0),d(this,"imagesJSONCache",null),y(this,t,e)}process(){const t=[...this.getImagesToProcess()];this.processImages(t)}getAllImageElements(){return Array.from(document.querySelectorAll("img"))}getImagesToProcess(){const t=this.getAllImageElements(),e=[];for(const s of t){if(!s.complete){s.addEventListener("onload",(()=>this.process()));continue}let i=s.getAttribute("alt"),r=s.getAttribute("aria-label");if(r===i?s.removeAttribute("aria-label"):r&&r.trim().length>0&&(i=!i||i.trim().length<1?r:`${i} | ${r.trim()}`,s.setAttribute("alt",i),s.removeAttribute("aria-label")),this.isSpacerImage(s)&&s.setAttribute("role","presentation"),i&&!this.isAltOverridable(s,t))continue;const n=C(s);!n||n.length<5||w(s.src,"data:image/")?(s.setAttribute("alt",""),s.setAttribute("role","presentation")):(this.generateAltCandidate(s),"image"===s.getAttribute("data-ac-alt-type")&&"presentation"!==s.getAttribute("role")?e.push({element:s,src:n}):this.setAltAttribute(s))}return e}isSpacerImage(t){return t.complete&&t.offsetWidth<=16&&t.offsetHeight<=16&&!t.getAttribute("role")}isAltOverridable(t,e=this.getAllImageElements()){if(!t.alt)return!0;const s=t.alt.toLowerCase().trim();return!e.some((e=>e.alt.toLowerCase()===s&&e!==t&&e.src!==t.src))&&s.trim().replace(/\r?\n|\r/g,"").split(/(\s|-)+/).length<=1}generateAltCandidate(t){let e=t.getAttribute("src");const s=t.getAttribute("data-ac-bg-image");if(s&&s.length>4&&(e=t.getAttribute("data-ac-bg-image")),!e||e.length<5)return;const i=t.getAttribute("alt"),r=t.getAttribute("title"),n=t.getAttribute("aria-label");let a=null,o=null;i&&(a=r,o="alt"),!a&&n&&(null==n?void 0:n.length)>0&&(a=n,o="ariaLabel"),!a&&r&&(a=r,o="title"),a||(o="image",a=this.getFileAlt(t,e)),null!==o&&t.setAttribute("data-ac-alt-type",o)}getFileAlt(t,e){let s=function(t,e=!1){var s,i;let r=(!(i=t.split("/"))||i.length<1?i:i.filter((t=>!!t))).pop();return r=null!=(s=null==r?void 0:r.split("?")[0].split("#")[0])?s:"",e&&(r=function(t){const e=t.toLowerCase();return e.charAt(0).toUpperCase()+e.slice(1)}(r.replace(/-/g," "))),r.split(".")[0]}(e||t.src,!0);for(let i of s.split(" "))s!==i&&(i=i.toLowerCase(),(v(i.replace("px",""))||v(i.replace("x","")))&&(s=s.replace(` ${i}`,"")));return s}setAltAttribute(t){const e=t.getAttribute("data-ac-alt-candidate"),s=t.getAttribute("data-ac-alt-type"),i=t.getAttribute("data-ac-bg-image");e&&(i&&i.length>=5&&"IMG"!==t.tagName||t.setAttribute("alt",e),"image"===s&&"IMG"===t.tagName&&t.setAttribute("role","presentation"),t.removeAttribute("data-ac-alt-candidate"))}appendAltTagsToImages(t){var e;if(t.size<1)return;const s=this.getAllImageElements();for(const i of s){const s=C(i).split("?")[0],r=t.get(s);if(r&&(i.setAttribute("alt",null!=(e=null==r?void 0:r.alt)?e:"Fetching image text, please refresh the page."),t.delete(s),0===t.size))break}}processImages(e){return A(this,null,(function*(){if(e.length<1)return;const s=new Set,i=new Map;for(let t of e){if(!t.element)continue;const e=t.element.getAttribute("alt");if(e&&e.trim().length>0)continue;const i=I(t.src,!1,!0);s.add(i)}if(s.size<1)return;this.imagesJSONCache=this.imagesJSONCache||(yield m(this,t).fetchImages(s));const r=[];for(let e of s){for(let t in this.imagesJSONCache)t!==e||i.set(e,this.imagesJSONCache[t]);r.includes(e)||i.has(e)||m(this,t).isInStorage(e)||r.push(e)}r.length>0&&(yield m(this,t).processImages(r),m(this,t).cacheImages(new Map(r.map((t=>[t,null])))),this.imagesJSONCache=null),i.size>0&&(m(this,t).cacheImages(i),this.appendAltTagsToImages(i))}))}}t=new WeakMap;class ${constructor(t,e){d(this,"inProcess",new Map),d(this,"storageService"),d(this,"apiClient"),this.storageService=t,this.apiClient=e}isInProcess(t){return this.inProcess.has(t)}isInStorage(t){return this.storageService.has(t)}isCachedImage(t){return this.storageService.has(t)&&null!==this.getCachedImage(t)}getCachedImage(t){return this.storageService.get(t)}cacheImages(t){for(const[e,s]of t)this.storageService.store(e,s)}fetchImages(t){return A(this,null,(function*(){const e=yield this.apiClient.request({path:"/widget/alt-tags",method:"POST",body:JSON.stringify({images:Array.from(t)})});if(!e.ok)throw new Error(`Failed to fetch images: ${e.statusText}`);return(yield e.json()).payload}))}sendImages(t){return A(this,null,(function*(){const e=yield this.apiClient.request({path:"/widget/query-alt-tags",method:"POST",body:JSON.stringify({pageUrl:window.location.href,images:t})});if(!e.ok)throw new Error(`Failed to process images: ${e.statusText}`)}))}processImages(t){return A(this,null,(function*(){const e=t.filter((t=>!this.isInProcess(t)));e.forEach((t=>{this.inProcess.set(t,Date.now())})),yield this.sendImages(e),e.forEach((t=>{this.inProcess.delete(t)}))}))}}function E({token:t=null}){return{request(e){var s;e.headers=p((s=p({},e.headers),o(s,l({"Content-Type":"application/json"}))),t&&{"X-Accessibly-Api-Token":t});const i=function(e){const{path:s=""}=e;return`${t?"https://shopify.accessiblyapp.com/api/extension":"/apps/otmacessiblyapp"}${s}`}(e);return fetch(i,e)}}}class T{constructor(){d(this,"storageKey","accessibly:storage")}store(t,e){const s=this.getAll();s[t]=e,localStorage.setItem(this.storageKey,JSON.stringify(s))}has(t){return t in this.getAll()}get(t){return this.getAll()[t]}delete(t){const e=this.getAll();delete e[t],localStorage.setItem(this.storageKey,JSON.stringify(e))}update(t,e){const s=this.getAll();t in s&&(s[t]=e,localStorage.setItem(this.storageKey,JSON.stringify(s)))}getAll(){const t=localStorage.getItem(this.storageKey);return t?JSON.parse(t):{}}}class k{constructor(t){var a,o;b(this,e,void 0),b(this,s,void 0),b(this,i,void 0),b(this,r,!1),b(this,n,void 0),y(this,e,t),y(this,s,new T),y(this,n,E({token:null!=(o=null==(a=document.querySelector("script[data-accessibly-api-token]"))?void 0:a.getAttribute("data-accessibly-api-token"))?o:null}));const l=new $(m(this,s),m(this,n));y(this,i,new O(l))}fetchSettings(){return A(this,null,(function*(){return m(this,n).request({path:"/widget/settings",method:"GET"}).then((t=>{if(!t.ok)throw new Error(`Failed to fetch settings: ${t.statusText}`);return t.json()}))}))}isAccessiblyWidgetLoaded(){return void 0!==window.Accessibly}init(){this.isAccessiblyWidgetLoaded()&&(m(this,r)||this.loadWidget())}loadWidget(){return A(this,null,(function*(){const{settings:t}=yield this.fetchSettings(),s=p(p(p({},t),m(this,e)),{enabled:!0});window.Accessibly.init(s),y(this,r,!0),s.enableFetchAltTags&&m(this,i).process()}))}}function P(){!function(){var t;const e=document.createElement("script"),s=document.querySelector("head")||document.body,i=document.getElementById("accessibly-config"),r=(null==i?void 0:i.innerHTML)||"{}";e.src="https://cdn.accessibly.app/accessibly/2.0.0/accessibly.js",e.async=!0,(null==(t=window.Shopify)?void 0:t.shop)&&e.setAttribute("data-accessibly-shop",window.Shopify.shop),e.type="text/javascript",e.onload=function(){new k(JSON.parse(r)).init()},s.appendChild(e)}()}e=new WeakMap,s=new WeakMap,i=new WeakMap,r=new WeakMap,n=new WeakMap,"loading"===document.readyState?document.addEventListener("DOMContentLoaded",P):P(),window.manualInitAccessiblyShopify=function(){P()}}));