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 { getDisplayStateFromUserStatus, TimeFormatOptions } from '../../utils.js';
import { UmbUserKind } from '../../utils/user-kind.js';
import { css, customElement, html, ifDefined, nothing, property, state, when, } from '../../../../../external/lit/index.js';
import { UmbLitElement } from '../../../../core/lit-element/index.js';
import { UmbUserGroupItemRepository } from '../../../user-group/index.js';
import { UmbDeselectedEvent, UmbSelectedEvent } from '../../../../core/event/index.js';
import { UserStateModel } from '../../../../core/backend-api/index.js';
let UmbUserCollectionItemCardElement = class UmbUserCollectionItemCardElement extends UmbLitElement {
    constructor() {
        super(...arguments);
        this.selectable = false;
        this.selected = false;
        this.selectOnly = false;
        this.disabled = false;
        this._userGroupItems = [];
        this.#userGroupItemRepository = new UmbUserGroupItemRepository(this);
    }
    #item;
    get item() {
        return this.#item;
    }
    set item(value) {
        this.#item = value;
        this.#loadUserGroups();
    }
    #userGroupItemRepository;
    async #loadUserGroups() {
        if (!this.item || !this.item.userGroupUniques || this.item.userGroupUniques.length === 0) {
            this._userGroupItems = [];
            return;
        }
        const { data } = await this.#userGroupItemRepository.requestItems(this.item.userGroupUniques.map((ref) => ref.unique));
        this._userGroupItems = data ?? [];
    }
    #onSelected(event) {
        if (!this.item)
            return;
        event.stopPropagation();
        this.dispatchEvent(new UmbSelectedEvent(this.item.unique));
    }
    #onDeselected(event) {
        if (!this.item)
            return;
        event.stopPropagation();
        this.dispatchEvent(new UmbDeselectedEvent(this.item.unique));
    }
    render() {
        if (!this.item)
            return nothing;
        return html `
			<uui-card-user
				.name=${this.item.name ?? this.localize.term('general_unnamed')}
				href=${ifDefined(this.href)}
				?selectable=${this.selectable}
				?select-only=${this.selectOnly}
				?selected=${this.selected}
				?disabled=${this.disabled}
				@selected=${this.#onSelected}
				@deselected=${this.#onDeselected}>
				${this.#renderUserTag()} ${this.#renderUserGroupNames()} ${this.#renderUserLoginDate()}
				<umb-user-avatar
					slot="avatar"
					.name=${this.item.name}
					.kind=${this.item.kind}
					.imgUrls=${this.item.avatarUrls}></umb-user-avatar>
			</uui-card-user>
		`;
    }
    #renderUserTag() {
        if (this.item?.state && this.item?.state === UserStateModel.ACTIVE) {
            return nothing;
        }
        const statusLook = this.item?.state ? getDisplayStateFromUserStatus(this.item.state) : undefined;
        return html `
			<uui-tag slot="tag" look=${ifDefined(statusLook?.look)} color=${ifDefined(statusLook?.color)}>
				<umb-localize key=${'user_' + statusLook?.key}></umb-localize>
			</uui-tag>
		`;
    }
    #renderUserGroupNames() {
        const userGroupNames = this._userGroupItems
            .filter((userGroup) => this.item?.userGroupUniques?.map((reference) => reference.unique).includes(userGroup.unique))
            .map((userGroup) => userGroup.name)
            .join(', ');
        return html `<div>${userGroupNames}</div>`;
    }
    #renderUserLoginDate() {
        if (this.item?.kind === UmbUserKind.API)
            return nothing;
        return html `
			<div class="user-login-time">
				${when(this.item?.lastLoginDate, (lastLoginDate) => html `
						<umb-localize key="user_lastLogin">Last login</umb-localize>
						<span>${this.localize.date(lastLoginDate, TimeFormatOptions)}</span>
					`, () => html `<umb-localize key="user_noLogin">has not logged in yet</umb-localize>`)}
			</div>
		`;
    }
    static { this.styles = [
        css `
			uui-card-user {
				width: 100%;
				min-width: auto;
				height: 100%;
				justify-content: normal;
				padding-top: var(--uui-size-space-5);
				flex-direction: column;

				umb-user-avatar {
					font-size: 1.6rem;
				}
			}

			.user-login-time {
				margin-top: var(--uui-size-1);
			}
		`,
    ]; }
};
__decorate([
    property({ type: Object })
], UmbUserCollectionItemCardElement.prototype, "item", null);
__decorate([
    property({ type: Boolean })
], UmbUserCollectionItemCardElement.prototype, "selectable", void 0);
__decorate([
    property({ type: Boolean })
], UmbUserCollectionItemCardElement.prototype, "selected", void 0);
__decorate([
    property({ type: Boolean })
], UmbUserCollectionItemCardElement.prototype, "selectOnly", void 0);
__decorate([
    property({ type: Boolean })
], UmbUserCollectionItemCardElement.prototype, "disabled", void 0);
__decorate([
    property({ type: String })
], UmbUserCollectionItemCardElement.prototype, "href", void 0);
__decorate([
    state()
], UmbUserCollectionItemCardElement.prototype, "_userGroupItems", void 0);
UmbUserCollectionItemCardElement = __decorate([
    customElement('umb-user-collection-item-card')
], UmbUserCollectionItemCardElement);
export { UmbUserCollectionItemCardElement };
export { UmbUserCollectionItemCardElement as element };
