import { buildProductListing, buildSearch } from "@coveo/headless/commerce";
import { LitElement, html } from "lit";
import { customElement, 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-commerce-refine-modal/atomic-commerce-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 AtomicCommerceRefineToggle extends LitElement {
    initialize() {
        const searchOrListing = 'search' === this.bindings.interfaceElement.type ? buildSearch(this.bindings.engine) : buildProductListing(this.bindings.engine);
        this.summary = searchOrListing.summary();
    }
    loadModal() {
        if (this.modalRef) return;
        this.modalRef = document.createElement('atomic-commerce-refine-modal');
        this.insertAdjacentElement('beforebegin', this.modalRef);
        this.modalRef.openButton = this.buttonRef;
    }
    enableModal() {
        if (this.modalRef) this.modalRef.isOpen = true;
    }
    render() {
        return html`${refineToggleGuard({
            firstRequestExecuted: this.summaryState.firstRequestExecuted,
            hasError: this.summaryState.hasError,
            hasItems: this.summaryState.hasProducts
        }, ()=>html`${renderRefineToggleButton({
                props: {
                    i18n: this.bindings.i18n,
                    onClick: ()=>this.enableModal(),
                    refCallback: (button)=>{
                        if (!button) return;
                        this.buttonRef = button;
                        this.loadModal();
                    }
                }
            })}`)}`;
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceRefineToggle.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceRefineToggle.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('summary'),
    state(),
    _ts_metadata("design:type", Object)
], AtomicCommerceRefineToggle.prototype, "summaryState", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicCommerceRefineToggle.prototype, "render", null);
AtomicCommerceRefineToggle = _ts_decorate([
    customElement('atomic-commerce-refine-toggle'),
    bindings(),
    withTailwindStyles
], AtomicCommerceRefineToggle);
export { AtomicCommerceRefineToggle };
