import { buildBreadcrumbManager, buildSearchStatus } from "@coveo/headless/insight";
import { LitElement, html } from "lit";
import { customElement, 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 { refineToggleGuard } from "../../common/refine-modal/guard.js";
import "../atomic-insight-refine-modal/atomic-insight-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 AtomicInsightRefineToggle extends LitElement {
    get numberOfBreadcrumbs() {
        return [
            ...this.breadcrumbManagerState.facetBreadcrumbs,
            ...this.breadcrumbManagerState.categoryFacetBreadcrumbs,
            ...this.breadcrumbManagerState.numericFacetBreadcrumbs,
            ...this.breadcrumbManagerState.dateFacetBreadcrumbs,
            ...this.breadcrumbManagerState.staticFilterBreadcrumbs
        ].length;
    }
    initialize() {
        this.searchStatus = buildSearchStatus(this.bindings.engine);
        this.breadcrumbManager = buildBreadcrumbManager(this.bindings.engine);
    }
    loadModal() {
        if (this.modalRef) return;
        this.modalRef = document.createElement('atomic-insight-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.searchStatusState.firstSearchExecuted,
            hasError: this.searchStatusState.hasError,
            hasItems: this.searchStatusState.hasResults || this.numberOfBreadcrumbs > 0
        }, ()=>html`${renderIconButton({
                props: {
                    partPrefix: 'insight-refine-toggle',
                    style: 'outline-neutral',
                    icon: filter,
                    ariaLabel: this.bindings.i18n.t('filters'),
                    title: this.bindings.i18n.t('filters'),
                    disabled: !this.searchStatusState.hasResults && 0 === this.numberOfBreadcrumbs,
                    onClick: ()=>this.enableModal(),
                    buttonRef: (button)=>{
                        if (!button) return;
                        this.buttonRef = button;
                        this.loadModal();
                    },
                    badge: this.breadcrumbManagerState.hasBreadcrumbs ? html`${this.numberOfBreadcrumbs}` : void 0
                }
            })}`)}`;
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightRefineToggle.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightRefineToggle.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('searchStatus'),
    state(),
    _ts_metadata("design:type", "u" < typeof SearchStatusState ? Object : SearchStatusState)
], AtomicInsightRefineToggle.prototype, "searchStatusState", void 0);
_ts_decorate([
    bindStateToController('breadcrumbManager'),
    state(),
    _ts_metadata("design:type", "u" < typeof BreadcrumbManagerState ? Object : BreadcrumbManagerState)
], AtomicInsightRefineToggle.prototype, "breadcrumbManagerState", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightRefineToggle.prototype, "render", null);
AtomicInsightRefineToggle = _ts_decorate([
    customElement('atomic-insight-refine-toggle'),
    bindings(),
    withTailwindStyles
], AtomicInsightRefineToggle);
export { AtomicInsightRefineToggle };
