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, css, state } from '../../../../../external/lit/index.js';
import { UmbTextStyles } from '../../../style/index.js';
import { UmbModalBaseElement } from '../../../modal/index.js';
import { UmbExtensionApiInitializer } from '../../../../../libs/extension-api/index.js';
import { umbExtensionsRegistry } from '../../../extension-registry/index.js';
import { umbFocus } from '../../../lit-element/index.js';
let UmbRenameModalElement = class UmbRenameModalElement extends UmbModalBaseElement {
    constructor() {
        super(...arguments);
        this._name = '';
    }
    // TODO: make base type for item and detail models
    #itemRepository;
    #renameRepository;
    #init;
    connectedCallback() {
        super.connectedCallback();
        this.#observeRepository();
    }
    #observeRepository() {
        if (!this.data?.renameRepositoryAlias)
            throw new Error('A rename repository alias is required');
        if (!this.data?.itemRepositoryAlias)
            throw new Error('An item repository alias is required');
        // TODO: We should properly look into how we can simplify the one time usage of a extension api, as its a bit of overkill to take conditions/overwrites and observation of extensions into play here: [NL]
        // But since this happens when we execute an action, it does most likely not hurt any users, but it is a bit of a overkill to do this for every action: [NL]
        this.#init = Promise.all([
            new UmbExtensionApiInitializer(this, umbExtensionsRegistry, this.data.itemRepositoryAlias, [this], (permitted, ctrl) => {
                this.#itemRepository = permitted ? ctrl.api : undefined;
            }).asPromise(),
            new UmbExtensionApiInitializer(this, umbExtensionsRegistry, this.data.renameRepositoryAlias, [this], (permitted, ctrl) => {
                this.#renameRepository = permitted ? ctrl.api : undefined;
            }).asPromise(),
        ]);
    }
    async firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
        if (!this.data?.unique)
            throw new Error('Unique identifier is not available');
        await this.#init;
        if (!this.#itemRepository)
            throw new Error('Item repository is not available');
        const { data } = await this.#itemRepository.requestItems([this.data.unique]);
        this._name = data?.[0].name ?? '';
    }
    async #onSubmit(event) {
        event.preventDefault();
        if (!this.#renameRepository)
            throw new Error('Rename repository is not available');
        if (!this.data?.unique)
            throw new Error('Unique identifier is not available');
        const form = event.target;
        if (!form)
            return;
        const isValid = form.checkValidity();
        if (!isValid)
            return;
        const formData = new FormData(form);
        const name = formData.get('name');
        const { data } = await this.#renameRepository.rename(this.data.unique, name);
        if (data) {
            this.value = {
                name: data.name,
                unique: data.unique,
            };
            this._submitModal();
        }
        else {
            this._rejectModal();
        }
    }
    render() {
        return html `
			<umb-body-layout headline=${'Rename'}>
				<uui-box>
					<uui-form>
						<form id="RenameForm" @submit="${this.#onSubmit}">
							<uui-form-layout-item>
								<uui-label id="nameLabel" for="name" slot="label" required>Name</uui-label>
								<uui-input
									type="text"
									id="name"
									name="name"
									value=${this._name}
									placeholder="Enter new name..."
									required
									required-message="Name is required"
									${umbFocus()}></uui-input>
							</uui-form-layout-item>
						</form>
					</uui-form>
				</uui-box>

				<uui-button slot="actions" id="cancel" label="Cancel" @click="${this._rejectModal}"></uui-button>
				<uui-button
					form="RenameForm"
					type="submit"
					slot="actions"
					color="positive"
					look="primary"
					label="Rename"></uui-button>
			</umb-body-layout>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			#name {
				width: 100%;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbRenameModalElement.prototype, "_name", void 0);
UmbRenameModalElement = __decorate([
    customElement('umb-rename-modal')
], UmbRenameModalElement);
export { UmbRenameModalElement };
export default UmbRenameModalElement;
