import { buildProductListing, buildSearch } from "@coveo/headless/commerce";
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { renderFieldsetGroup } from "../../common/fieldset-group.js";
import { createAppLoadedListener } from "../../common/interface/store.js";
import { renderChoices } from "../../common/items-per-page/choices.js";
import { renderLabel } from "../../common/items-per-page/label.js";
import { convertChoicesToNumbers, validateInitialChoice } from "../../common/items-per-page/validate.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 { buildCustomEvent } from "../../../utils/event-utils.js";
import { randomID } from "../../../utils/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 AtomicCommerceProductsPerPage extends LitElement {
    initialize() {
        this.choices = convertChoicesToNumbers(this.choicesDisplayed);
        this.initialChoice = this.initialChoice ?? this.choices[0];
        validateInitialChoice(this.initialChoice, this.choices);
        const controller = 'search' === this.bindings.interfaceElement.type ? buildSearch(this.bindings.engine) : buildProductListing(this.bindings.engine);
        this.summary = controller.summary();
        this.pagination = controller.pagination(this.initialChoice ? {
            options: {
                pageSize: this.initialChoice
            }
        } : {});
        createAppLoadedListener(this.bindings.store, (isAppLoaded)=>{
            this.isAppLoaded = isAppLoaded;
        });
    }
    scrollToTopEvent() {
        this.dispatchEvent(buildCustomEvent('atomic/scrollToTop'));
    }
    get label() {
        return this.bindings.i18n.t('products-per-page');
    }
    render() {
        return html`
      ${when(!this.summaryState.hasError && this.summaryState.hasProducts && this.isAppLoaded, ()=>html`
          <div class="flex items-center">
            ${renderLabel()(html`${this.label}`)}
            ${renderFieldsetGroup({
                props: {
                    label: this.label
                }
            })(renderChoices({
                props: {
                    label: this.label,
                    groupName: this.radioGroupName,
                    pageSize: this.paginationState.pageSize,
                    choices: this.choices,
                    lang: this.bindings.i18n.language,
                    scrollToTopEvent: ()=>this.scrollToTopEvent(),
                    setItemSize: (size)=>this.pagination.setPageSize(size),
                    focusOnFirstResultAfterNextSearch: ()=>this.bindings.store.state.resultList?.focusOnFirstResultAfterNextSearch(),
                    focusOnNextNewResult: ()=>this.bindings.store.state.resultList?.focusOnNextNewResult()
                }
            }))}
          </div>
        `)}
    `;
    }
    constructor(...args){
        super(...args), this.isAppLoaded = false, this.radioGroupName = randomID('atomic-commerce-products-per-page-'), this.choicesDisplayed = '10,25,50,100';
    }
}
_ts_decorate([
    bindStateToController('pagination'),
    state(),
    _ts_metadata("design:type", "u" < typeof PaginationState ? Object : PaginationState)
], AtomicCommerceProductsPerPage.prototype, "paginationState", void 0);
_ts_decorate([
    bindStateToController('summary'),
    state(),
    _ts_metadata("design:type", Object)
], AtomicCommerceProductsPerPage.prototype, "summaryState", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceProductsPerPage.prototype, "error", void 0);
_ts_decorate([
    state()
], AtomicCommerceProductsPerPage.prototype, "isAppLoaded", void 0);
_ts_decorate([
    property({
        reflect: true,
        attribute: 'choices-displayed'
    })
], AtomicCommerceProductsPerPage.prototype, "choicesDisplayed", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'initial-choice'
    }),
    _ts_metadata("design:type", Number)
], AtomicCommerceProductsPerPage.prototype, "initialChoice", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicCommerceProductsPerPage.prototype, "render", null);
AtomicCommerceProductsPerPage = _ts_decorate([
    customElement('atomic-commerce-products-per-page'),
    bindings(),
    withTailwindStyles
], AtomicCommerceProductsPerPage);
export { AtomicCommerceProductsPerPage };
