(function () {
    function iapcReplaceCartBlockThumbnails() {
        document.querySelectorAll(
            ".wc-block-cart-items__row, .wc-block-mini-cart-items .wc-block-cart-items__row"
        ).forEach(function (row) {
            const pngLink = row.querySelector('a[href*="iapc-designs"]');
            const img = row.querySelector(
                ".wc-block-cart-item__image img, .wc-block-components-product-image img"
            );

            if (!img) {
                return;
            }

            if (pngLink) {
                img.src = pngLink.href;
                img.setAttribute("src", pngLink.href);
            }

            img.removeAttribute("srcset");
            img.removeAttribute("sizes");
            img.classList.add("iapc-cart-custom-thumb");
        });
    }

    document.addEventListener("DOMContentLoaded", iapcReplaceCartBlockThumbnails);
    window.addEventListener("load", iapcReplaceCartBlockThumbnails);

    const observer = new MutationObserver(iapcReplaceCartBlockThumbnails);
    observer.observe(document.body, { childList: true, subtree: true });
})();
