 (() => {
     const noDecoratePathnames = ["/kindle-dbs/manga-store/webcomic/"];
     if (noDecoratePathnames.includes(window.location.pathname)) {
         return;
     }

     const hrefMatcher = new RegExp(`^${window.location.origin}/(gp/aw/d/|([^/]+/)?dp/|gp/product/)(?<asin>[A-Z0-9]{10})\\W`);
     const srcMatcher = new RegExp(`m.media[^/]+/images/I/(?<physicalId>[^.]+).*[.]jpg`);

     const callbackItems = [];
     window.addEventListener("qvIndexUpdate", (event) => {
         if (event?.detail?.state === "UPDATING") {
             callbackItems[event?.detail?.index]?.scrollIntoView({ block: "center", inline: "center" });
         }
     }, false);

     const scrapeMetdataAndLaunchQuickView = (activatedItem, parentSelector, itemSelector) => {
         const parent = activatedItem?.closest(`${parentSelector}`);
         if (!parent) { return; }
         let index = -1;
         let initialIndex = 0;
         const metadatas = [];
         const items = [];
         parent.querySelectorAll(`${itemSelector}`).forEach(item => {
             const metadata = { asin: "", physicalId: "", title: "", authors: "", additionalData: {} };
             let foundAsin = false;
             let foundPhysicalId = false;
             item.querySelectorAll('a.a-link-normal').forEach(a => {
                 if (foundAsin && foundPhysicalId) { return; }
                 const asin = a.href.match(hrefMatcher)?.groups['asin'];
                 if (!asin) { return; }
                 if (!foundAsin) {
                     foundAsin = true;
                     metadata.asin = asin;
                     metadata.additionalData.detailLink = a.href;
                 }
                 if (!foundPhysicalId) {
                     a.querySelectorAll('img').forEach(img => {
                         if (foundPhysicalId) { return; }
                         const physicalId = img.src.match(srcMatcher)?.groups['physicalId'];
                         if (physicalId) {
                             foundPhysicalId = true;
                             metadata.physicalId = physicalId;
                             metadata.additionalData.coverImageUrl = img.src;
                         }
                     });
                 }
             });
             if (metadata.asin) {
                 index += 1;
                 metadatas.push(metadata);
                 if (item === activatedItem) { initialIndex = index; }
                 items.push(item);
             }
         });
         if (metadatas.length > 0 && typeof window.BrowserHost?.launchQuickView === 'function') {
             callbackItems.length = 0;
             callbackItems.push(...items);
             window.BrowserHost?.launchQuickView({ initialIndex: initialIndex, asins: metadatas });
             return true;
         }
     };

     const decorate = (parentSelector, itemSelector) => {
         const clickHandler = (event) => {
             const asin = event.target.closest('a.a-link-normal')?.href?.match(hrefMatcher)?.groups['asin'];
             if (asin) {
                 const item = event.target.closest(itemSelector);
                 if (scrapeMetdataAndLaunchQuickView(item, parentSelector, itemSelector)) {
                     event.stopImmediatePropagation();
                     event.preventDefault();
                     window.BrowserHost?.triggerQvWeblab();
                 }
             }
         };
         // Dedupe parents
         const parents = [];
         document.querySelectorAll(`${parentSelector} > ${itemSelector}`).forEach(child => {
             if (child.querySelector('a.a-link-normal')) {
                 const parent = child.closest(parentSelector);
                 if (parent && !parents.includes[parent]) { parents.push(parent); }
             }
          });
         parents.forEach(parent => { parent.addEventListener('click', clickHandler, false); });
     };

     //decorate('ol.a-carousel', 'li.a-carousel-card'); // AUI carousels
     decorate('ol.a-carousel', 'li.a-carousel-card:not([class*="manga-store"])'); // AUI carousels, except the manga store ones
     decorate('ol.a-carousel', 'li.a-carousel-card > div[class*="manga-store"]'); // AUI carousels with manga store special sauce
     decorate('div[class*="grid-container"]', 'div[class*="grid"] div[class*="grid-item"]'); // Manga store ranked grids
     decorate('#browse-list-view', '.browse-clickable-item'); // Search
     decorate('#browse-grid-view', '.browse-clickable-item'); // Search on wide screens
     decorate('#series-childAsin-list', '.series-childAsin-batch > .series-childAsin-item'); // Series
     decorate('.a-section :not(.aos-single-asin-section)', '.aos-single-asin-section'); // Fancy series
 })()
