var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") 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;
};
import { html, customElement, property, state, repeat, nothing, css, } from '../../../../../external/lit/index.js';
import { UmbLitElement } from '../../../lit-element/index.js';
import '../menu-item/collection-menu-item.element.js';
let UmbDefaultCollectionMenuElement = class UmbDefaultCollectionMenuElement extends UmbLitElement {
    constructor() {
        super(...arguments);
        this._selectionConfiguration = {
            multiple: false,
            selectable: true,
            selection: [],
        };
        this.selectionConfiguration = this._selectionConfiguration;
        this.selectableFilter = () => true;
        this.filter = () => true;
        this._items = [];
        this._currentPage = 1;
        this._totalPages = 1;
        this.#onLoadMoreClick = (event) => {
            event.stopPropagation();
            const next = (this._currentPage = this._currentPage + 1);
            this._api?.pagination.setCurrentPageNumber(next);
        };
    }
    get api() {
        return this._api;
    }
    set api(value) {
        this._api = value;
        if (this._api) {
            this._api.filterArgs = this.#filterArgs;
        }
        this.#observeData();
    }
    get filterArgs() {
        return this.#filterArgs;
    }
    set filterArgs(value) {
        this.#filterArgs = value;
        if (this._api) {
            this._api.filterArgs = this.#filterArgs;
        }
    }
    #filterArgs;
    #observeData() {
        this.observe(this._api?.items, (items) => (this._items = items ?? []));
        this.observe(this._api?.pagination.currentPage, (value) => (this._currentPage = value ?? 1));
        this.observe(this._api?.pagination.totalPages, (value) => (this._totalPages = value ?? 1));
    }
    async updated(_changedProperties) {
        super.updated(_changedProperties);
        if (this._api === undefined)
            return;
        if (_changedProperties.has('selectionConfiguration')) {
            this._selectionConfiguration = this.selectionConfiguration;
            this._api.selection.setMultiple(this._selectionConfiguration.multiple ?? false);
            this._api.selection.setSelectable(this._selectionConfiguration.selectable ?? true);
            this._api.selection.setSelection(this._selectionConfiguration.selection ?? []);
        }
        if (_changedProperties.has('selectableFilter')) {
            this._api.selectableFilter = this.selectableFilter;
        }
        if (_changedProperties.has('filter')) {
            this._api.filter = this.filter;
        }
    }
    #onLoadMoreClick;
    render() {
        return html `
			${repeat(this._items, (item) => item.unique, (item) => this.#renderItem(item))}
			${this.#renderPaging()}
		`;
    }
    #renderItem(item) {
        return html `
			<umb-collection-menu-item
				entityType=${item.entityType}
				.props=${{
            item: item,
        }}></umb-collection-menu-item>
		`;
    }
    #renderPaging() {
        if (this._totalPages <= 1 || this._currentPage === this._totalPages) {
            return nothing;
        }
        return html ` <uui-button
			data-mark="load-more"
			id="load-more"
			look="secondary"
			.label=${this.localize.term('actions_loadMore')}
			@click=${this.#onLoadMoreClick}></uui-button>`;
    }
    static { this.styles = css `
		:host {
			--uui-menu-item-indent: 0;
			--uui-menu-item-flat-structure: 1;
		}

		#load-more {
			width: 100%;
		}
	`; }
};
__decorate([
    property({ type: Object, attribute: false })
], UmbDefaultCollectionMenuElement.prototype, "api", null);
__decorate([
    property({ type: Object, attribute: false })
], UmbDefaultCollectionMenuElement.prototype, "selectionConfiguration", void 0);
__decorate([
    property({ attribute: false })
], UmbDefaultCollectionMenuElement.prototype, "selectableFilter", void 0);
__decorate([
    property({ attribute: false })
], UmbDefaultCollectionMenuElement.prototype, "filter", void 0);
__decorate([
    state()
], UmbDefaultCollectionMenuElement.prototype, "_items", void 0);
__decorate([
    state()
], UmbDefaultCollectionMenuElement.prototype, "_currentPage", void 0);
__decorate([
    state()
], UmbDefaultCollectionMenuElement.prototype, "_totalPages", void 0);
UmbDefaultCollectionMenuElement = __decorate([
    customElement('umb-default-collection-menu')
], UmbDefaultCollectionMenuElement);
export { UmbDefaultCollectionMenuElement };
export { UmbDefaultCollectionMenuElement as element };
