import { isNullOrUndefined } from "@coveo/bueno";
import { ResultTemplatesHelpers } from "@coveo/headless";
import { LitElement, html, nothing } 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 { 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 { mapProperty } from "../../../utils/props-utils.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 AtomicResultLocalizedText extends LightDomMixin(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;
        }
    }
    render() {
        return html`${when(this.result, ()=>{
            const localizedText = this.bindings.i18n.t(this.localeKey, {
                ...this.parseFieldValues(),
                ...this.parseFieldCount()
            });
            return localizedText;
        }, ()=>nothing)}`;
    }
    parseFieldValues() {
        const ret = {};
        if (!this.field || 0 === Object.keys(this.field).length) return ret;
        Object.entries(this.field).forEach(([fieldName, i18nParameter])=>{
            const fieldValueRaw = ResultTemplatesHelpers.getResultProperty(this.result, fieldName);
            if (!isNullOrUndefined(fieldValueRaw)) ret[i18nParameter] = fieldValueRaw;
        });
        return ret;
    }
    parseFieldCount() {
        if (!this.fieldCount) return {};
        return {
            count: ResultTemplatesHelpers.getResultProperty(this.result, this.fieldCount) ?? 1
        };
    }
    constructor(...args){
        super(...args), this.resultController = createResultContextController(this);
    }
}
_ts_decorate([
    property({
        type: String,
        attribute: 'locale-key'
    }),
    _ts_metadata("design:type", String)
], AtomicResultLocalizedText.prototype, "localeKey", void 0);
_ts_decorate([
    mapProperty({
        attributePrefix: 'field'
    }),
    _ts_metadata("design:type", "u" < typeof Record ? Object : Record)
], AtomicResultLocalizedText.prototype, "field", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'field-count'
    }),
    _ts_metadata("design:type", String)
], AtomicResultLocalizedText.prototype, "fieldCount", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Result ? Object : Result)
], AtomicResultLocalizedText.prototype, "result", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultLocalizedText.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultLocalizedText.prototype, "error", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultLocalizedText.prototype, "render", null);
AtomicResultLocalizedText = _ts_decorate([
    customElement('atomic-result-localized-text'),
    bindings()
], AtomicResultLocalizedText);
export { AtomicResultLocalizedText };
