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 { UMB_MEMBER_COLLECTION_CONTEXT } from '../../member-collection.context-token.js';
import { UmbMemberKind } from '../../../utils/index.js';
import { UmbTextStyles } from '../../../../../core/style/index.js';
import { css, html, customElement, state } from '../../../../../../external/lit/index.js';
import { UmbLitElement } from '../../../../../core/lit-element/index.js';
import { UmbMemberTypeItemRepository } from '../../../../member-type/index.js';
import { UmbDirection } from '../../../../../core/utils/index.js';
let UmbMemberTableCollectionViewElement = class UmbMemberTableCollectionViewElement extends UmbLitElement {
    #collectionContext;
    #memberTypeItemRepository;
    constructor() {
        super();
        this._tableConfig = {
            allowSelection: false,
        };
        this._tableColumns = [
            {
                name: this.localize.term('general_name'),
                alias: 'memberName',
                allowSorting: true,
            },
            {
                name: this.localize.term('general_username'),
                alias: 'memberUsername',
                allowSorting: true,
            },
            {
                name: this.localize.term('general_email'),
                alias: 'memberEmail',
                allowSorting: true,
            },
            {
                name: this.localize.term('content_membertype'),
                alias: 'memberType',
                allowSorting: true,
            },
            {
                name: this.localize.term('member_kind'),
                alias: 'memberKind',
            },
            {
                name: '',
                alias: 'entityActions',
                align: 'right',
            },
        ];
        this._tableItems = [];
        this._orderingColumn = 'memberUsername';
        this._orderingDesc = false;
        this.#memberTypeItemRepository = new UmbMemberTypeItemRepository(this);
        this.#columnAliasToOrderBy = {
            memberName: 'name',
            memberUsername: 'username',
            memberEmail: 'email',
            memberType: 'memberType',
        };
        this.consumeContext(UMB_MEMBER_COLLECTION_CONTEXT, (instance) => {
            this.#collectionContext = instance;
            this.#observeCollectionItems();
        });
    }
    #columnAliasToOrderBy;
    #handleOrdering(event) {
        const table = event.target;
        const orderingColumn = table.orderingColumn;
        const orderingDesc = table.orderingDesc;
        const orderBy = orderingColumn ? this.#columnAliasToOrderBy[orderingColumn] : undefined;
        if (orderBy) {
            this._orderingColumn = orderingColumn;
            this._orderingDesc = orderingDesc;
            this.#collectionContext?.setFilter({
                orderBy,
                orderDirection: orderingDesc ? UmbDirection.DESCENDING : UmbDirection.ASCENDING,
            });
        }
    }
    #observeCollectionItems() {
        if (!this.#collectionContext)
            return;
        this.observe(this.#collectionContext.items, (items) => this.#createTableItems(items), 'umbCollectionItemsObserver');
    }
    async #createTableItems(members) {
        const memberTypeUniques = members.map((member) => member.memberType.unique);
        const { data: memberTypes } = await this.#memberTypeItemRepository.requestItems(memberTypeUniques);
        this._tableItems = members.map((member) => {
            // TODO: get correct variant name
            const name = member.variants[0].name;
            const kind = member.kind === UmbMemberKind.API
                ? this.localize.term('member_memberKindApi')
                : this.localize.term('member_memberKindDefault');
            const memberType = memberTypes?.find((type) => type.unique === member.memberType.unique);
            return {
                id: member.unique,
                icon: memberType?.icon,
                data: [
                    {
                        columnAlias: 'memberName',
                        value: html `<a href=${'section/member-management/workspace/member/edit/' + member.unique}>${name}</a>`,
                    },
                    {
                        columnAlias: 'memberUsername',
                        value: member.username,
                    },
                    {
                        columnAlias: 'memberEmail',
                        value: member.email,
                    },
                    {
                        columnAlias: 'memberType',
                        value: memberType?.name,
                    },
                    {
                        columnAlias: 'memberKind',
                        value: kind,
                    },
                    {
                        columnAlias: 'entityActions',
                        value: html `<umb-entity-actions-table-column-view
							.value=${{
                            entityType: member.entityType,
                            unique: member.unique,
                            name: member.variants[0].name,
                        }}></umb-entity-actions-table-column-view>`,
                    },
                ],
            };
        });
    }
    render() {
        return html `
			<umb-table
				.config=${this._tableConfig}
				.columns=${this._tableColumns}
				.items=${this._tableItems}
				.orderingColumn=${this._orderingColumn}
				.orderingDesc=${this._orderingDesc}
				@ordered=${this.#handleOrdering}></umb-table>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				display: flex;
				flex-direction: column;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbMemberTableCollectionViewElement.prototype, "_tableConfig", void 0);
__decorate([
    state()
], UmbMemberTableCollectionViewElement.prototype, "_tableColumns", void 0);
__decorate([
    state()
], UmbMemberTableCollectionViewElement.prototype, "_tableItems", void 0);
__decorate([
    state()
], UmbMemberTableCollectionViewElement.prototype, "_orderingColumn", void 0);
__decorate([
    state()
], UmbMemberTableCollectionViewElement.prototype, "_orderingDesc", void 0);
UmbMemberTableCollectionViewElement = __decorate([
    customElement('umb-member-table-collection-view')
], UmbMemberTableCollectionViewElement);
export { UmbMemberTableCollectionViewElement };
export default UmbMemberTableCollectionViewElement;
