import { buildProductListing, buildSearch } from "@coveo/headless/commerce";
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { createAppLoadedListener } from "../../common/interface/store.js";
import { renderLoadMoreButton } from "../../common/load-more/button.js";
import { renderLoadMoreContainer } from "../../common/load-more/container.js";
import { renderLoadMoreProgressBar } from "../../common/load-more/progress-bar.js";
import { renderLoadMoreSummary } from "../../common/load-more/summary.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";
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 AtomicCommerceLoadMoreProducts extends LitElement {
    initialize() {
        if ('product-listing' === this.bindings.interfaceElement.type) this.listingOrSearch = buildProductListing(this.bindings.engine);
        else this.listingOrSearch = buildSearch(this.bindings.engine);
        this.pagination = this.listingOrSearch.pagination();
        createAppLoadedListener(this.bindings.store, (isAppLoaded)=>{
            this.isAppLoaded = isAppLoaded;
        });
    }
    render() {
        return html`${when(this.shouldRender, ()=>renderLoadMoreContainer()(html`
        ${renderLoadMoreSummary({
                props: {
                    from: this.lastProduct,
                    to: this.paginationState.totalEntries,
                    i18n: this.bindings.i18n,
                    label: 'showing-products-of-load-more'
                }
            })}
        ${renderLoadMoreProgressBar({
                props: {
                    from: this.lastProduct,
                    to: this.paginationState.totalEntries
                }
            })}
        ${renderLoadMoreButton({
                props: {
                    i18n: this.bindings.i18n,
                    label: 'load-more-products',
                    moreAvailable: this.lastProduct < this.paginationState.totalEntries,
                    onClick: ()=>this.onClick()
                }
            })}
      `))}`;
    }
    get lastProduct() {
        return this.productListingOrSearchState.products.length;
    }
    async onClick() {
        this.bindings.store.state.resultList?.focusOnNextNewResult();
        this.pagination.fetchMoreProducts();
    }
    get shouldRender() {
        return this.isAppLoaded && this.paginationState.totalEntries > 0;
    }
    constructor(...args){
        super(...args), this.isAppLoaded = false;
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceLoadMoreProducts.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceLoadMoreProducts.prototype, "error", void 0);
_ts_decorate([
    state()
], AtomicCommerceLoadMoreProducts.prototype, "isAppLoaded", void 0);
_ts_decorate([
    bindStateToController('pagination'),
    state(),
    _ts_metadata("design:type", "u" < typeof PaginationState ? Object : PaginationState)
], AtomicCommerceLoadMoreProducts.prototype, "paginationState", void 0);
_ts_decorate([
    bindStateToController('listingOrSearch'),
    state(),
    _ts_metadata("design:type", Object)
], AtomicCommerceLoadMoreProducts.prototype, "productListingOrSearchState", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicCommerceLoadMoreProducts.prototype, "render", null);
AtomicCommerceLoadMoreProducts = _ts_decorate([
    customElement('atomic-commerce-load-more-products'),
    bindings(),
    withTailwindStyles
], AtomicCommerceLoadMoreProducts);
export { AtomicCommerceLoadMoreProducts };
