import { buildBreadcrumbManager, buildSearchStatus } from "@coveo/headless";
import { LitElement, css, html } from "lit";
import { customElement, property, state as decorators_js_state } from "lit/decorators.js";
import { renderIconButton } from "../../common/icon-button.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 filter from "../../../images/filter.js";
import "../atomic-ipx-refine-modal/atomic-ipx-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 AtomicIpxRefineToggle extends LitElement {
    get numberOfBreadcrumbs() {
        const state = this.breadcrumbManagerState;
        return state.facetBreadcrumbs.length + state.categoryFacetBreadcrumbs.length + state.numericFacetBreadcrumbs.length + state.dateFacetBreadcrumbs.length + state.staticFilterBreadcrumbs.length + state.automaticFacetBreadcrumbs.length;
    }
    initialize() {
        this.breadcrumbManager = buildBreadcrumbManager(this.bindings.engine);
        this.searchStatus = buildSearchStatus(this.bindings.engine);
    }
    loadModal() {
        if (this.modalLoaded) return;
        this.modalLoaded = true;
        this.modalRef = document.createElement('atomic-ipx-refine-modal');
        this.insertAdjacentElement('beforebegin', this.modalRef);
        this.modalRef.openButton = this.buttonRef;
        this.modalRef.collapseFacetsAfter = this.collapseFacetsAfter;
    }
    openModal() {
        if (this.modalRef) this.modalRef.isOpen = true;
    }
    handleClick() {
        this.bindings.store.waitUntilAppLoaded(()=>{
            this.openModal();
        });
    }
    render() {
        return renderIconButton({
            props: {
                partPrefix: 'ipx-refine-toggle',
                class: 'icon-button',
                style: 'outline-neutral',
                title: this.bindings.i18n.t('filters'),
                icon: filter,
                disabled: !this.searchStatusState.hasResults && !this.numberOfBreadcrumbs,
                ariaLabel: this.bindings.i18n.t('filters'),
                onClick: ()=>this.handleClick(),
                buttonRef: (button)=>{
                    if (!button) return;
                    this.buttonRef = button;
                    this.loadModal();
                },
                badge: this.breadcrumbManagerState.hasBreadcrumbs ? html`<slot>${this.numberOfBreadcrumbs}</slot>` : void 0
            }
        });
    }
    constructor(...args){
        super(...args), this.collapseFacetsAfter = 0, this.modalLoaded = false;
    }
}
AtomicIpxRefineToggle.styles = css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */@layer properties{@supports (((-webkit-hyphens:none)) and (not (margin-trim:inline))) or ((-moz-orient:inline) and (not (color:rgb(from red r g b)))){*,::backdrop,:after,:before{--tw-leading:initial}}}:host [part=ipx-refine-toggle-button]{height:100%;width:auto}:host [part=ipx-refine-toggle-badge]{font-size:var(--text-xs,.75rem);height:calc(var(--spacing,.25rem)*5);line-height:var(--tw-leading,var(--text-xs--line-height,1.33333));width:calc(var(--spacing,.25rem)*5);--tw-leading:calc(var(--spacing,.25rem)*5);line-height:calc(var(--spacing,.25rem)*5)}:host [part=ipx-refine-toggle-icon]{height:calc(var(--spacing,.25rem)*5);width:calc(var(--spacing,.25rem)*5)}@property --tw-leading{syntax:"*";inherits:false}`;
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'collapse-facets-after'
    })
], AtomicIpxRefineToggle.prototype, "collapseFacetsAfter", void 0);
_ts_decorate([
    decorators_js_state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicIpxRefineToggle.prototype, "bindings", void 0);
_ts_decorate([
    decorators_js_state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicIpxRefineToggle.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('breadcrumbManager'),
    decorators_js_state(),
    _ts_metadata("design:type", "u" < typeof BreadcrumbManagerState ? Object : BreadcrumbManagerState)
], AtomicIpxRefineToggle.prototype, "breadcrumbManagerState", void 0);
_ts_decorate([
    bindStateToController('searchStatus'),
    decorators_js_state(),
    _ts_metadata("design:type", "u" < typeof SearchStatusState ? Object : SearchStatusState)
], AtomicIpxRefineToggle.prototype, "searchStatusState", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicIpxRefineToggle.prototype, "render", null);
AtomicIpxRefineToggle = _ts_decorate([
    customElement('atomic-ipx-refine-toggle'),
    bindings(),
    withTailwindStyles
], AtomicIpxRefineToggle);
export { AtomicIpxRefineToggle };
