"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AtomicProductImage: ()=>AtomicProductImage
});
const bueno_namespaceObject = require("@coveo/bueno");
const commerce_namespaceObject = require("@coveo/headless/commerce");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const product_context_controller_cjs_namespaceObject = require("../product-template-component-utils/context/product-context-controller.cjs");
const image_carousel_cjs_namespaceObject = require("../../common/image-carousel/image-carousel.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const xss_utils_cjs_namespaceObject = require("../../../utils/xss-utils.cjs");
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
class AtomicProductImage extends external_lit_namespaceObject.LitElement {
    initialize() {
        if (!this.product && this.productController.item) this.product = this.productController.item;
    }
    previousImage() {
        this.currentImage = this.currentImage >= 1 ? this.currentImage - 1 : this.numberOfImages - 1;
    }
    nextImage() {
        this.currentImage = (this.currentImage + 1) % this.numberOfImages;
    }
    navigateToImage(index) {
        if (index < 0 || index >= this.numberOfImages) return;
        this.currentImage = index;
    }
    logWarning(message) {
        this.bindings.engine.logger.warn(message);
    }
    handleImageError(event) {
        const image = event.target;
        const message = `The image url "${image.src}" is not valid or could not be loaded. You might want to add a "fallback" property.`;
        if (this.fallback && image.src !== this.fallback) this.useFallback = true;
        else if (image.src === this.fallback) this.logWarning('The fallback image failed to load.');
        else this.logWarning(message);
    }
    handleMissingFallback(message) {
        if (!this.fallback) {
            this.logWarning(message);
            this.isFallbackMissing = true;
            return null;
        }
        return this.fallback;
    }
    validateUrl(url) {
        if (!url) {
            const message = `Image for ${this.product.ec_name} is missing. Please review your indexing. You might want to add a "fallback" property.`;
            return this.handleMissingFallback(message);
        }
        if ('string' != typeof url) {
            const message = `Expected "${this.field}" to be a text field. Please review your indexing. You might want to add a "fallback" property.`;
            return this.handleMissingFallback(message);
        }
        return url;
    }
    filterValidImageUrls() {
        return this.imageUrls.filter((image)=>'string' == typeof image);
    }
    getAltText(index, max, validImageAlt) {
        if ('string' == typeof validImageAlt) return validImageAlt;
        return this.bindings.i18n.t('image-alt-fallback-multiple', {
            count: index + 1,
            max,
            itemName: this.product.ec_name
        });
    }
    buildImage(url, index) {
        const finalUrl = this.useFallback ? this.fallback : url;
        this.validateUrl(finalUrl);
        const validImageAlt = this.imageAlt;
        const altText = this.getAltText(index, this.filterValidImageUrls().length, validImageAlt);
        return {
            src: (0, xss_utils_cjs_namespaceObject.filterProtocol)(finalUrl),
            alt: altText
        };
    }
    get images() {
        return this.filterValidImageUrls().map((url, index)=>this.buildImage(url, index));
    }
    get imageUrls() {
        try {
            const value = commerce_namespaceObject.ProductTemplatesHelpers.getProductProperty(this.product, this.field);
            return Array.isArray(value) ? value : [
                value
            ];
        } catch (error) {
            this.error = error;
            return [];
        }
    }
    get imageAlt() {
        if (!this.imageAltField) return null;
        const value = commerce_namespaceObject.ProductTemplatesHelpers.getProductProperty(this.product, this.imageAltField);
        if ((0, bueno_namespaceObject.isNullOrUndefined)(value)) return null;
        return value.trim();
    }
    get numberOfImages() {
        return this.images.length;
    }
    renderCurrentImage(image) {
        if (null === image) return external_lit_namespaceObject.nothing;
        return (0, external_lit_namespaceObject.html)`
      <img
        part="product-image"
        class="aspect-square w-full h-full object-contain"
        alt=${image.alt}
        src="${image.src}"
        @error=${(event)=>this.handleImageError(event)}
        loading="lazy"
      />
    `;
    }
    render() {
        if (null === this.product || void 0 === this.product) return external_lit_namespaceObject.nothing;
        if (this.isFallbackMissing) return external_lit_namespaceObject.nothing;
        const alt = this.imageAlt ? this.imageAlt : this.bindings.i18n.t('image-not-found-alt', {
            itemName: this.product.ec_name
        });
        return (0, external_lit_namespaceObject.html)`
      ${(0, when_js_namespaceObject.when)(0 === this.images.length, ()=>{
            this.validateUrl(this.fallback);
            return (0, external_lit_namespaceObject.html)`
            <img
              part="product-image"
              class="aspect-square w-full h-full object-contain"
              alt=${alt}
              src="${this.fallback}"
              loading="lazy"
            />
          `;
        }, ()=>(0, when_js_namespaceObject.when)(1 === this.images.length, ()=>this.renderCurrentImage(this.images[this.currentImage]), ()=>(0, image_carousel_cjs_namespaceObject.renderImageCarousel)({
                    props: {
                        bindings: this.bindings,
                        currentImage: this.currentImage,
                        navigateToImage: (index)=>this.navigateToImage(index),
                        nextImage: ()=>this.nextImage(),
                        previousImage: ()=>this.previousImage(),
                        numberOfImages: this.numberOfImages,
                        label: this.bindings.i18n.t('image-gallery', {
                            itemName: this.product.ec_name
                        })
                    }
                })(this.renderCurrentImage(this.images[this.currentImage]))))}
    `;
    }
    constructor(...args){
        super(...args), this.productController = (0, product_context_controller_cjs_namespaceObject.createProductContextController)(this), this.useFallback = false, this.currentImage = 0, this.isFallbackMissing = false, this.field = 'ec_thumbnails', this.fallback = 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50"%3E%3Crect width="50" height="50" fill="none" stroke="none"%3E%3C/rect%3E%3C/svg%3E';
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicProductImage.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Product ? Object : Product)
], AtomicProductImage.prototype, "product", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicProductImage.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicProductImage.prototype, "useFallback", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicProductImage.prototype, "currentImage", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductImage.prototype, "field", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true,
        attribute: 'image-alt-field'
    }),
    _ts_metadata("design:type", String)
], AtomicProductImage.prototype, "imageAltField", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductImage.prototype, "fallback", void 0);
_ts_decorate([
    (0, binding_guard_cjs_namespaceObject.bindingGuard)(),
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicProductImage.prototype, "render", null);
AtomicProductImage = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-product-image'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicProductImage);
exports.AtomicProductImage = __webpack_exports__.AtomicProductImage;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicProductImage"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
