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 { customElement, html, property, state } from '../../../external/lit/index.js';
import { UmbLitElement } from '../../core/lit-element/index.js';
import { UMB_DOCUMENT_COLLECTION_ALIAS } from '../../documents/documents/index.js';
import { UMB_PROPERTY_CONTEXT } from '../../core/property/index.js';
import { UMB_CONTENT_COLLECTION_WORKSPACE_CONTEXT } from '../../content/content/index.js';
/**
 * @element umb-property-editor-ui-collection
 */
let UmbPropertyEditorUICollectionElement = class UmbPropertyEditorUICollectionElement extends UmbLitElement {
    #workspaceContext;
    #propertyContext;
    set config(config) {
        this._config = this.#mapDataTypeConfigToCollectionConfig(config);
    }
    constructor() {
        super();
        this._collectionAlias = UMB_DOCUMENT_COLLECTION_ALIAS;
        this.consumeContext(UMB_CONTENT_COLLECTION_WORKSPACE_CONTEXT, (workspaceContext) => {
            this.#workspaceContext = workspaceContext;
            this._collectionAlias = workspaceContext?.collection.getCollectionAlias() ?? UMB_DOCUMENT_COLLECTION_ALIAS;
            this.#gotContexts();
        });
        this.consumeContext(UMB_PROPERTY_CONTEXT, (propertyContext) => {
            this.#propertyContext = propertyContext;
            this.#gotContexts();
        });
    }
    #gotContexts() {
        if (!this.#workspaceContext || !this.#propertyContext)
            return;
        this.observe(this.#propertyContext?.alias, async (propertyAlias) => {
            if (this.#workspaceContext && propertyAlias) {
                // Gets the Data Type ID for the current property.
                const property = await this.#workspaceContext.structure.getPropertyStructureByAlias(propertyAlias);
                if (!this.#workspaceContext) {
                    // We got destroyed in the meantime.
                    return;
                }
                const unique = this.#workspaceContext.getUnique();
                if (unique && property && this._config) {
                    // TODO: Handle case where config might not be set when this executes during initialization, its not likely but it is fragile to assume this. [NL]
                    this._config.unique = unique;
                    this._config.dataTypeId = property.dataType.unique;
                    this.requestUpdate('_config');
                }
            }
        });
    }
    #mapDataTypeConfigToCollectionConfig(config) {
        const pageSize = Number(config?.getValueByAlias('pageSize'));
        return {
            layouts: config?.getValueByAlias('layouts'),
            orderBy: config?.getValueByAlias('orderBy') ?? 'updateDate',
            orderDirection: config?.getValueByAlias('orderDirection') ?? 'asc',
            pageSize: isNaN(pageSize) ? 50 : pageSize,
            userDefinedProperties: config?.getValueByAlias('includeProperties'),
        };
    }
    render() {
        if (!this._config?.unique || !this._config?.dataTypeId)
            return html `<uui-loader></uui-loader>`;
        return html `<umb-collection .alias=${this._collectionAlias} .config=${this._config}></umb-collection>`;
    }
    destroy() {
        super.destroy();
        this.#workspaceContext = undefined;
        this.#propertyContext = undefined;
    }
};
__decorate([
    property()
], UmbPropertyEditorUICollectionElement.prototype, "value", void 0);
__decorate([
    state()
], UmbPropertyEditorUICollectionElement.prototype, "_collectionAlias", void 0);
__decorate([
    state()
], UmbPropertyEditorUICollectionElement.prototype, "_config", void 0);
UmbPropertyEditorUICollectionElement = __decorate([
    customElement('umb-property-editor-ui-collection')
], UmbPropertyEditorUICollectionElement);
export { UmbPropertyEditorUICollectionElement };
export default UmbPropertyEditorUICollectionElement;
