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_DOCUMENT_COLLECTION_CONTEXT } from '../document-collection.context-token.js';
import { UmbDocumentItemDataResolver } from '../../item/document-item-data-resolver.js';
import { css, customElement, html, ifDefined, nothing, property, repeat, state, when, } from '../../../../../external/lit/index.js';
import { UmbLitElement } from '../../../../core/lit-element/index.js';
import { UmbDeselectedEvent, UmbSelectedEvent } from '../../../../core/event/index.js';
import { UmbEntityContentTypeEntityContext } from '../../../../content/content-type/index.js';
import { UMB_DOCUMENT_TYPE_ENTITY_TYPE } from '../../../document-types/index.js';
import { DocumentVariantStateModel } from '../../../../core/backend-api/index.js';
import { fromCamelCase } from '../../../../core/utils/index.js';
let UmbDocumentCollectionItemCardElement = class UmbDocumentCollectionItemCardElement extends UmbLitElement {
    #item;
    #entityContentTypeContext;
    #resolver;
    get item() {
        return this.#item;
    }
    set item(value) {
        this.#item = value;
        this.#resolver.setData(value);
        const documentTypeUnique = value?.documentType.unique;
        this.#entityContentTypeContext.setEntityType(documentTypeUnique ? UMB_DOCUMENT_TYPE_ENTITY_TYPE : undefined);
        this.#entityContentTypeContext.setUnique(documentTypeUnique);
    }
    #collectionContext;
    constructor() {
        super();
        this.#entityContentTypeContext = new UmbEntityContentTypeEntityContext(this);
        this.#resolver = new UmbDocumentItemDataResolver(this);
        this.selectable = false;
        this.selected = false;
        this.selectOnly = false;
        this.disabled = false;
        this.consumeContext(UMB_DOCUMENT_COLLECTION_CONTEXT, (collectionContext) => {
            this.#collectionContext = collectionContext;
            this.#observeCollectionContext();
        });
        this.#resolver.observe(this.#resolver.name, (name) => (this._name = name || ''));
        this.#resolver.observe(this.#resolver.state, (state) => (this._state = state || ''));
        this.#resolver.observe(this.#resolver.createDate, (createDate) => (this._createDate = createDate));
        this.#resolver.observe(this.#resolver.updateDate, (updateDate) => (this._updateDate = updateDate));
    }
    #observeCollectionContext() {
        if (!this.#collectionContext)
            return;
        this.observe(this.#collectionContext.userDefinedProperties, (userDefinedProperties) => {
            this._userDefinedProperties = userDefinedProperties;
        }, '_observeUserDefinedProperties');
    }
    #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));
    }
    #getPropertyValueByAlias(alias) {
        if (!this.item)
            return '';
        switch (alias) {
            case 'contentTypeAlias':
                return this.item.documentType.alias;
            case 'createDate':
                return this._createDate?.toLocaleString();
            case 'creator':
            case 'owner':
                return this.item.creator;
            case 'name':
                return this._name;
            case 'state':
                return this._state ? fromCamelCase(this._state) : '';
            case 'published':
                return this._state !== DocumentVariantStateModel.DRAFT ? 'True' : 'False';
            case 'sortOrder':
                return this.item.sortOrder;
            case 'updateDate':
                return this._updateDate?.toLocaleString();
            case 'updater':
                return this.item.updater;
            default: {
                const culture = this.#resolver.getCulture();
                const prop = this.item.values.find((x) => x.alias === alias && (!x.culture || x.culture === culture));
                return prop?.value ?? '';
            }
        }
    }
    #getStateTagConfig() {
        if (!this._state)
            return;
        switch (this._state) {
            case DocumentVariantStateModel.PUBLISHED:
                return { color: 'positive', label: this.localize.term('content_published') };
            case DocumentVariantStateModel.PUBLISHED_PENDING_CHANGES:
                return { color: 'warning', label: this.localize.term('content_publishedPendingChanges') };
            case DocumentVariantStateModel.DRAFT:
                return { color: 'default', label: this.localize.term('content_unpublished') };
            case DocumentVariantStateModel.NOT_CREATED:
                return { color: 'danger', label: this.localize.term('content_notCreated') };
            default:
                return { color: 'danger', label: fromCamelCase(this._state) };
        }
    }
    render() {
        if (!this.item)
            return nothing;
        return html `
			<uui-card-content-node
				.name=${this._name}
				href=${ifDefined(this.href)}
				?selectable=${this.selectable}
				?select-only=${this.selectOnly}
				?selected=${this.selected}
				?disabled=${this.disabled}
				@selected=${this.#onSelected}
				@deselected=${this.#onDeselected}>
				${this.#renderIcon()} ${this.#renderState()}
				<div id="properties">${this.#renderProperties()}</div>
				<slot name="actions" slot="actions"></slot>
			</uui-card-content-node>
		`;
    }
    #renderIcon() {
        if (!this.item)
            return nothing;
        return html `<umb-icon slot="icon" name=${this.item.documentType.icon}></umb-icon>`;
    }
    #renderState() {
        const tagConfig = this.#getStateTagConfig();
        if (!tagConfig)
            return nothing;
        return html `<uui-tag slot="tag" id="state" color=${tagConfig.color} look="secondary">${tagConfig.label}</uui-tag>`;
    }
    #renderProperties() {
        if (!this._userDefinedProperties)
            return;
        return html `
			<ul>
				${repeat(this._userDefinedProperties, (column) => column.alias, (column) => this.#renderProperty(column))}
			</ul>
		`;
    }
    #renderProperty(column) {
        const value = this.#getPropertyValueByAlias(column.alias);
        return html `
			<li>
				<span>${this.localize.string(column.header)}:</span>
				${when(column.nameTemplate, () => html `<umb-ufm-render inline .markdown=${column.nameTemplate} .value=${{ value }}></umb-ufm-render>`, () => html `${value}`)}
			</li>
		`;
    }
    static { this.styles = [
        css `
			uui-card-content-node {
				min-width: auto;
				width: 100%;
				min-height: 180px;
			}

			#properties {
				font-size: var(--uui-type-small-size);
				line-height: calc(2 * var(--uui-size-3));

				> ul {
					list-style: none;
					padding-inline-start: 0px;
					margin: 0;

					> li > span {
						font-weight: 700;
					}
				}
			}
		`,
    ]; }
};
__decorate([
    property({ type: Object })
], UmbDocumentCollectionItemCardElement.prototype, "item", null);
__decorate([
    property({ type: Boolean })
], UmbDocumentCollectionItemCardElement.prototype, "selectable", void 0);
__decorate([
    property({ type: Boolean })
], UmbDocumentCollectionItemCardElement.prototype, "selected", void 0);
__decorate([
    property({ type: Boolean })
], UmbDocumentCollectionItemCardElement.prototype, "selectOnly", void 0);
__decorate([
    property({ type: Boolean })
], UmbDocumentCollectionItemCardElement.prototype, "disabled", void 0);
__decorate([
    property({ type: String })
], UmbDocumentCollectionItemCardElement.prototype, "href", void 0);
__decorate([
    state()
], UmbDocumentCollectionItemCardElement.prototype, "_name", void 0);
__decorate([
    state()
], UmbDocumentCollectionItemCardElement.prototype, "_state", void 0);
__decorate([
    state()
], UmbDocumentCollectionItemCardElement.prototype, "_createDate", void 0);
__decorate([
    state()
], UmbDocumentCollectionItemCardElement.prototype, "_updateDate", void 0);
__decorate([
    state()
], UmbDocumentCollectionItemCardElement.prototype, "_userDefinedProperties", void 0);
UmbDocumentCollectionItemCardElement = __decorate([
    customElement('umb-document-collection-item-card')
], UmbDocumentCollectionItemCardElement);
export { UmbDocumentCollectionItemCardElement };
export { UmbDocumentCollectionItemCardElement as element };
