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 { html, customElement, property, css } from '../../../../../../external/lit/index.js';
import { UUIFormControlMixin } from '../../../../../../external/uui/index.js';
import { UmbLitElement } from '../../../../../core/lit-element/index.js';
import { UmbChangeEvent, UmbSelectionChangeEvent } from '../../../../../core/event/index.js';
import { UmbMediaTypeDetailRepository, UMB_MEDIA_TYPE_PICKER_MODAL } from '../../../../../media/media-types/index.js';
import { UMB_DOCUMENT_TYPE_PICKER_MODAL, UmbDocumentTypeDetailRepository } from '../../../../../documents/document-types/index.js';
import { UMB_MEMBER_TYPE_PICKER_MODAL, UmbMemberTypeDetailRepository } from '../../../../../members/member-type/index.js';
import { UMB_ITEM_PICKER_MODAL, UMB_MODAL_MANAGER_CONTEXT } from '../../../../../core/modal/index.js';
let UmbInputCollectionContentTypePropertyElement = class UmbInputCollectionContentTypePropertyElement extends UUIFormControlMixin(UmbLitElement, undefined) {
    #configuration;
    #modalManager;
    getFormElement() {
        return undefined;
    }
    constructor() {
        super();
        this.#configuration = {
            documentTypes: {
                item: {
                    label: this.localize.term('content_documentType'),
                    description: this.localize.term('defaultdialogs_selectContentType'),
                    value: 'documentTypes',
                    icon: 'icon-document',
                },
                pickerModal: () => UMB_DOCUMENT_TYPE_PICKER_MODAL,
                pickableFilter: (docType) => !docType.isElement,
                repository: () => new UmbDocumentTypeDetailRepository(this),
                systemProperties: [
                    {
                        label: this.localize.term('content_documentType'),
                        description: 'contentTypeAlias',
                        value: 'contentTypeAlias',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_createDate'),
                        description: 'createDate',
                        value: 'createDate',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_createBy'),
                        description: 'creator',
                        value: 'creator',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_isPublished'),
                        description: 'published',
                        value: 'published',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('general_sort'),
                        description: 'sortOrder',
                        value: 'sortOrder',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_updateDate'),
                        description: 'updateDate',
                        value: 'updateDate',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_updatedBy'),
                        description: 'updater',
                        value: 'updater',
                        icon: 'icon-settings',
                    },
                ],
            },
            elementTypes: {
                item: {
                    label: this.localize.term('create_elementType'),
                    description: this.localize.term('content_nestedContentSelectElementTypeModalTitle'),
                    value: 'elementTypes',
                    icon: 'icon-plugin',
                },
                pickerModal: () => UMB_DOCUMENT_TYPE_PICKER_MODAL,
                pickableFilter: (docType) => docType.isElement,
                repository: () => new UmbDocumentTypeDetailRepository(this),
                systemProperties: [
                    {
                        label: this.localize.term('content_documentType'),
                        description: 'contentTypeAlias',
                        value: 'contentTypeAlias',
                    },
                ],
            },
            mediaTypes: {
                item: {
                    label: this.localize.term('content_mediatype'),
                    description: this.localize.term('defaultdialogs_selectMediaType'),
                    value: 'mediaTypes',
                    icon: 'icon-picture',
                },
                pickerModal: () => UMB_MEDIA_TYPE_PICKER_MODAL,
                repository: () => new UmbMediaTypeDetailRepository(this),
                systemProperties: [
                    {
                        label: this.localize.term('content_documentType'),
                        description: 'contentTypeAlias',
                        value: 'contentTypeAlias',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_createDate'),
                        description: 'createDate',
                        value: 'createDate',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_createBy'),
                        description: 'creator',
                        value: 'creator',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('general_sort'),
                        description: 'sortOrder',
                        value: 'sortOrder',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_updateDate'),
                        description: 'updateDate',
                        value: 'updateDate',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_updatedBy'),
                        description: 'updater',
                        value: 'updater',
                        icon: 'icon-settings',
                    },
                ],
            },
            memberTypes: {
                item: {
                    label: this.localize.term('content_membertype'),
                    description: this.localize.term('defaultdialogs_selectMemberType'),
                    value: 'memberTypes',
                    icon: 'icon-user',
                },
                pickerModal: () => UMB_MEMBER_TYPE_PICKER_MODAL,
                repository: () => new UmbMemberTypeDetailRepository(this),
                systemProperties: [
                    {
                        label: this.localize.term('content_documentType'),
                        description: 'contentTypeAlias',
                        value: 'contentTypeAlias',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('content_createDate'),
                        description: 'createDate',
                        value: 'createDate',
                        icon: 'icon-settings',
                    },
                    { label: this.localize.term('general_email'), description: 'email', value: 'email', icon: 'icon-settings' },
                    {
                        label: this.localize.term('content_updateDate'),
                        description: 'updateDate',
                        value: 'updateDate',
                        icon: 'icon-settings',
                    },
                    {
                        label: this.localize.term('general_username'),
                        description: 'username',
                        value: 'username',
                        icon: 'icon-settings',
                    },
                ],
            },
        };
        this.documentTypes = false;
        this.elementTypes = false;
        this.mediaTypes = false;
        this.memberTypes = false;
        this.consumeContext(UMB_MODAL_MANAGER_CONTEXT, (modalManager) => {
            this.#modalManager = modalManager;
        });
    }
    async #onClick() {
        const items = [];
        if (this.documentTypes) {
            items.push(this.#configuration['documentTypes'].item);
        }
        if (this.elementTypes) {
            items.push(this.#configuration['elementTypes'].item);
        }
        if (this.mediaTypes) {
            items.push(this.#configuration['mediaTypes'].item);
        }
        if (this.memberTypes) {
            items.push(this.#configuration['memberTypes'].item);
        }
        if (items.length === 1) {
            // If there is only one item, we can skip the modal and go directly to the picker.
            this.#openContentTypePicker(items[0].value);
            return;
        }
        if (!this.#modalManager)
            return;
        const modalContext = this.#modalManager.open(this, UMB_ITEM_PICKER_MODAL, {
            data: {
                headline: this.localize.term('defaultdialogs_selectContentType'),
                items: items,
            },
        });
        const modalValue = await modalContext.onSubmit();
        if (!modalValue)
            return;
        const configKey = modalValue.value;
        this.#openContentTypePicker(configKey);
    }
    async #openContentTypePicker(configKey) {
        const config = this.#configuration[configKey];
        if (!config)
            return;
        const pickerModal = config.pickerModal();
        const pickerContext = this.#modalManager?.open(this, pickerModal, {
            data: {
                hideTreeRoot: true,
                multiple: false,
                pickableFilter: config.pickableFilter,
            },
        });
        // NOTE: We listen for the selection change event to submit the picker.
        // This is to enforce a single selection and progress to the next modal.
        pickerContext?.addEventListener(UmbSelectionChangeEvent.TYPE, pickerContext.submit);
        const pickerValue = await pickerContext?.onSubmit();
        const selectedItems = pickerValue?.selection ?? [];
        if (selectedItems.length === 0)
            return;
        const repository = config.repository();
        const { data } = await repository.requestByUnique(selectedItems[0] ?? '');
        if (!data)
            return;
        this.#openPropertyPicker(data, config);
    }
    async #openPropertyPicker(contentType, config) {
        if (!contentType)
            return;
        if (!this.#modalManager)
            return;
        const properties = contentType?.properties.map((property) => ({
            label: property.name,
            value: property.alias,
            description: property.alias,
            icon: config.item.icon,
        })) ?? [];
        const items = [...(config.systemProperties ?? []), ...properties];
        const modalContext = this.#modalManager.open(this, UMB_ITEM_PICKER_MODAL, {
            data: {
                headline: `Select a property from ${contentType.name}`,
                items: items,
            },
        });
        const modalValue = await modalContext.onSubmit();
        if (!modalValue)
            return;
        this.selectedProperty = {
            label: modalValue.label,
            alias: modalValue.value,
            isSystem: config.systemProperties?.some((property) => property.value === modalValue.value) ?? false,
        };
        this.dispatchEvent(new UmbChangeEvent());
    }
    render() {
        return html `<uui-button
			label=${this.localize.term('general_choose')}
			look="placeholder"
			color="default"
			@click=${this.#onClick}></uui-button>`;
    }
    static { this.styles = [
        css `
			:host {
				display: flex;
				flex-direction: column;
				gap: var(--uui-size-space-1);
			}
		`,
    ]; }
};
__decorate([
    property({ type: Boolean, attribute: 'document-types' })
], UmbInputCollectionContentTypePropertyElement.prototype, "documentTypes", void 0);
__decorate([
    property({ type: Boolean, attribute: 'element-types' })
], UmbInputCollectionContentTypePropertyElement.prototype, "elementTypes", void 0);
__decorate([
    property({ type: Boolean, attribute: 'media-types' })
], UmbInputCollectionContentTypePropertyElement.prototype, "mediaTypes", void 0);
__decorate([
    property({ type: Boolean, attribute: 'member-types' })
], UmbInputCollectionContentTypePropertyElement.prototype, "memberTypes", void 0);
UmbInputCollectionContentTypePropertyElement = __decorate([
    customElement('umb-input-collection-content-type-property')
], UmbInputCollectionContentTypePropertyElement);
export { UmbInputCollectionContentTypePropertyElement };
export default UmbInputCollectionContentTypePropertyElement;
