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_EDIT_MEDIA_WORKSPACE_PATH_PATTERN } from '../../../paths.js';
import { UMB_MEDIA_COLLECTION_CONTEXT } from '../../media-collection.context-token.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/media-table-column-name.element.js';
let UmbMediaTableCollectionViewElement = class UmbMediaTableCollectionViewElement extends UmbLitElement {
    #systemColumns;
    #collectionContext;
    constructor() {
        super();
        this._tableConfig = {
            allowSelection: true,
        };
        this._tableColumns = [];
        this.#systemColumns = [
            {
                name: this.localize.term('general_name'),
                alias: 'name',
                elementName: 'umb-media-table-column-name',
                allowSorting: true,
            },
        ];
        this._tableItems = [];
        this._selection = [];
        this.consumeContext(UMB_MEDIA_COLLECTION_CONTEXT, (collectionContext) => {
            this.#collectionContext = collectionContext;
            this.#observeCollectionContext();
            collectionContext?.setupView(this);
        });
    }
    #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();
        }, '_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,
                    labelTemplate: item.nameTemplate,
                    allowSorting: true,
                };
            });
            this._tableColumns = [
                ...this.#systemColumns,
                ...userColumns,
                { name: '', alias: 'entityActions', align: 'right' },
            ];
        }
        else {
            this._tableColumns = [...this.#systemColumns, { name: '', alias: 'entityActions', align: 'right' }];
        }
    }
    #createTableItems() {
        this._tableItems = [];
        if (this._items === undefined)
            return;
        if (this._tableColumns.length === 0) {
            this.#createTableHeadings();
        }
        this._tableItems = this._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-entity-actions-table-column-view
								.value=${{
                            entityType: item.entityType,
                            unique: item.unique,
                            name: item.name,
                        }}></umb-entity-actions-table-column-view>`,
                    };
                }
                const editPath = UMB_EDIT_MEDIA_WORKSPACE_PATH_PATTERN.generateAbsolute({
                    unique: item.unique,
                });
                return {
                    columnAlias: column.alias,
                    value: column.elementName ? { item, editPath } : this.#getPropertyValueByAlias(item, column.alias),
                };
            }) ?? [];
            return {
                id: item.unique,
                icon: item.icon,
                entityType: 'media',
                data: data,
            };
        });
    }
    #getPropertyValueByAlias(item, alias) {
        switch (alias) {
            case 'contentTypeAlias':
                return item.contentTypeAlias;
            case 'createDate':
                return item.createDate.toLocaleString();
            case 'name':
                return item.name;
            case 'creator':
            case 'owner':
                return item.creator;
            case 'sortOrder':
                return item.sortOrder;
            case 'updateDate':
                return item.updateDate.toLocaleString();
            case 'updater':
                return item.updater;
            default:
                return item.values?.find((value) => value.alias === alias)?.value ?? '';
        }
    }
    #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%;
			}

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