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, ifDefined, repeat, state } from '../../../../../external/lit/index.js';
import { UmbDocumentPickerInputContext } from '../../../../documents/documents/index.js';
import { UmbModalBaseElement } from '../../../../core/modal/index.js';
let UmbDynamicRootOriginPickerModalModalElement = class UmbDynamicRootOriginPickerModalModalElement extends UmbModalBaseElement {
    #documentPickerContext;
    constructor() {
        super();
        this._origins = [];
        this.#documentPickerContext = new UmbDocumentPickerInputContext(this);
        this.#documentPickerContext.max = 1;
    }
    connectedCallback() {
        super.connectedCallback();
        if (this.data) {
            this._origins = this.data.items;
        }
    }
    #choose(item) {
        switch (item.meta.originAlias) {
            // NOTE: Edge-case. Currently this is the only one that uses a document picker,
            // but other custom origins may want other configuration options. [LK:2024-01-25]
            case 'ByKey':
                this.#openDocumentPicker(item.meta.originAlias);
                break;
            default:
                this.#submit({ originAlias: item.meta.originAlias });
                break;
        }
    }
    #close() {
        this.modalContext?.reject();
    }
    async #openDocumentPicker(originAlias) {
        await this.#documentPickerContext.openPicker({
            hideTreeRoot: true,
        });
        const selectedItems = this.#documentPickerContext.getSelection();
        if (selectedItems.length !== 1)
            return;
        this.#submit({
            originAlias,
            originKey: selectedItems[0],
        });
    }
    #submit(value) {
        this.modalContext?.setValue(value);
        this.modalContext?.submit();
    }
    render() {
        return html `
			<umb-body-layout headline=${this.localize.term('dynamicRoot_pickDynamicRootOriginTitle')}>
				<div id="main">
					<uui-box>
						<uui-ref-list>
							${repeat(this._origins, (item) => item.alias, (item) => html `
									<umb-ref-item
										name=${ifDefined(item.meta.label)}
										detail=${ifDefined(item.meta.description)}
										icon=${ifDefined(item.meta.icon)}
										@open=${() => this.#choose(item)}>
									</umb-ref-item>
								`)}
						</uui-ref-list>
					</uui-box>
				</div>
				<div slot="actions">
					<uui-button look="default" label=${this.localize.term('general_close')} @click=${this.#close}></uui-button>
				</div>
			</umb-body-layout>
		`;
    }
};
__decorate([
    state()
], UmbDynamicRootOriginPickerModalModalElement.prototype, "_origins", void 0);
UmbDynamicRootOriginPickerModalModalElement = __decorate([
    customElement('umb-dynamic-root-origin-picker-modal')
], UmbDynamicRootOriginPickerModalModalElement);
export { UmbDynamicRootOriginPickerModalModalElement };
export default UmbDynamicRootOriginPickerModalModalElement;
