import { Schema, StringValue } from "@coveo/bueno";
import { ResultTemplatesHelpers } from "@coveo/headless";
import dayjs from "dayjs";
import calendar from "dayjs/plugin/calendar";
import updateLocale from "dayjs/plugin/updateLocale";
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 { 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 { parseDate } from "../../../utils/date-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);
}
dayjs.extend(calendar);
dayjs.extend(updateLocale);
class AtomicResultDate 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.renderDate())}`;
    }
    renderDate() {
        const dateToRender = this.getDateToRender();
        if (null === dateToRender) {
            this.remove();
            return nothing;
        }
        return html`${dateToRender}`;
    }
    getDateToRender() {
        const value = ResultTemplatesHelpers.getResultProperty(this.result, this.field);
        if (null === value) return null;
        const parsedValue = parseDate(value);
        if (!parsedValue.isValid()) {
            this.error = new Error(`Field "${this.field}" does not contain a valid date.`);
            return null;
        }
        if (this.relativeTime) {
            dayjs.updateLocale(this.bindings.interfaceElement.language, {
                calendar: {
                    sameDay: this.bindings.i18n.t('calendar-same-day'),
                    nextDay: this.bindings.i18n.t('calendar-next-day'),
                    nextWeek: this.bindings.i18n.t('calendar-next-week'),
                    lastDay: this.bindings.i18n.t('calendar-last-day'),
                    lastWeek: this.bindings.i18n.t('calendar-last-week'),
                    sameElse: this.format
                }
            });
            return parsedValue.calendar();
        }
        return parsedValue.format(this.format);
    }
    constructor(){
        super(), this.field = 'date', this.format = 'D/M/YYYY', this.relativeTime = false, this.resultController = createResultContextController(this);
        new ValidatePropsController(this, ()=>({
                field: this.field,
                format: this.format
            }), AtomicResultDate.propsSchema, false);
    }
}
AtomicResultDate.propsSchema = new Schema({
    field: new StringValue({
        required: true,
        emptyAllowed: false
    }),
    format: new StringValue({
        required: false,
        emptyAllowed: false
    })
});
_ts_decorate([
    property({
        type: String,
        reflect: true
    })
], AtomicResultDate.prototype, "field", void 0);
_ts_decorate([
    property({
        type: String,
        reflect: true
    })
], AtomicResultDate.prototype, "format", void 0);
_ts_decorate([
    property({
        type: Boolean,
        reflect: true,
        attribute: 'relative-time'
    })
], AtomicResultDate.prototype, "relativeTime", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultDate.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultDate.prototype, "error", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Result ? Object : Result)
], AtomicResultDate.prototype, "result", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultDate.prototype, "render", null);
AtomicResultDate = _ts_decorate([
    customElement('atomic-result-date'),
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicResultDate);
export { AtomicResultDate };
