import { LitElement, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { ref } from "lit/directives/ref.js";
import { resultComponentClass } from "../../common/item-list/item-list-common.js";
import { CustomRenderController } from "../../common/layout/custom-render-controller.js";
import { ItemLayoutController } from "../../common/layout/item-layout-controller.js";
import { booleanConverter } from "../../../converters/boolean-converter.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { ChildrenUpdateCompleteMixin } from "../../../mixins/children-update-complete-mixin.js";
import { parentNodeToString } from "../../../utils/dom-utils.js";
import atomic_product_tw_css from "./atomic-product.tw.css.js";
import "../atomic-product-text/atomic-product-text.js";
import "../atomic-product-link/atomic-product-link.js";
import "../atomic-product-image/atomic-product-image.js";
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 AtomicProduct extends ChildrenUpdateCompleteMixin(LitElement) {
    async connectedCallback() {
        super.connectedCallback();
        new CustomRenderController(this, {
            renderingFunction: ()=>this.renderingFunction,
            itemData: ()=>this.product,
            rootElementRef: ()=>this.productRootRef,
            linkContainerRef: ()=>this.linkContainerRef,
            onRenderComplete: (element, output)=>{
                this.itemLayoutController.applyLayoutClassesToElement(element, output);
            }
        });
        this.itemLayoutController = new ItemLayoutController(this, {
            elementPrefix: 'atomic-product',
            renderingFunction: ()=>this.renderingFunction,
            content: ()=>this.content,
            layoutConfig: ()=>({
                    display: this.display,
                    density: this.density,
                    imageSize: this.imageSize
                }),
            itemClasses: ()=>this.classes
        });
        this.addEventListener('atomic/resolveResult', this.resolveProduct);
        this.addEventListener('atomic/resolveInteractiveResult', this.resolveInteractiveProduct);
        this.addEventListener('atomic/resolveStopPropagation', this.resolveStopPropagation);
        this.addEventListener('atomic/resolveResultDisplayConfig', this.resolveProductDisplayConfig);
        this.addEventListener('click', this.handleClick);
        await this.getUpdateComplete();
        this.classList.add('hydrated');
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.removeEventListener('atomic/resolveResult', this.resolveProduct);
        this.removeEventListener('atomic/resolveInteractiveResult', this.resolveInteractiveProduct);
        this.removeEventListener('atomic/resolveStopPropagation', this.resolveStopPropagation);
        this.removeEventListener('atomic/resolveResultDisplayConfig', this.resolveProductDisplayConfig);
        this.removeEventListener('click', this.handleClick);
    }
    getContentHTML() {
        if (!this.content) {
            console.warn('atomic-product: content property is undefined. Cannot get content HTML.', this);
            return '';
        }
        return parentNodeToString(this.content);
    }
    getLinkHTML() {
        return parentNodeToString(this.linkContent ?? new HTMLElement());
    }
    render() {
        if (void 0 !== this.renderingFunction) return html`
        <div class=${resultComponentClass}>
          <div
            class="result-root"
            ${ref((el)=>{
            this.productRootRef = el;
        })}
          ></div>
          <div
            class="link-container"
            ${ref((el)=>{
            this.linkContainerRef = el;
        })}
          ></div>
        </div>
      `;
        if (!this.itemLayoutController.getLayout()) return html`<div class=${resultComponentClass}></div>`;
        return html`
      <div class=${resultComponentClass}>
        <div
          class="result-root ${this.itemLayoutController.getCombinedClasses().join(' ')}"
          .innerHTML=${this.getContentHTML()}
        ></div>
        <div class="link-container" .innerHTML=${this.getLinkHTML()}></div>
      </div>
    `;
    }
    firstUpdated(_changedProperties) {
        if (this.loadingFlag && this.store) this.store.unsetLoadingFlag(this.loadingFlag);
    }
    constructor(...args){
        super(...args), this.linkContent = new DocumentFragment(), this.display = 'list', this.density = 'normal', this.imageSize = 'icon', this.classes = '', this.resolveProduct = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            event.detail(this.product);
        }, this.resolveInteractiveProduct = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            if (this.interactiveProduct) event.detail(this.interactiveProduct);
        }, this.resolveStopPropagation = (event)=>{
            event.detail(this.stopPropagation);
        }, this.resolveProductDisplayConfig = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            event.detail({
                density: this.density,
                imageSize: this.imageSize
            });
        }, this.handleClick = (event)=>{
            if (this.stopPropagation) event.stopPropagation();
            if ('grid' === this.display) this.clickLinkContainer();
        }, this.clickLinkContainer = ()=>{
            this.shadowRoot?.querySelector('.link-container > atomic-product-link a:not([slot])')?.click();
        };
    }
}
AtomicProduct.styles = atomic_product_tw_css;
_ts_decorate([
    property({
        attribute: 'stop-propagation',
        type: Boolean,
        converter: booleanConverter,
        reflect: true
    }),
    _ts_metadata("design:type", Boolean)
], AtomicProduct.prototype, "stopPropagation", void 0);
_ts_decorate([
    property({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof Product ? Object : Product)
], AtomicProduct.prototype, "product", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: 'interactive-product'
    }),
    _ts_metadata("design:type", "u" < typeof InteractiveProduct ? Object : InteractiveProduct)
], AtomicProduct.prototype, "interactiveProduct", void 0);
_ts_decorate([
    property({
        type: Object
    }),
    _ts_metadata("design:type", Object)
], AtomicProduct.prototype, "store", void 0);
_ts_decorate([
    property({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof ParentNode ? Object : ParentNode)
], AtomicProduct.prototype, "content", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: 'link-content'
    }),
    _ts_metadata("design:type", "u" < typeof ParentNode ? Object : ParentNode)
], AtomicProduct.prototype, "linkContent", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: String
    }),
    _ts_metadata("design:type", "u" < typeof ItemDisplayLayout ? Object : ItemDisplayLayout)
], AtomicProduct.prototype, "display", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: String
    }),
    _ts_metadata("design:type", "u" < typeof ItemDisplayDensity ? Object : ItemDisplayDensity)
], AtomicProduct.prototype, "density", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: String,
        attribute: 'image-size'
    }),
    _ts_metadata("design:type", "u" < typeof ItemDisplayImageSize ? Object : ItemDisplayImageSize)
], AtomicProduct.prototype, "imageSize", void 0);
_ts_decorate([
    property({
        type: String
    })
], AtomicProduct.prototype, "classes", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'loading-flag'
    }),
    _ts_metadata("design:type", String)
], AtomicProduct.prototype, "loadingFlag", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: 'rendering-function'
    }),
    _ts_metadata("design:type", "u" < typeof ItemRenderingFunction ? Object : ItemRenderingFunction)
], AtomicProduct.prototype, "renderingFunction", void 0);
AtomicProduct = _ts_decorate([
    customElement('atomic-product'),
    withTailwindStyles
], AtomicProduct);
export { AtomicProduct };
