import { NumberValue, Schema } from "@coveo/bueno";
import { buildAutomaticFacetGenerator, buildSearchStatus } from "@coveo/headless";
import { LitElement, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { keyed } from "lit/directives/keyed.js";
import { renderFacetPlaceholder } from "../../common/facets/facet-placeholder/facet-placeholder.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 { LightDomMixin } from "../../../mixins/light-dom.js";
import "../atomic-automatic-facet/atomic-automatic-facet.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 AtomicAutomaticFacetGenerator extends LightDomMixin(LitElement) {
    initialize() {
        this.searchStatus = buildSearchStatus(this.bindings.engine);
        this.automaticFacetGenerator = buildAutomaticFacetGenerator(this.bindings.engine, {
            options: {
                desiredCount: this.desiredCount,
                numberOfValues: this.numberOfValues
            }
        });
    }
    updateCollapseFacetsDependingOnFacetsVisibility(collapseAfter, numberOfVisibleFacets) {
        if (-1 === collapseAfter) {
            this.collapseFacetsAfter = -1;
            return;
        }
        this.collapseFacetsAfter = Math.max(0, collapseAfter - numberOfVisibleFacets);
    }
    render() {
        if (!this.searchStatusState.firstSearchExecuted) return this.renderPlaceholders();
        return this.renderAutomaticFacets();
    }
    renderPlaceholders() {
        return html`${Array.from({
            length: this.desiredCount
        }, (_, index)=>renderFacetPlaceholder({
                props: {
                    numberOfValues: this.numberOfValues,
                    isCollapsed: this.shouldCollapseFacet(index)
                }
            }))}`;
    }
    renderAutomaticFacets() {
        if (0 === this.automaticFacetGeneratorState.automaticFacets.length) return nothing;
        return html`${this.automaticFacetGeneratorState.automaticFacets.map((facet, index)=>keyed(facet.state.field, html`
            <atomic-automatic-facet
              .field=${facet.state.field}
              .facetId=${facet.state.field}
              .facet=${facet}
              .searchStatus=${this.searchStatus}
              ?isCollapsed=${this.shouldCollapseFacet(index)}
            ></atomic-automatic-facet>
          `))}`;
    }
    shouldCollapseFacet(index) {
        if (-1 === this.collapseFacetsAfter) return false;
        return this.collapseFacetsAfter ? index + 1 > this.collapseFacetsAfter : true;
    }
    constructor(){
        super(), this.desiredCount = 5, this.numberOfValues = 8, this.collapseFacetsAfter = -1;
        new ValidatePropsController(this, ()=>({
                desiredCount: this.desiredCount,
                numberOfValues: this.numberOfValues
            }), AtomicAutomaticFacetGenerator.propsSchema, false);
    }
}
AtomicAutomaticFacetGenerator.propsSchema = new Schema({
    desiredCount: new NumberValue({
        min: 1,
        max: 20,
        required: false
    }),
    numberOfValues: new NumberValue({
        min: 1,
        required: false
    })
});
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'desired-count'
    })
], AtomicAutomaticFacetGenerator.prototype, "desiredCount", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'number-of-values'
    })
], AtomicAutomaticFacetGenerator.prototype, "numberOfValues", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicAutomaticFacetGenerator.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicAutomaticFacetGenerator.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('automaticFacetGenerator'),
    state(),
    _ts_metadata("design:type", "u" < typeof AutomaticFacetGeneratorState ? Object : AutomaticFacetGeneratorState)
], AtomicAutomaticFacetGenerator.prototype, "automaticFacetGeneratorState", void 0);
_ts_decorate([
    bindStateToController('searchStatus'),
    state(),
    _ts_metadata("design:type", "u" < typeof SearchStatusState ? Object : SearchStatusState)
], AtomicAutomaticFacetGenerator.prototype, "searchStatusState", void 0);
_ts_decorate([
    state()
], AtomicAutomaticFacetGenerator.prototype, "collapseFacetsAfter", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicAutomaticFacetGenerator.prototype, "render", null);
AtomicAutomaticFacetGenerator = _ts_decorate([
    customElement('atomic-automatic-facet-generator'),
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicAutomaticFacetGenerator);
export { AtomicAutomaticFacetGenerator };
