"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__, {
    AtomicProductChildren: ()=>AtomicProductChildren
});
const commerce_namespaceObject = require("@coveo/headless/commerce");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const product_context_controller_cjs_namespaceObject = require("../product-template-component-utils/context/product-context-controller.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const event_utils_cjs_namespaceObject = require("../../../utils/event-utils.cjs");
const xss_utils_cjs_namespaceObject = require("../../../utils/xss-utils.cjs");
const button_cjs_namespaceObject = require("../../common/button.cjs");
require("../atomic-commerce-text/atomic-commerce-text.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const multi_class_map_cjs_namespaceObject = require("../../../directives/multi-class-map.cjs");
const light_dom_cjs_namespaceObject = require("../../../mixins/light-dom.cjs");
const dom_utils_cjs_namespaceObject = require("../../../utils/dom-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);
}
const AMOUNT_OF_VISIBLE_CHILDREN = 5;
class AtomicProductChildren extends (0, light_dom_cjs_namespaceObject.LightDomMixin)(external_lit_namespaceObject.LitElement) {
    initialize() {
        const product = this.productController.item;
        if (product) {
            this.activeChildId = product.permanentid;
            this.childProducts = product.children ?? [];
        }
    }
    onSelectChild(child) {
        this.activeChildId = child.permanentid;
        this.dispatchEvent((0, event_utils_cjs_namespaceObject.buildCustomEvent)('atomic/selectChildProduct', {
            child
        }));
    }
    getImageUrl(child) {
        const value = commerce_namespaceObject.ProductTemplatesHelpers.getProductProperty(child, this.field);
        if ('string' == typeof value) return (0, xss_utils_cjs_namespaceObject.filterProtocol)(value);
        if (Array.isArray(value) && 'string' == typeof value[0]) return (0, xss_utils_cjs_namespaceObject.filterProtocol)(value[0]);
        return (0, xss_utils_cjs_namespaceObject.filterProtocol)(this.fallback);
    }
    renderChild(child) {
        const childName = child.ec_name ?? '';
        const buttonClasses = (0, multi_class_map_cjs_namespaceObject.tw)({
            'product-child': true,
            'box-border rounded border border-primary': child.permanentid === this.activeChildId
        });
        return (0, external_lit_namespaceObject.html)`
      <button
        class="${(0, multi_class_map_cjs_namespaceObject.multiClassMap)(buttonClasses)}"
        title=${childName}
        @keypress=${(event)=>'Enter' === event.key && this.onSelectChild(child)}
        @mouseenter=${()=>this.onSelectChild(child)}
        @touchstart=${(event)=>{
            event.stopPropagation();
            event.preventDefault();
            this.onSelectChild(child);
        }}
        @click=${this.handleClick}
      >
        <img
          class="aspect-square p-1"
          src=${this.getImageUrl(child)}
          alt=${childName}
          loading="lazy"
        />
      </button>
    `;
    }
    renderLabel() {
        return (0, external_lit_namespaceObject.html)`
      <div class="text-neutral-dark my-2 font-semibold">
        <atomic-commerce-text
          .value=${this.bindings.i18n.t(this.label)}
        ></atomic-commerce-text>
      </div>
    `;
    }
    get count() {
        const hiddenChildrenCount = Math.max(0, this.childProducts.length - AMOUNT_OF_VISIBLE_CHILDREN);
        return this.productController.item?.totalNumberOfChildren - this.productController.item?.children.length + hiddenChildrenCount;
    }
    render() {
        if (0 === this.childProducts.length) return external_lit_namespaceObject.nothing;
        const visibleChildren = this.childProducts.slice(0, AMOUNT_OF_VISIBLE_CHILDREN);
        return (0, external_lit_namespaceObject.html)`
      ${'' !== this.label.trim() ? this.renderLabel() : external_lit_namespaceObject.nothing}
      <div class="flex flex-wrap">
        ${visibleChildren.map((child)=>this.renderChild(child))}
        ${this.count > 0 ? (0, button_cjs_namespaceObject.renderButton)({
            props: {
                style: 'text-primary',
                class: 'product-child',
                onClick: this.handleClick
            }
        })((0, external_lit_namespaceObject.html)`+${this.count}`) : external_lit_namespaceObject.nothing}
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.productController = (0, product_context_controller_cjs_namespaceObject.createProductContextController)(this), this.childProducts = [], this.activeChildId = '', this.label = 'available-in', 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="gray"%3E%3C/rect%3E%3C/svg%3E', this.handleClick = (event)=>{
            if (this.parentElement?.tagName !== 'A') (0, dom_utils_cjs_namespaceObject.closest)(this, 'atomic-product').clickLinkContainer();
            else event.stopPropagation();
        };
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicProductChildren.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", Array)
], AtomicProductChildren.prototype, "childProducts", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", String)
], AtomicProductChildren.prototype, "activeChildId", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)(),
    _ts_metadata("design:type", String)
], AtomicProductChildren.prototype, "label", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductChildren.prototype, "field", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductChildren.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)
], AtomicProductChildren.prototype, "render", null);
AtomicProductChildren = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-product-children'),
    (0, bindings_cjs_namespaceObject.bindings)()
], AtomicProductChildren);
exports.AtomicProductChildren = __webpack_exports__.AtomicProductChildren;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicProductChildren"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
