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 { UmbMemberGroupCollectionRepository } from '../../collection/index.js';
import { css, customElement, html, repeat, state, when } from '../../../../../external/lit/index.js';
import { UmbModalBaseElement } from '../../../../core/modal/index.js';
import { UmbPaginationManager, UmbSelectionManager } from '../../../../core/utils/index.js';
const PAGE_SIZE = 50;
let UmbMemberGroupPickerModalElement = class UmbMemberGroupPickerModalElement extends UmbModalBaseElement {
    constructor() {
        super(...arguments);
        this._memberGroups = [];
        this._currentPage = 1;
        this._totalPages = 1;
        this.#collectionRepository = new UmbMemberGroupCollectionRepository(this);
        this.#selectionManager = new UmbSelectionManager(this);
        this.#pagination = new UmbPaginationManager();
    }
    #collectionRepository;
    #selectionManager;
    #pagination;
    connectedCallback() {
        super.connectedCallback();
        this.#pagination.setPageSize(PAGE_SIZE);
        this.#selectionManager.setSelectable(true);
        this.#selectionManager.setMultiple(this.data?.multiple ?? false);
        this.#selectionManager.setSelection(this.value?.selection ?? []);
    }
    firstUpdated() {
        this.#loadMemberGroups();
    }
    async #loadMemberGroups() {
        const skip = this.#pagination.getSkip();
        const { data } = await this.#collectionRepository.requestCollection({ skip, take: PAGE_SIZE });
        this._memberGroups = data?.items ?? [];
        this.#pagination.setTotalItems(data?.total ?? 0);
        this._currentPage = this.#pagination.getCurrentPageNumber();
        this._totalPages = this.#pagination.getTotalPages();
    }
    #onPageChange(event) {
        event.stopPropagation();
        this.#pagination.setCurrentPageNumber(event.target.current);
        this.#loadMemberGroups();
    }
    get #filteredMemberGroups() {
        if (this.data?.filter) {
            return this._memberGroups.filter(this.data.filter);
        }
        else {
            return this._memberGroups;
        }
    }
    #submit() {
        this.value = { selection: this.#selectionManager.getSelection() };
        this.modalContext?.submit();
    }
    #close() {
        this.modalContext?.reject();
    }
    render() {
        return html `
			<umb-body-layout headline=${this.localize.term('defaultdialogs_chooseMemberGroup')}>
				<uui-box>
					${repeat(this.#filteredMemberGroups, (item) => item.unique, (item) => html `
							<uui-menu-item
								label=${item.name ?? ''}
								selectable
								@selected=${() => this.#selectionManager.select(item.unique)}
								@deselected=${() => this.#selectionManager.deselect(item.unique)}
								?selected=${this.#selectionManager.isSelected(item.unique)}>
								<uui-icon slot="icon" name="icon-users"></uui-icon>
							</uui-menu-item>
						`)}
				</uui-box>
				${when(this._totalPages > 1, () => html ` <uui-pagination
							.current=${this._currentPage}
							.total=${this._totalPages}
							firstlabel=${this.localize.term('general_first')}
							previouslabel=${this.localize.term('general_previous')}
							nextlabel=${this.localize.term('general_next')}
							lastlabel=${this.localize.term('general_last')}
							@change=${this.#onPageChange}></uui-pagination>`)}
				<div slot="actions">
					<uui-button label=${this.localize.term('general_close')} @click=${this.#close}></uui-button>
					<uui-button
						label=${this.localize.term('general_choose')}
						look="primary"
						color="positive"
						@click=${this.#submit}></uui-button>
				</div>
			</umb-body-layout>
		`;
    }
    static { this.styles = [
        css `
			uui-pagination {
				display: block;
				margin-top: var(--uui-size-layout-1);
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbMemberGroupPickerModalElement.prototype, "_memberGroups", void 0);
__decorate([
    state()
], UmbMemberGroupPickerModalElement.prototype, "_currentPage", void 0);
__decorate([
    state()
], UmbMemberGroupPickerModalElement.prototype, "_totalPages", void 0);
UmbMemberGroupPickerModalElement = __decorate([
    customElement('umb-member-group-picker-modal')
], UmbMemberGroupPickerModalElement);
export { UmbMemberGroupPickerModalElement };
export default UmbMemberGroupPickerModalElement;
