import { BooleanValue, Schema, StringValue } from "@coveo/bueno";
import { LitElement, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.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 { booleanConverter } from "../../../converters/boolean-converter.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 { getStringValueFromResultOrNull } from "../../../utils/result-utils.js";
import "../atomic-html/atomic-html.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 AtomicResultHtml 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 && this.field, ()=>this.renderResultHtml(), ()=>nothing)}
    `;
    }
    renderResultHtml() {
        const resultValue = getStringValueFromResultOrNull(this.result, this.field);
        if (!resultValue) {
            this.remove();
            return nothing;
        }
        return html`
      <atomic-html
        value=${resultValue}
        ?sanitize=${this.sanitize}
      ></atomic-html>
    `;
    }
    constructor(){
        super(), this.sanitize = true, this.resultController = createResultContextController(this);
        new ValidatePropsController(this, ()=>({
                field: this.field,
                sanitize: this.sanitize
            }), AtomicResultHtml.propsSchema);
    }
}
AtomicResultHtml.propsSchema = new Schema({
    field: new StringValue({
        required: true,
        emptyAllowed: false
    }),
    sanitize: new BooleanValue()
});
_ts_decorate([
    property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicResultHtml.prototype, "field", void 0);
_ts_decorate([
    property({
        type: Boolean,
        reflect: true,
        converter: booleanConverter
    })
], AtomicResultHtml.prototype, "sanitize", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Result ? Object : Result)
], AtomicResultHtml.prototype, "result", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultHtml.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultHtml.prototype, "error", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultHtml.prototype, "render", null);
AtomicResultHtml = _ts_decorate([
    customElement('atomic-result-html'),
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicResultHtml);
export { AtomicResultHtml };
