import { buildQuerySummary } from "@coveo/headless";
import { LitElement, css, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { bindStateToController } from "../../../decorators/bind-state.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 { localizedString } from "../../../directives/localized-string.js";
import { InitializeBindingsMixin } from "../../../mixins/bindings-mixin.js";
import { AriaLiveRegionController } from "../../../utils/accessibility-utils.js";
import { renderQuerySummaryContainer } from "../../common/query-summary/container.js";
import { renderQuerySummaryGuard } from "../../common/query-summary/guard.js";
import { getQuerySummaryI18nParameters } from "../../common/query-summary/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 AtomicQuerySummary extends InitializeBindingsMixin(LitElement) {
    initialize() {
        this.querySummary = buildQuerySummary(this.bindings.engine);
    }
    render() {
        const { firstSearchExecuted, hasResults, hasError, total, firstResult, lastResult, query, durationInSeconds, isLoading } = this.querySummaryState;
        const { i18nKey, highlights, ariaLiveMessage } = getQuerySummaryI18nParameters({
            first: firstResult,
            last: lastResult,
            query,
            total,
            i18n: this.bindings.i18n,
            isLoading
        });
        this.ariaMessage.message = ariaLiveMessage;
        return html`${renderQuerySummaryGuard({
            props: {
                firstSearchExecuted,
                hasResults,
                hasError
            }
        })(html`
      ${renderQuerySummaryContainer({
            props: {}
        })(html`
        ${localizedString({
            i18n: this.bindings.i18n,
            key: i18nKey,
            params: {
                first: html`${highlights.first}`,
                last: html`${highlights.last}`,
                total: html`${highlights.total}`,
                query: html`${highlights.query}`
            },
            count: total
        })}
        <span class="hidden" part="duration">
          &nbsp;
          ${localizedString({
            i18n: this.bindings.i18n,
            key: 'in-seconds',
            params: {
                count: durationInSeconds.toLocaleString(this.bindings.i18n.language)
            }
        })}
        </span>
      `)}
    `)}`;
    }
    constructor(...args){
        super(...args), this.ariaMessage = new AriaLiveRegionController(this, 'query-summary');
    }
}
AtomicQuerySummary.styles = [
    css`:host{overflow:hidden}`
];
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicQuerySummary.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicQuerySummary.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('querySummary'),
    state(),
    _ts_metadata("design:type", "u" < typeof QuerySummaryState ? Object : QuerySummaryState)
], AtomicQuerySummary.prototype, "querySummaryState", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicQuerySummary.prototype, "render", null);
AtomicQuerySummary = _ts_decorate([
    customElement('atomic-query-summary'),
    bindings(),
    withTailwindStyles
], AtomicQuerySummary);
export { AtomicQuerySummary };
