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 { UMB_DOCUMENT_ENTITY_TYPE } from '../../../entity.js';
import { UMB_EDIT_DOCUMENT_WORKSPACE_PATH_PATTERN } from '../../../paths.js';
import { css, customElement, html, state } from '../../../../../../external/lit/index.js';
import { UmbLitElement } from '../../../../../core/lit-element/index.js';
import { UmbTextStyles } from '../../../../../core/style/index.js';
import './column-layouts/document-entity-actions-table-column-view.element.js';
import './column-layouts/document-table-column-name.element.js';
import './column-layouts/document-table-column-property-value.element.js';
import './column-layouts/document-table-column-state.element.js';
let UmbDocumentTableCollectionViewElement = class UmbDocumentTableCollectionViewElement extends UmbLitElement {
    #systemColumns;
    #collectionContext;
    constructor() {
        super();
        this._tableConfig = {
            allowSelection: true,
        };
        this._tableColumns = [];
        this.#systemColumns = [
            {
                name: this.localize.term('general_name'),
                alias: 'name',
                elementName: 'umb-document-table-column-name',
                allowSorting: true,
            },
            {
                name: this.localize.term('content_publishStatus'),
                alias: 'state',
                elementName: 'umb-document-table-column-state',
                allowSorting: false,
            },
        ];
        this._tableItems = [];
        this._selection = [];
        this.consumeContext(UMB_DOCUMENT_COLLECTION_CONTEXT, (collectionContext) => {
            this.#collectionContext = collectionContext;
            collectionContext?.setupView(this);
            this.#observeCollectionContext();
        });
    }
    #observeCollectionContext() {
        if (!this.#collectionContext)
            return;
        this.observe(this.#collectionContext.userDefinedProperties, (userDefinedProperties) => {
            this._userDefinedProperties = userDefinedProperties;
            this.#createTableHeadings();
        }, '_observeUserDefinedProperties');
        this.observe(this.#collectionContext.items, (items) => {
            this._items = items;
            this.#createTableItems(this._items);
        }, '_observeItems');
        this.observe(this.#collectionContext.selection.selection, (selection) => {
            this._selection = selection;
        }, '_observeSelection');
    }
    #createTableHeadings() {
        if (this._userDefinedProperties && this._userDefinedProperties.length > 0) {
            const userColumns = this._userDefinedProperties.map((item) => {
                return {
                    name: this.localize.string(item.header),
                    alias: item.alias,
                    elementName: item.elementName || 'umb-document-table-column-property-value',
                    labelTemplate: item.nameTemplate,
                    allowSorting: true,
                };
            });
            this._tableColumns = [
                ...this.#systemColumns,
                ...userColumns,
                { name: '', alias: 'entityActions', align: 'right' },
            ];
        }
    }
    #createTableItems(items) {
        this._tableItems = items.map((item) => {
            if (!item.unique)
                throw new Error('Item id is missing.');
            const data = this._tableColumns?.map((column) => {
                if (column.alias === 'entityActions') {
                    return {
                        columnAlias: 'entityActions',
                        value: html `<umb-document-entity-actions-table-column-view
								.value=${item}></umb-document-entity-actions-table-column-view>`,
                    };
                }
                const editPath = UMB_EDIT_DOCUMENT_WORKSPACE_PATH_PATTERN.generateAbsolute({
                    unique: item.unique,
                });
                return {
                    columnAlias: column.alias,
                    value: { item, editPath },
                };
            }) ?? [];
            return {
                id: item.unique,
                icon: item.documentType.icon,
                entityType: UMB_DOCUMENT_ENTITY_TYPE,
                data: data,
            };
        });
    }
    #handleSelect(event) {
        event.stopPropagation();
        const table = event.target;
        const selection = table.selection;
        this.#collectionContext?.selection.setSelection(selection);
    }
    #handleDeselect(event) {
        event.stopPropagation();
        const table = event.target;
        const selection = table.selection;
        this.#collectionContext?.selection.setSelection(selection);
    }
    #handleOrdering(event) {
        const table = event.target;
        const orderingColumn = table.orderingColumn;
        const orderingDesc = table.orderingDesc;
        this.#collectionContext?.setFilter({
            orderBy: orderingColumn,
            orderDirection: orderingDesc ? 'desc' : 'asc',
        });
    }
    render() {
        return html `
			<umb-table
				.config=${this._tableConfig}
				.columns=${this._tableColumns}
				.items=${this._tableItems}
				.selection=${this._selection}
				@selected=${this.#handleSelect}
				@deselected=${this.#handleDeselect}
				@ordered=${this.#handleOrdering}></umb-table>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				display: block;
				box-sizing: border-box;
				height: auto;
				width: 100%;
				padding: var(--uui-size-space-3) 0;
			}

			.container {
				display: flex;
				justify-content: center;
				align-items: center;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbDocumentTableCollectionViewElement.prototype, "_userDefinedProperties", void 0);
__decorate([
    state()
], UmbDocumentTableCollectionViewElement.prototype, "_items", void 0);
__decorate([
    state()
], UmbDocumentTableCollectionViewElement.prototype, "_tableConfig", void 0);
__decorate([
    state()
], UmbDocumentTableCollectionViewElement.prototype, "_tableColumns", void 0);
__decorate([
    state()
], UmbDocumentTableCollectionViewElement.prototype, "_tableItems", void 0);
__decorate([
    state()
], UmbDocumentTableCollectionViewElement.prototype, "_selection", void 0);
UmbDocumentTableCollectionViewElement = __decorate([
    customElement('umb-document-table-collection-view')
], UmbDocumentTableCollectionViewElement);
export { UmbDocumentTableCollectionViewElement };
export default UmbDocumentTableCollectionViewElement;
