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, nothing, unsafeHTML, } from '../../../../../../external/lit/index.js';
import { UmbTextStyles } from '../../../../../core/style/index.js';
import { UmbModalBaseElement } from '../../../../../core/modal/index.js';
import { umbFocus } from '../../../../../core/lit-element/index.js';
import { createExtensionApiByAlias } from '../../../../../core/extension-registry/index.js';
let UmbTrashWithRelationConfirmModalElement = class UmbTrashWithRelationConfirmModalElement extends UmbModalBaseElement {
    constructor() {
        super(...arguments);
        // Three-state model for reference-aware trashing:
        //   undefined = loading (button disabled, no message yet)
        //   false     = blocked (button disabled, "cannot trash" message)
        //   true      = allowed (button enabled, normal confirmation message)
        this._canTrash = undefined;
    }
    #itemRepository;
    firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
        this.#initData();
    }
    async #initData() {
        if (!this.data) {
            this.#itemRepository?.destroy();
            return;
        }
        this.#itemRepository = await createExtensionApiByAlias(this, this.data.itemRepositoryAlias);
        const { data } = await this.#itemRepository.requestItems([this.data.unique]);
        const item = data?.[0];
        if (!item)
            throw new Error('Item not found.');
        if (this.data.itemDataResolver) {
            const resolver = new this.data.itemDataResolver(this);
            resolver.setData(item);
            this._name = await resolver.getName();
        }
        else {
            this._name = item.name;
        }
        // If disableDeleteWhenReferenced is not set, allow trashing immediately.
        // Otherwise stay in loading state until the references component reports totals.
        if (!this.data.disableDeleteWhenReferenced) {
            this._canTrash = true;
        }
        this._referencesConfig = {
            unique: this.data.unique,
            itemRepositoryAlias: this.data.itemRepositoryAlias,
            referenceRepositoryAlias: this.data.referenceRepositoryAlias,
        };
    }
    #onReferencesChange(event) {
        event.stopPropagation();
        if (this._canTrash !== undefined)
            return;
        const target = event.target;
        const total = target.getTotalReferencedBy() + target.getTotalDescendantsWithReferences();
        this._canTrash = total === 0;
    }
    render() {
        const headline = this.localize.string('#actions_trash');
        return html `
			<uui-dialog-layout class="uui-text" headline=${headline}>
				${this._canTrash !== undefined
            ? html `<p>${unsafeHTML(this._canTrash === false
                ? this.localize.string('#defaultdialogs_cannotTrashWhenReferenced', this._name)
                : this.localize.string('#defaultdialogs_confirmTrash', this._name))}</p>`
            : nothing}

				${this._referencesConfig
            ? html `<umb-confirm-action-modal-entity-references
							.config=${this._referencesConfig}
							@change=${this.#onReferencesChange}></umb-confirm-action-modal-entity-references>`
            : nothing}

				<uui-button slot="actions" id="cancel" label="Cancel" @click=${this._rejectModal}></uui-button>

				<uui-button
					slot="actions"
					id="confirm"
					color="danger"
					look="primary"
					label=${this.localize.term('actions_trash')}
					?disabled=${!this._canTrash}
					@click=${this._submitModal}
					${umbFocus()}></uui-button>
			</uui-dialog-layout>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			uui-dialog-layout {
				max-inline-size: 60ch;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbTrashWithRelationConfirmModalElement.prototype, "_name", void 0);
__decorate([
    state()
], UmbTrashWithRelationConfirmModalElement.prototype, "_referencesConfig", void 0);
__decorate([
    state()
], UmbTrashWithRelationConfirmModalElement.prototype, "_canTrash", void 0);
UmbTrashWithRelationConfirmModalElement = __decorate([
    customElement('umb-trash-with-relation-confirm-modal')
], UmbTrashWithRelationConfirmModalElement);
export { UmbTrashWithRelationConfirmModalElement };
export { UmbTrashWithRelationConfirmModalElement as element };
