import { ResultTemplatesHelpers } from "@coveo/headless";
import { LitElement, css, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { createResultContextController } from "../result-template-component-utils/context/result-context-controller.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import "../../common/atomic-icon/atomic-icon.js";
import "../atomic-text/atomic-text.js";
import "../atomic-result-text/atomic-result-text.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.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 AtomicResultBadge extends LitElement {
    initialize() {}
    renderIcon() {
        return html`
      <atomic-icon
        part="result-badge-icon"
        .icon=${this.icon}
        class="h-3 w-3 fill-current"
      ></atomic-icon>
    `;
    }
    getTextContent() {
        if (void 0 !== this.field) return html`<atomic-result-text
        .field=${this.field}
      ></atomic-result-text>`;
        if (void 0 !== this.label) return html`<atomic-text .value=${this.label}></atomic-text>`;
        return html`<slot></slot>`;
    }
    renderText() {
        return html`<span part="result-badge-label"
      >${this.getTextContent()}</span
    >`;
    }
    renderBadge() {
        return html`
      <div
        part="result-badge-element"
        class="bg-neutral-light text-neutral-dark inline-flex h-full place-items-center space-x-1.5 rounded-full px-3"
      >
        ${when(this.icon, ()=>this.renderIcon())} ${this.renderText()}
      </div>
    `;
    }
    willUpdate() {
        if (this.field && this.resultContext.item) {
            const result = this.resultContext.item;
            const hasValue = null !== ResultTemplatesHelpers.getResultProperty(result, this.field);
            if (!hasValue) this.remove();
        }
    }
    render() {
        return this.renderBadge();
    }
    constructor(...args){
        super(...args), this.resultContext = createResultContextController(this);
    }
}
AtomicResultBadge.styles = css`:host{display:inline-flex;height:var(--row-height,auto);place-items:center;word-break:break-word}`;
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultBadge.prototype, "bindings", void 0);
_ts_decorate([
    property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicResultBadge.prototype, "field", void 0);
_ts_decorate([
    property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicResultBadge.prototype, "label", void 0);
_ts_decorate([
    property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicResultBadge.prototype, "icon", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultBadge.prototype, "render", null);
AtomicResultBadge = _ts_decorate([
    customElement('atomic-result-badge'),
    withTailwindStyles,
    bindings()
], AtomicResultBadge);
export { AtomicResultBadge };
