import { Schema, StringValue } from "@coveo/bueno";
import { ResultTemplatesHelpers } from "@coveo/headless";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.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 { InitializeBindingsMixin } from "../../../mixins/bindings-mixin.js";
import { LightDomMixin } from "../../../mixins/light-dom.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);
}
dayjs.extend(duration);
class AtomicResultTimespan extends LightDomMixin(InitializeBindingsMixin(LitElement)) {
    initialize() {
        if (!this.value) {
            this.error = new Error(`No value found for field ${this.field}`);
            return;
        }
        if (isNaN(this.value)) this.error = new Error(`Value ${this.value} for field ${this.field} is not a number`);
    }
    render() {
        const durationValue = dayjs.duration(this.value, this.unit);
        if (this.format) return html`${durationValue.format(this.format)}`;
        if (durationValue.asYears() >= 1) return html`${this.bindings.i18n.t('approx_year', {
            count: Math.round(durationValue.asYears())
        })}`;
        if (durationValue.asMonths() >= 1) return html`${this.bindings.i18n.t('approx_month', {
            count: Math.round(durationValue.asMonths())
        })}`;
        if (durationValue.asDays() >= 1) return html`${this.bindings.i18n.t('approx_day', {
            count: Math.round(durationValue.asDays())
        })}`;
        return html`${durationValue.format('HH:mm:ss')}`;
    }
    get value() {
        return ResultTemplatesHelpers.getResultProperty(this.result, this.field);
    }
    get result() {
        return this.resultContext.item;
    }
    constructor(){
        super(), this.unit = 'ms', this.resultContext = createResultContextController(this);
        new ValidatePropsController(this, ()=>({
                field: this.field,
                unit: this.unit,
                format: this.format
            }), AtomicResultTimespan.propsSchema);
    }
}
AtomicResultTimespan.propsSchema = new Schema({
    field: new StringValue({
        required: true,
        emptyAllowed: false
    }),
    unit: new StringValue({
        constrainTo: [
            'milliseconds',
            'ms',
            'seconds',
            's',
            'minutes',
            'm',
            'hours',
            'h',
            'days',
            'd',
            'weeks',
            'w',
            'months',
            'M',
            'years',
            'y'
        ]
    }),
    format: new StringValue({
        required: false,
        emptyAllowed: false
    })
});
_ts_decorate([
    property({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicResultTimespan.prototype, "field", void 0);
_ts_decorate([
    property({
        reflect: true
    })
], AtomicResultTimespan.prototype, "unit", void 0);
_ts_decorate([
    property({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicResultTimespan.prototype, "format", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultTimespan.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultTimespan.prototype, "error", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultTimespan.prototype, "render", null);
AtomicResultTimespan = _ts_decorate([
    customElement('atomic-result-timespan'),
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicResultTimespan);
export { AtomicResultTimespan };
