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_PROPERTY_VALUE_ENTITY_TYPE } from '../../../../entity.js';
import { html, customElement, state, repeat } from '../../../../../../../external/lit/index.js';
import { UMB_MODAL_MANAGER_CONTEXT, UmbModalBaseElement } from '../../../../../../core/modal/index.js';
import { UmbDocumentTypeDetailRepository } from '../../../../../document-types/index.js';
import { UMB_ENTITY_USER_PERMISSION_MODAL } from '../../../../../../user/user-permission/index.js';
let UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement = class UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement extends UmbModalBaseElement {
    constructor() {
        super(...arguments);
        this._documentTypeProperties = [];
        this._selectedItem = null;
        this._pickableFilter = () => true;
        this.#detailRepository = new UmbDocumentTypeDetailRepository(this);
    }
    #detailRepository;
    #onItemSelected(event, item) {
        event.stopPropagation();
        this._selectedItem = item;
    }
    #onItemDeselected(event) {
        event.stopPropagation();
        this._selectedItem = null;
    }
    async firstUpdated() {
        if (!this.data?.documentType?.unique) {
            throw new Error('Document type unique is required');
        }
        this._pickableFilter = this.data.pickableFilter ?? this._pickableFilter;
        const { data } = await this.#detailRepository.requestByUnique(this.data.documentType.unique);
        this._documentTypeProperties = data?.properties ?? [];
        this._documentTypeName = data?.name;
    }
    #getItemDetail(item) {
        const isMandatory = item.validation?.mandatory ? ' - Mandatory' : '';
        const variesByCulture = item.variesByCulture ? ' - Varies by culture' : '';
        const variesBySegment = item.variesBySegment ? ' - Varies by segment' : '';
        return `${item.alias} ${isMandatory} ${variesByCulture} ${variesBySegment}`;
    }
    #next() {
        if (!this._selectedItem) {
            throw new Error('Could not proceed, no property was selected');
        }
        this.#selectEntityUserPermissionsForProperty();
    }
    async #selectEntityUserPermissionsForProperty() {
        if (!this._selectedItem) {
            throw new Error('Could not open permissions modal, no property was provided');
        }
        const headline = `Permissions for ${this._documentTypeName}: ${this._selectedItem.name}`;
        const modalManager = await this.getContext(UMB_MODAL_MANAGER_CONTEXT);
        if (!modalManager) {
            throw new Error('Could not open permissions modal, modal manager is not available');
        }
        const modal = modalManager.open(this, UMB_ENTITY_USER_PERMISSION_MODAL, {
            data: {
                entityType: UMB_DOCUMENT_PROPERTY_VALUE_ENTITY_TYPE,
                headline,
                preset: {
                    allowedVerbs: this.data?.preset?.verbs ?? [],
                },
            },
        });
        try {
            const value = await modal.onSubmit();
            this.updateValue({
                propertyType: { unique: this._selectedItem.unique },
                verbs: value.allowedVerbs,
            });
            this._submitModal();
        }
        catch (error) {
            console.error(error);
        }
    }
    render() {
        return html `<umb-body-layout headline="Choose Property">
			<uui-box>
				${this._documentTypeProperties.length > 0
            ? repeat(this._documentTypeProperties, (item) => item.unique, (item) => html `
								<uui-ref-node
									name=${this.localize.string(item.name ?? '')}
									detail=${this.#getItemDetail(item)}
									?selectable=${this._pickableFilter(item)}
									select-only
									@selected=${(event) => this.#onItemSelected(event, item)}
									@deselected=${(event) => this.#onItemDeselected(event)}
									?selected=${this._selectedItem?.unique === item.unique}
									?disabled=${!this._pickableFilter(item)}>
									<uui-icon slot="icon" name="icon-settings"></uui-icon>
								</uui-ref-node>
							`)
            : html `There are no properties to choose from.`}
			</uui-box>
			<div slot="actions">
				<uui-button label="Close" @click=${this._rejectModal}></uui-button>
				<uui-button
					label="${this.localize.term('general_next')}"
					look="primary"
					color="positive"
					@click=${this.#next}
					?disabled=${!this._selectedItem}></uui-button>
			</div>
		</umb-body-layout> `;
    }
};
__decorate([
    state()
], UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement.prototype, "_documentTypeProperties", void 0);
__decorate([
    state()
], UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement.prototype, "_documentTypeName", void 0);
__decorate([
    state()
], UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement.prototype, "_selectedItem", void 0);
__decorate([
    state()
], UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement.prototype, "_pickableFilter", void 0);
UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement = __decorate([
    customElement('umb-document-property-value-user-permission-flow-property-type-modal')
], UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement);
export { UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement };
export { UmbDocumentPropertyValueUserPermissionFlowPropertyTypeModalElement as element };
