import { NumberValue, Schema, StringValue } from "@coveo/bueno";
import { ResultTemplatesHelpers } from "@coveo/headless";
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import atomic_rating_tw_css from "../../common/atomic-rating/atomic-rating.tw.css.js";
import { renderRating } from "../../common/atomic-rating/rating.js";
import { computeNumberOfStars } from "../../common/atomic-rating/rating-utils.js";
import { ValidatePropsController } from "../../common/validate-props-controller/validate-props-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 { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { InitializeBindingsMixin } from "../../../mixins/bindings-mixin.js";
import star from "../../../images/star.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 AtomicResultRating extends InitializeBindingsMixin(LitElement) {
    initialize() {
        if (!this.result && this.resultController.item) {
            const item = this.resultController.item;
            if ('result' in item) this.result = item.result;
            else this.result = item;
        }
    }
    updateNumberOfStars() {
        if (!this.result) return;
        const value = ResultTemplatesHelpers.getResultProperty(this.result, this.field);
        try {
            this.numberOfStars = computeNumberOfStars(value, this.field);
        } catch (error) {
            this.error = error instanceof Error ? error : new Error(`${error}`);
            this.numberOfStars = null;
        }
    }
    render() {
        this.updateNumberOfStars();
        return html`
      ${when(null !== this.numberOfStars, ()=>renderRating({
                props: {
                    i18n: this.bindings.i18n,
                    icon: this.icon,
                    numberOfTotalIcons: this.maxValueInIndex,
                    numberOfActiveIcons: this.numberOfStars,
                    iconSize: 0.875
                }
            }))}
    `;
    }
    constructor(){
        super(), this.maxValueInIndex = 5, this.icon = star, this.numberOfStars = null, this.resultController = createResultContextController(this);
        new ValidatePropsController(this, ()=>({
                field: this.field,
                maxValueInIndex: this.maxValueInIndex
            }), AtomicResultRating.propsSchema, false);
    }
}
AtomicResultRating.styles = atomic_rating_tw_css;
AtomicResultRating.propsSchema = new Schema({
    field: new StringValue({
        required: true,
        emptyAllowed: false
    }),
    maxValueInIndex: new NumberValue({
        default: 5,
        min: 1,
        required: false
    })
});
_ts_decorate([
    property({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicResultRating.prototype, "field", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'max-value-in-index'
    })
], AtomicResultRating.prototype, "maxValueInIndex", void 0);
_ts_decorate([
    property({
        reflect: true
    })
], AtomicResultRating.prototype, "icon", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultRating.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultRating.prototype, "error", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Result ? Object : Result)
], AtomicResultRating.prototype, "result", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Object)
], AtomicResultRating.prototype, "numberOfStars", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultRating.prototype, "render", null);
AtomicResultRating = _ts_decorate([
    customElement('atomic-result-rating'),
    bindings(),
    withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicResultRating);
export { AtomicResultRating };
