import { buildProductListing, buildSearch } from "@coveo/headless/commerce";
import { LitElement, css, html } from "lit";
import { customElement, state as decorators_js_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 { AriaLiveRegionController } from "../../../utils/accessibility-utils.js";
import { renderQuerySummaryContainer } from "../../common/query-summary/container.js";
import { renderQuerySummaryGuard } from "../../common/query-summary/guard.js";
import { getProductQuerySummaryI18nParameters } 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 AtomicCommerceQuerySummary extends LitElement {
    initialize() {
        const controller = 'product-listing' === this.bindings.interfaceElement.type ? buildProductListing(this.bindings.engine) : buildSearch(this.bindings.engine);
        this.listingOrSearchSummary = controller.summary();
    }
    render() {
        const { firstProduct, firstRequestExecuted, lastProduct, totalNumberOfProducts, hasProducts, hasError, isLoading } = this.listingOrSearchSummaryState;
        const { i18nKey, highlights, ariaLiveMessage } = getProductQuerySummaryI18nParameters({
            first: firstProduct,
            last: lastProduct,
            query: this.isSearch(this.listingOrSearchSummaryState) ? this.listingOrSearchSummaryState.query : '',
            total: totalNumberOfProducts,
            i18n: this.bindings.i18n,
            isLoading
        });
        this.ariaMessage.message = ariaLiveMessage;
        return html`${renderQuerySummaryGuard({
            props: {
                firstSearchExecuted: firstRequestExecuted,
                hasResults: hasProducts,
                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: lastProduct
        })}`)}
    `)}`;
    }
    isSearch(state) {
        return 'query' in state;
    }
    constructor(...args){
        super(...args), this.ariaMessage = new AriaLiveRegionController(this, 'query-summary');
    }
}
AtomicCommerceQuerySummary.styles = [
    css`:host{overflow:hidden}`
];
_ts_decorate([
    decorators_js_state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceQuerySummary.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('listingOrSearchSummary'),
    decorators_js_state(),
    _ts_metadata("design:type", Object)
], AtomicCommerceQuerySummary.prototype, "listingOrSearchSummaryState", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicCommerceQuerySummary.prototype, "render", null);
AtomicCommerceQuerySummary = _ts_decorate([
    customElement('atomic-commerce-query-summary'),
    bindings(),
    withTailwindStyles
], AtomicCommerceQuerySummary);
export { AtomicCommerceQuerySummary };
