import { buildQuerySummary, buildResultList } from "@coveo/headless";
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 AtomicLoadMoreResults extends LitElement {
    initialize() {
        this.querySummary = buildQuerySummary(this.bindings.engine);
        this.resultList = buildResultList(this.bindings.engine, {
            options: {
                fieldsToInclude: []
            }
        });
        createAppLoadedListener(this.bindings.store, (isAppLoaded)=>{
            this.isAppLoaded = isAppLoaded;
        });
    }
    render() {
        return html`${when(this.shouldRender, ()=>renderLoadMoreContainer()(html`
        ${renderLoadMoreSummary({
                props: {
                    from: this.querySummaryState.lastResult,
                    to: this.querySummaryState.total,
                    i18n: this.bindings.i18n,
                    label: 'showing-results-of-load-more'
                }
            })}
        ${renderLoadMoreProgressBar({
                props: {
                    from: this.querySummaryState.lastResult,
                    to: this.querySummaryState.total
                }
            })}
        ${renderLoadMoreButton({
                props: {
                    i18n: this.bindings.i18n,
                    label: 'load-more-results',
                    moreAvailable: this.resultListState.moreResultsAvailable,
                    onClick: ()=>this.onClick()
                }
            })}
      `))}`;
    }
    async onClick() {
        document.activeElement?.blur();
        this.resultList.fetchMoreResults();
    }
    get shouldRender() {
        return this.isAppLoaded && this.querySummaryState.hasResults;
    }
    constructor(...args){
        super(...args), this.isAppLoaded = false;
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicLoadMoreResults.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicLoadMoreResults.prototype, "error", void 0);
_ts_decorate([
    state()
], AtomicLoadMoreResults.prototype, "isAppLoaded", void 0);
_ts_decorate([
    bindStateToController('querySummary'),
    state(),
    _ts_metadata("design:type", "u" < typeof QuerySummaryState ? Object : QuerySummaryState)
], AtomicLoadMoreResults.prototype, "querySummaryState", void 0);
_ts_decorate([
    bindStateToController('resultList'),
    state(),
    _ts_metadata("design:type", "u" < typeof ResultListState ? Object : ResultListState)
], AtomicLoadMoreResults.prototype, "resultListState", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicLoadMoreResults.prototype, "render", null);
AtomicLoadMoreResults = _ts_decorate([
    customElement('atomic-load-more-results'),
    bindings(),
    withTailwindStyles
], AtomicLoadMoreResults);
export { AtomicLoadMoreResults };
