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 { getAttributesFromLinkSlotContent } from "../../common/item-link/attributes-slot.js";
import { renderLinkWithItemAnalytics } from "../../common/item-link/item-link.js";
import { createInteractiveResultContextController } from "../result-template-component-utils/context/interactive-result-context-controller.js";
import { createResultContextController } from "../result-template-component-utils/context/result-context-controller.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { LightDomMixin } from "../../../mixins/light-dom.js";
import { SlotsForNoShadowDOMMixin } from "../../../mixins/slots-for-no-shadow-dom-mixin.js";
import { buildCustomEvent } from "../../../utils/event-utils.js";
import { buildStringTemplateFromResult } from "../../../utils/result-utils.js";
import "../atomic-result-text/atomic-result-text.js";
import atomic_result_link_tw_css from "./atomic-result-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 AtomicResultLink extends LightDomMixin(SlotsForNoShadowDOMMixin(LitElement)) {
    initialize() {
        if (!this.result && this.resultContext.item) {
            const item = this.resultContext.item;
            if ('result' in item) this.result = item.result;
            else this.result = item;
        }
        if (!this.interactiveResult && this.interactiveResultContext.interactiveItem) this.interactiveResult = this.interactiveResultContext.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.result && this.interactiveResult, ()=>{
            const result = this.result;
            const interactiveResult = this.interactiveResult;
            const href = isUndefined(this.hrefTemplate) ? result.clickUri : buildStringTemplateFromResult(this.hrefTemplate, result, this.bindings);
            return renderLinkWithItemAnalytics({
                props: {
                    href,
                    onSelect: ()=>interactiveResult.select(),
                    onBeginDelayedSelect: ()=>interactiveResult.beginDelayedSelect(),
                    onCancelPendingSelect: ()=>interactiveResult.cancelPendingSelect(),
                    attributes: this.linkAttributes,
                    stopPropagation: this.stopPropagation,
                    onInitializeLink: (cleanupCallback)=>{
                        if (this.removeLinkEventHandlers) this.removeLinkEventHandlers();
                        this.removeLinkEventHandlers = cleanupCallback;
                    }
                }
            })(html`
        ${this.renderDefaultSlotContent(html`<atomic-result-text
            field="title"
            default="no-title"
          ></atomic-result-text>`)}
      `);
        })}`;
    }
    constructor(...args){
        super(...args), this.resultContext = createResultContextController(this), this.interactiveResultContext = createInteractiveResultContextController(this);
    }
}
AtomicResultLink.styles = atomic_result_link_tw_css;
_ts_decorate([
    property({
        reflect: true,
        attribute: 'href-template'
    }),
    _ts_metadata("design:type", String)
], AtomicResultLink.prototype, "hrefTemplate", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Array)
], AtomicResultLink.prototype, "linkAttributes", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Boolean)
], AtomicResultLink.prototype, "stopPropagation", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Result ? Object : Result)
], AtomicResultLink.prototype, "result", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InteractiveResult ? Object : InteractiveResult)
], AtomicResultLink.prototype, "interactiveResult", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultLink.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultLink.prototype, "error", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultLink.prototype, "render", null);
AtomicResultLink = _ts_decorate([
    customElement('atomic-result-link'),
    bindings()
], AtomicResultLink);
export { AtomicResultLink };
