import { buildProductListing, buildSearch } from "@coveo/headless/commerce";
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { guard } from "lit/directives/guard.js";
import { map } from "lit/directives/map.js";
import { bindings } from "../../../decorators/bindings.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { bindStateToController } from "../../../decorators/bind-state.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { randomID } from "../../../utils/utils.js";
import { renderSortLabel } from "../../common/sort/label.js";
import { renderSortSelect } from "../../common/sort/select.js";
import { sortGuard } from "../../common/sort/sort-guard.js";
import { getSortByLabel, renderCommerceSortOption } from "../sort/option.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 AtomicCommerceSortDropdown extends LitElement {
    initialize() {
        if ('product-listing' === this.bindings.interfaceElement.type) this.searchOrListing = buildProductListing(this.bindings.engine);
        else this.searchOrListing = buildSearch(this.bindings.engine);
        this.sort = this.searchOrListing.sort();
    }
    select(e) {
        const select = e.composedPath()[0];
        this.sort.sortBy(getSortByLabel(select.value, this.sortState.availableSorts));
    }
    sortLabelTemplate() {
        return renderSortLabel({
            props: {
                id: this.dropdownId,
                i18n: this.bindings.i18n
            }
        });
    }
    sortSelectTemplate() {
        const { bindings: { i18n }, dropdownId: id } = this;
        return renderSortSelect({
            props: {
                i18n,
                id,
                onSelect: (evt)=>this.select(evt)
            }
        })(html`${guard([
            this.sortState
        ], ()=>map(this.sortState.availableSorts, (sort)=>renderCommerceSortOption({
                    props: {
                        i18n,
                        selected: this.sort.isSortedBy(sort),
                        sort
                    }
                })))}`);
    }
    render() {
        const { responseId, error, products, isLoading } = this.searchOrListingState;
        return html`${sortGuard({
            isLoading,
            firstSearchExecuted: '' !== responseId,
            hasResults: products.length > 0 && this.sortState.availableSorts.length > 1,
            hasError: null !== error
        }, ()=>html`<div class="text-on-background flex flex-wrap items-center">
          ${this.sortLabelTemplate()} ${this.sortSelectTemplate()}
        </div>`)}`;
    }
    constructor(...args){
        super(...args), this.dropdownId = randomID('atomic-commerce-sort-dropdown-');
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceSortDropdown.prototype, "bindings", void 0);
_ts_decorate([
    bindStateToController('sort'),
    state(),
    _ts_metadata("design:type", "u" < typeof SortState ? Object : SortState)
], AtomicCommerceSortDropdown.prototype, "sortState", void 0);
_ts_decorate([
    bindStateToController('searchOrListing'),
    state(),
    _ts_metadata("design:type", Object)
], AtomicCommerceSortDropdown.prototype, "searchOrListingState", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceSortDropdown.prototype, "error", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicCommerceSortDropdown.prototype, "render", null);
AtomicCommerceSortDropdown = _ts_decorate([
    customElement('atomic-commerce-sort-dropdown'),
    bindings(),
    withTailwindStyles
], AtomicCommerceSortDropdown);
export { AtomicCommerceSortDropdown };
