import { buildBreadcrumbManager, buildQuerySummary } from "@coveo/headless";
import { LitElement, css, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { collapseFacetsAfter } from "../../common/facets/facet-common.js";
import { popoverClass } from "../../common/facets/popover/popover-type.js";
import { isRefineModalFacet } from "../../common/interface/store.js";
import { renderRefineModalBody } from "../../common/refine-modal/body.js";
import { renderRefineModalFiltersClearButton, renderRefineModalFiltersSection } from "../../common/refine-modal/filters.js";
import { renderRefineModal } from "../../common/refine-modal/modal.js";
import { booleanConverter } from "../../../converters/boolean-converter.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 { watch } from "../../../decorators/watch.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { sortByDocumentPosition } from "../../../utils/utils.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 AtomicIpxRefineModal extends LitElement {
    initialize() {
        this.breadcrumbManager = buildBreadcrumbManager(this.bindings.engine);
        this.querySummary = buildQuerySummary(this.bindings.engine);
        this.watchEnabled();
    }
    watchEnabled() {
        if (this.isOpen) this.createFacetSlotIfAbsent();
    }
    createFacetSlotIfAbsent() {
        if (this.querySelector('div[slot="facets"]')) return;
        this.appendChild(this.createFacetSlot());
    }
    createFacetSlot() {
        const divSlot = document.createElement('div');
        divSlot.setAttribute('slot', 'facets');
        this.addFacetColumnStyling(divSlot);
        const facets = this.bindings.store.getFacetElements().sort(sortByDocumentPosition);
        const clonedFacets = this.cloneFacets(facets);
        collapseFacetsAfter(clonedFacets, this.collapseFacetsAfter);
        divSlot.append(...clonedFacets);
        return divSlot;
    }
    cloneFacets(facets) {
        return facets.map((facet, i)=>{
            const clone = facet.cloneNode(true);
            clone.classList.remove(popoverClass);
            clone.setAttribute(isRefineModalFacet, '');
            clone.isCollapsed = -1 === this.collapseFacetsAfter ? false : i + 1 > this.collapseFacetsAfter;
            return clone;
        });
    }
    addFacetColumnStyling(el) {
        el.style.display = 'flex';
        el.style.flexDirection = 'column';
        el.style.gap = 'var(--atomic-refine-modal-facet-margin, 20px)';
    }
    renderFilters() {
        const hasFacetElements = this.bindings.store.getFacetElements().length > 0;
        if (!hasFacetElements) return nothing;
        const { i18n } = this.bindings;
        return renderRefineModalFiltersSection({
            props: {
                i18n,
                withFacets: hasFacetElements,
                withAutomaticFacets: false
            }
        })(this.breadcrumbManagerState.hasBreadcrumbs ? renderRefineModalFiltersClearButton({
            props: {
                i18n,
                onClick: ()=>this.breadcrumbManager.deselectAll()
            }
        }) : nothing);
    }
    render() {
        return html`${renderRefineModal({
            props: {
                i18n: this.bindings.i18n,
                i18nFooterButtonTextKey: 'view-results',
                host: this,
                isOpen: this.isOpen,
                onClose: ()=>{
                    this.isOpen = false;
                },
                title: this.bindings.i18n.t('filters'),
                numberOfItems: this.querySummaryState.total,
                openButton: this.openButton,
                boundary: 'element',
                scope: this.bindings.interfaceElement
            }
        })(renderRefineModalBody(this.bindings.i18n)(html`${this.renderFilters()}`))}`;
    }
    constructor(...args){
        super(...args), this.isOpen = false, this.collapseFacetsAfter = 0;
    }
}
AtomicIpxRefineModal.styles = [
    css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */:host::part(container){border-radius:.25rem;max-height:calc(100vh - 4.25rem)}`
];
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicIpxRefineModal.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicIpxRefineModal.prototype, "error", void 0);
_ts_decorate([
    property({
        attribute: 'open-button',
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof HTMLElement ? Object : HTMLElement)
], AtomicIpxRefineModal.prototype, "openButton", void 0);
_ts_decorate([
    property({
        type: Boolean,
        converter: booleanConverter,
        reflect: true,
        attribute: 'is-open'
    })
], AtomicIpxRefineModal.prototype, "isOpen", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'collapse-facets-after'
    })
], AtomicIpxRefineModal.prototype, "collapseFacetsAfter", void 0);
_ts_decorate([
    bindStateToController('breadcrumbManager'),
    state(),
    _ts_metadata("design:type", "u" < typeof BreadcrumbManagerState ? Object : BreadcrumbManagerState)
], AtomicIpxRefineModal.prototype, "breadcrumbManagerState", void 0);
_ts_decorate([
    bindStateToController('querySummary'),
    state(),
    _ts_metadata("design:type", "u" < typeof QuerySummaryState ? Object : QuerySummaryState)
], AtomicIpxRefineModal.prototype, "querySummaryState", void 0);
_ts_decorate([
    watch('isOpen'),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicIpxRefineModal.prototype, "watchEnabled", null);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicIpxRefineModal.prototype, "render", null);
AtomicIpxRefineModal = _ts_decorate([
    customElement('atomic-ipx-refine-modal'),
    bindings(),
    withTailwindStyles
], AtomicIpxRefineModal);
export { AtomicIpxRefineModal };
