import { NumberValue, Schema } from "@coveo/bueno";
import { buildFacetManager } from "@coveo/headless";
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { collapseFacetsAfter, getAutomaticFacetGenerator, getFacetsInChildren, sortFacetVisibility } from "../../common/facets/facet-common.js";
import { ValidatePropsController } from "../../common/validate-props-controller/validate-props-controller.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 { ChildrenUpdateCompleteMixin } from "../../../mixins/children-update-complete-mixin.js";
import { LightDomMixin } from "../../../mixins/light-dom.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 AtomicFacetManager extends ChildrenUpdateCompleteMixin(LightDomMixin(LitElement)) {
    initialize() {
        this.facetManager = buildFacetManager(this.bindings.engine);
        this.bindings.i18n.on('languageChanged', this.sortFacets);
    }
    sortFacetsUsingManager(facets, facetManager) {
        const payload = facets.map((f)=>({
                facetId: f.facetId,
                payload: f
            }));
        return facetManager.sort(payload).map((f)=>f.payload);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.bindings?.i18n.off('languageChanged', this.sortFacets);
    }
    render() {
        return html`<slot></slot>`;
    }
    constructor(){
        super(), this.collapseFacetsAfter = 4, this.sortFacets = async ()=>{
            await this.getUpdateComplete();
            const facets = getFacetsInChildren(this);
            const sortedFacets = this.sortFacetsUsingManager(facets, this.facetManager);
            const { visibleFacets, invisibleFacets } = sortFacetVisibility(sortedFacets, this.bindings.store.getAllFacets());
            const generator = getAutomaticFacetGenerator(this);
            collapseFacetsAfter(visibleFacets, this.collapseFacetsAfter);
            generator?.updateCollapseFacetsDependingOnFacetsVisibility?.(this.collapseFacetsAfter, visibleFacets.length);
            this.append(...visibleFacets, ...invisibleFacets, ...generator ? [
                generator
            ] : []);
        };
        new ValidatePropsController(this, ()=>({
                collapseFacetsAfter: this.collapseFacetsAfter
            }), AtomicFacetManager.propsSchema);
    }
}
AtomicFacetManager.propsSchema = new Schema({
    collapseFacetsAfter: new NumberValue({
        min: -1,
        required: true
    })
});
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicFacetManager.prototype, "bindings", void 0);
_ts_decorate([
    bindStateToController('facetManager', {
        onUpdateCallbackMethod: 'sortFacets'
    }),
    state(),
    _ts_metadata("design:type", "u" < typeof FacetManagerState ? Object : FacetManagerState)
], AtomicFacetManager.prototype, "facetManagerState", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicFacetManager.prototype, "error", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'collapse-facets-after'
    })
], AtomicFacetManager.prototype, "collapseFacetsAfter", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicFacetManager.prototype, "render", null);
AtomicFacetManager = _ts_decorate([
    customElement('atomic-facet-manager'),
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicFacetManager);
export { AtomicFacetManager };
