import { isUndefined } from "@coveo/bueno";
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { createInteractiveProductContextController } from "../product-template-component-utils/context/interactive-product-context-controller.js";
import { getAttributesFromLinkSlotContent } from "../../common/item-link/attributes-slot.js";
import { renderLinkWithItemAnalytics } from "../../common/item-link/item-link.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { buildCustomEvent } from "../../../utils/event-utils.js";
import { buildStringTemplateFromProduct } from "../product-template-component-utils/product-utils.js";
import "../atomic-product-text/atomic-product-text.js";
import { createProductContextController } from "../product-template-component-utils/context/product-context-controller.js";
import { LightDomMixin } from "../../../mixins/light-dom.js";
import { SlotsForNoShadowDOMMixin } from "../../../mixins/slots-for-no-shadow-dom-mixin.js";
import atomic_product_link_tw_css from "./atomic-product-link.tw.css.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 AtomicProductLink extends LightDomMixin(SlotsForNoShadowDOMMixin(LitElement)) {
    logWarningIfNeeded(warning) {
        if (warning) this.bindings.engine.logger.warn(warning);
    }
    initialize() {
        if (!this.product && this.productController.item) this.product = this.productController.item;
        if (!this.interactiveProduct && this.interactiveProductController.interactiveItem) this.interactiveProduct = this.interactiveProductController.interactiveItem;
        this.dispatchEvent(buildCustomEvent('atomic/resolveStopPropagation', (stopPropagation)=>{
            this.stopPropagation = stopPropagation;
        }));
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        if (this.removeLinkEventHandlers) {
            this.removeLinkEventHandlers();
            this.removeLinkEventHandlers = void 0;
        }
    }
    willUpdate(changedProperties) {
        super.willUpdate(changedProperties);
        this.linkAttributes = getAttributesFromLinkSlotContent(this, 'attributes');
    }
    render() {
        return html`${when(this.product && this.interactiveProduct, ()=>{
            const product = this.product;
            const interactiveProduct = this.interactiveProduct;
            const href = isUndefined(this.hrefTemplate) ? product.clickUri : buildStringTemplateFromProduct(this.hrefTemplate, product, this.bindings);
            const { warningMessage } = interactiveProduct;
            return renderLinkWithItemAnalytics({
                props: {
                    href,
                    onSelect: ()=>{
                        this.logWarningIfNeeded(warningMessage);
                        interactiveProduct.select();
                    },
                    onBeginDelayedSelect: ()=>{
                        this.logWarningIfNeeded(warningMessage);
                        interactiveProduct.beginDelayedSelect();
                    },
                    onCancelPendingSelect: ()=>{
                        this.logWarningIfNeeded(warningMessage);
                        interactiveProduct.cancelPendingSelect();
                    },
                    attributes: this.linkAttributes,
                    stopPropagation: this.stopPropagation,
                    onInitializeLink: (cleanupCallback)=>{
                        if (this.removeLinkEventHandlers) this.removeLinkEventHandlers();
                        this.removeLinkEventHandlers = cleanupCallback;
                    }
                }
            })(html`
        ${this.renderDefaultSlotContent(html`<atomic-product-text
            field="ec_name"
            default="no-title"
          ></atomic-product-text>`)}
      `);
        })}`;
    }
    constructor(...args){
        super(...args), this.productController = createProductContextController(this), this.interactiveProductController = createInteractiveProductContextController(this);
    }
}
AtomicProductLink.styles = atomic_product_link_tw_css;
_ts_decorate([
    property({
        type: String,
        attribute: 'href-template',
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductLink.prototype, "hrefTemplate", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Product ? Object : Product)
], AtomicProductLink.prototype, "product", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InteractiveProduct ? Object : InteractiveProduct)
], AtomicProductLink.prototype, "interactiveProduct", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicProductLink.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicProductLink.prototype, "error", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Array)
], AtomicProductLink.prototype, "linkAttributes", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicProductLink.prototype, "render", null);
AtomicProductLink = _ts_decorate([
    customElement('atomic-product-link'),
    bindings()
], AtomicProductLink);
export { AtomicProductLink };
