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_DATA_TYPE_PICKER_FLOW_MODAL, UMB_DATATYPE_WORKSPACE_MODAL } from '../../constants.js';
import { css, html, customElement, property, state } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../../core/lit-element/index.js';
import { UmbModalRouteRegistrationController } from '../../../core/router/index.js';
import { UmbChangeEvent } from '../../../core/event/index.js';
import { UMB_VALIDATION_EMPTY_LOCALIZATION_KEY, UmbFormControlMixin } from '../../../core/validation/index.js';
// Note: Does only support picking a single data type. But this could be developed later into this same component. To follow other picker input components.
/**
 * Form control for picking or creating a data type.
 * @element umb-data-type-flow-input
 * @fires change - when the value of the input changes
 * @fires blur - when the input loses focus
 * @fires focus - when the input gains focus
 */
let UmbInputDataTypeElement = class UmbInputDataTypeElement extends UmbFormControlMixin(UmbLitElement, '') {
    getFormElement() {
        return undefined;
    }
    /**
     * @param {string} dataTypeId
     * @default
     */
    set value(dataTypeId) {
        super.value = dataTypeId ?? '';
        this._ids = super.value
            .split(',')
            .map((tag) => tag.trim())
            .filter((id) => id.length !== 0);
    }
    get value() {
        return super.value?.toString() ?? '';
    }
    #editDataTypeModal;
    constructor() {
        super();
        this.#editDataTypeModal = new UmbModalRouteRegistrationController(this, UMB_DATATYPE_WORKSPACE_MODAL);
        new UmbModalRouteRegistrationController(this, UMB_DATA_TYPE_PICKER_FLOW_MODAL)
            .onSetup(() => {
            return {
                data: {},
                value: { selection: this._ids ?? [] },
            };
        })
            .onSubmit((submitData) => {
            // TODO: we maybe should set the alias to null, if no selection?
            this.value = submitData?.selection.join(',') ?? '';
            this.dispatchEvent(new UmbChangeEvent());
        })
            .observeRouteBuilder((routeBuilder) => {
            this._createRoute = routeBuilder(null);
        });
    }
    firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
        this.addValidator('valueMissing', () => UMB_VALIDATION_EMPTY_LOCALIZATION_KEY, () => this.hasAttribute('required') && !this.value);
    }
    focus() {
        this.shadowRoot?.querySelector('umb-ref-data-type')?.focus();
    }
    render() {
        return this._ids && this._ids.length > 0
            ? html `
					<umb-ref-data-type
						data-type-id=${this._ids[0]}
						@open=${() => {
                // TODO: Could use something smarter for workspace modals, as I would like to avoid setting the rest of the URL here:
                this.#editDataTypeModal?.open({}, 'edit/' + this._ids[0]);
            }}
						standalone>
						<uui-action-bar slot="actions">
							<uui-button label="Change" .href=${this._createRoute}></uui-button>
						</uui-action-bar>
					</umb-ref-data-type>
				`
            : html `
					<uui-button
						id="empty-state-button"
						label="Select Property Editor"
						look="placeholder"
						color="default"
						@blur=${() => {
                this.pristine = false;
            }}
						.href=${this._createRoute}></uui-button>
				`;
    }
    static { this.styles = [
        css `
			#empty-state-button {
				width: 100%;
				--uui-button-padding-top-factor: 4;
				--uui-button-padding-bottom-factor: 4;
			}
			:host(:invalid:not([pristine])) #empty-state-button {
				--uui-button-border-color: var(--uui-color-invalid);
				--uui-button-border-width: 2px;
				--uui-button-contrast: var(--uui-color-invalid);
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbInputDataTypeElement.prototype, "_ids", void 0);
__decorate([
    property({ type: String, attribute: false })
], UmbInputDataTypeElement.prototype, "value", null);
__decorate([
    state()
], UmbInputDataTypeElement.prototype, "_createRoute", void 0);
UmbInputDataTypeElement = __decorate([
    customElement('umb-data-type-flow-input')
], UmbInputDataTypeElement);
export { UmbInputDataTypeElement };
