import { isNullOrUndefined } from "@coveo/bueno";
import { LitElement, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import facet_common_tw_css from "../../common/facets/facet-common.tw.css.js";
import { renderFacetContainer } from "../../common/facets/facet-container/facet-container.js";
import { renderFacetHeader } from "../../common/facets/facet-header/facet-header.js";
import { renderFacetValueCheckbox } from "../../common/facets/facet-value-checkbox/facet-value-checkbox.js";
import facet_value_checkbox_tw_css from "../../common/facets/facet-value-checkbox/facet-value-checkbox.tw.css.js";
import { renderFacetValueLabelHighlight } from "../../common/facets/facet-value-label-highlight/facet-value-label-highlight.js";
import { renderFacetValuesGroup } from "../../common/facets/facet-values-group/facet-values-group.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 { InitializeBindingsMixin } from "../../../mixins/bindings-mixin.js";
import { FocusTargetController } from "../../../utils/accessibility-utils.js";
import { getFieldValueCaption } from "../../../utils/field-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 AtomicAutomaticFacet extends InitializeBindingsMixin(LitElement) {
    initialize() {}
    get focusTarget() {
        if (!this.headerFocus) this.headerFocus = new FocusTargetController(this, this.bindings);
        return this.headerFocus;
    }
    get numberOfSelectedValues() {
        return this.facet.state.values.filter((value)=>this.isSelected(value)).length;
    }
    isSelected(value) {
        return 'selected' === value.state;
    }
    get label() {
        return isNullOrUndefined(this.facet.state.label) ? this.facet.state.field : this.facet.state.label;
    }
    renderValue(facetValue, onClick) {
        const displayValue = getFieldValueCaption(this.facet.state.field, facetValue.value, this.bindings.i18n);
        return renderFacetValueCheckbox({
            props: {
                displayValue,
                numberOfResults: facetValue.numberOfResults,
                isSelected: this.isSelected(facetValue),
                i18n: this.bindings.i18n,
                onClick
            }
        })(html`${renderFacetValueLabelHighlight({
            props: {
                displayValue,
                isSelected: this.isSelected(facetValue)
            }
        })}`);
    }
    renderValuesContainer(children, query) {
        return renderFacetValuesGroup({
            props: {
                i18n: this.bindings.i18n,
                label: this.facet.state.label,
                query
            }
        })(html`<ul class="mt-3" part="values">
        ${children}
      </ul>`);
    }
    renderValues() {
        return this.renderValuesContainer(this.facet.state.values.map((value)=>this.renderValue(value, ()=>this.facet.toggleSelect(value))));
    }
    renderHeader() {
        return renderFacetHeader({
            props: {
                i18n: this.bindings.i18n,
                label: this.label,
                onClearFilters: ()=>{
                    this.focusTarget.focusAfterSearch();
                    this.facet.deselectAll();
                },
                numberOfActiveValues: this.numberOfSelectedValues,
                isCollapsed: this.isCollapsed,
                headingLevel: 0,
                onToggleCollapse: ()=>{
                    this.isCollapsed = !this.isCollapsed;
                },
                headerRef: (el)=>this.focusTarget.setTarget(el)
            }
        });
    }
    render() {
        if (this.searchStatus.state.hasError) return nothing;
        return renderFacetContainer()(html`
      ${this.renderHeader()}
      ${when(!this.isCollapsed, ()=>this.renderValues())}
    `);
    }
    constructor(...args){
        super(...args), this.isCollapsed = false;
    }
}
AtomicAutomaticFacet.styles = [
    facet_common_tw_css,
    facet_value_checkbox_tw_css
];
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicAutomaticFacet.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicAutomaticFacet.prototype, "error", void 0);
_ts_decorate([
    property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicAutomaticFacet.prototype, "field", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'facet-id',
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicAutomaticFacet.prototype, "facetId", void 0);
_ts_decorate([
    property({
        type: Object,
        reflect: true
    }),
    _ts_metadata("design:type", "u" < typeof AutomaticFacet ? Object : AutomaticFacet)
], AtomicAutomaticFacet.prototype, "facet", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: 'search-status',
        reflect: true
    }),
    _ts_metadata("design:type", "u" < typeof SearchStatus ? Object : SearchStatus)
], AtomicAutomaticFacet.prototype, "searchStatus", void 0);
_ts_decorate([
    property({
        type: Boolean,
        attribute: 'is-collapsed',
        reflect: true
    })
], AtomicAutomaticFacet.prototype, "isCollapsed", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicAutomaticFacet.prototype, "render", null);
AtomicAutomaticFacet = _ts_decorate([
    customElement('atomic-automatic-facet'),
    bindings(),
    withTailwindStyles
], AtomicAutomaticFacet);
export { AtomicAutomaticFacet };
