import { buildSearchStatus } from "@coveo/headless";
import { LitElement, html } from "lit";
import { customElement, property, 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 { renderRefineToggleButton } from "../../common/refine-modal/button.js";
import { refineToggleGuard } from "../../common/refine-modal/guard.js";
import "../atomic-refine-modal/atomic-refine-modal.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 AtomicRefineToggle extends LitElement {
    initialize() {
        this.searchStatus = buildSearchStatus(this.bindings.engine);
    }
    loadModal() {
        if (this.modalRef) return;
        this.modalRef = document.createElement('atomic-refine-modal');
        this.insertAdjacentElement('beforebegin', this.modalRef);
        this.modalRef.openButton = this.buttonRef;
        this.modalRef.collapseFacetsAfter = this.collapseFacetsAfter;
    }
    enableModal() {
        if (this.modalRef) this.modalRef.isOpen = true;
    }
    render() {
        return html`${refineToggleGuard({
            firstRequestExecuted: this.searchStatusState.firstSearchExecuted,
            hasError: this.searchStatusState.hasError,
            hasItems: this.searchStatusState.hasResults
        }, ()=>html`${renderRefineToggleButton({
                props: {
                    i18n: this.bindings.i18n,
                    onClick: ()=>this.enableModal(),
                    refCallback: (button)=>{
                        if (!button) return;
                        this.buttonRef = button;
                        this.loadModal();
                    }
                }
            })}`)}`;
    }
    constructor(...args){
        super(...args), this.collapseFacetsAfter = 0;
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicRefineToggle.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicRefineToggle.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('searchStatus'),
    state(),
    _ts_metadata("design:type", "u" < typeof SearchStatusState ? Object : SearchStatusState)
], AtomicRefineToggle.prototype, "searchStatusState", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'collapse-facets-after'
    })
], AtomicRefineToggle.prototype, "collapseFacetsAfter", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicRefineToggle.prototype, "render", null);
AtomicRefineToggle = _ts_decorate([
    customElement('atomic-refine-toggle'),
    bindings(),
    withTailwindStyles
], AtomicRefineToggle);
export { AtomicRefineToggle };
