import { ProductTemplatesHelpers } from "@coveo/headless/commerce";
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { createProductContextController } from "../product-template-component-utils/context/product-context-controller.js";
import atomic_rating_tw_css from "../../common/atomic-rating/atomic-rating.tw.css.js";
import { computeNumberOfStars } from "../../common/atomic-rating/rating-utils.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 star from "../../../images/star.js";
import { renderRating } from "../../common/atomic-rating/rating.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 AtomicProductRating extends LightDomMixin(LitElement) {
    initialize() {
        if (!this.product && this.productController.item) this.product = this.productController.item;
    }
    updateNumberOfStars() {
        const value = ProductTemplatesHelpers.getProductProperty(this.product, this.field);
        try {
            this.numberOfStars = computeNumberOfStars(value, this.field);
        } catch (error) {
            this.error = error instanceof Error ? error : new Error(`${error}`);
            this.numberOfStars = null;
        }
    }
    updateRatingDetailsValue() {
        if (void 0 === this.ratingDetailsField) {
            this.ratingDetails = null;
            return;
        }
        const value = ProductTemplatesHelpers.getProductProperty(this.product, this.ratingDetailsField);
        if (null === value) {
            this.ratingDetails = null;
            return;
        }
        this.ratingDetails = value;
    }
    updateStates() {
        if (!this.product) return;
        this.updateNumberOfStars();
        this.updateRatingDetailsValue();
    }
    render() {
        if (!this.product && this.productController.item) this.product = this.productController.item;
        this.updateStates();
        return html`
      ${when(null !== this.numberOfStars, ()=>html`
          <div class="flex items-center align-middle">
            ${renderRating({
                props: {
                    i18n: this.bindings.i18n,
                    icon: this.icon,
                    numberOfTotalIcons: this.maxValueInIndex,
                    numberOfActiveIcons: this.numberOfStars,
                    iconSize: 0.875
                }
            })}
            ${when(null !== this.ratingDetails, ()=>html`
                <span class="text-neutral-dark rating-details pl-1">
                  (${this.ratingDetails})
                </span>
              `)}
          </div>
        `)}
    `;
    }
    constructor(...args){
        super(...args), this.productController = createProductContextController(this), this.field = 'ec_rating', this.maxValueInIndex = 5, this.icon = star, this.numberOfStars = null, this.ratingDetails = null;
    }
}
AtomicProductRating.styles = atomic_rating_tw_css;
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicProductRating.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicProductRating.prototype, "error", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Product ? Object : Product)
], AtomicProductRating.prototype, "product", void 0);
_ts_decorate([
    property({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductRating.prototype, "field", void 0);
_ts_decorate([
    property({
        reflect: true,
        attribute: 'rating-details-field'
    }),
    _ts_metadata("design:type", String)
], AtomicProductRating.prototype, "ratingDetailsField", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'max-value-in-index'
    })
], AtomicProductRating.prototype, "maxValueInIndex", void 0);
_ts_decorate([
    property({
        reflect: true
    })
], AtomicProductRating.prototype, "icon", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Object)
], AtomicProductRating.prototype, "numberOfStars", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Object)
], AtomicProductRating.prototype, "ratingDetails", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicProductRating.prototype, "render", null);
AtomicProductRating = _ts_decorate([
    customElement('atomic-product-rating'),
    bindings()
], AtomicProductRating);
export { AtomicProductRating };
