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 { UmbRefItemElement } from '../../../../components/index.js';
import { UmbExtensionsApiInitializer } from '../../../../../../libs/extension-api/index.js';
import { umbExtensionsRegistry } from '../../../../extension-registry/index.js';
import { html, customElement, state, repeat, ifDefined, css, } from '../../../../../../external/lit/index.js';
import { UmbModalBaseElement } from '../../../../modal/index.js';
let UmbEntityCreateOptionActionListModalElement = class UmbEntityCreateOptionActionListModalElement extends UmbModalBaseElement {
    constructor() {
        super(...arguments);
        this._apiControllers = [];
        this._hrefList = [];
    }
    updated(_changedProperties) {
        super.updated(_changedProperties);
        if (_changedProperties.has('data')) {
            this.#initApi();
        }
    }
    #initApi() {
        const data = this.data;
        if (!data)
            throw new Error('No data found');
        if (!data.entityType)
            throw new Error('No entityType found');
        if (data.unique === undefined)
            throw new Error('No unique found');
        new UmbExtensionsApiInitializer(this, umbExtensionsRegistry, 'entityCreateOptionAction', (manifest) => {
            return [{ entityType: data.entityType, unique: data.unique, meta: manifest.meta }];
        }, (manifest) => manifest.forEntityTypes.includes(data.entityType), async (controllers) => {
            this._apiControllers = controllers;
            const hrefPromises = this._apiControllers.map((controller) => controller.api?.getHref());
            this._hrefList = await Promise.all(hrefPromises);
        });
    }
    async #onOpen(event, controller) {
        event.stopPropagation();
        if (!controller.api) {
            throw new Error('No API found');
        }
        controller.api
            .execute()
            .then(() => {
            this._submitModal();
        })
            .catch(() => { });
    }
    async #onNavigate(event, href) {
        const refItemElement = event.composedPath().find((x) => x instanceof UmbRefItemElement);
        // ignore click events if they are not on a ref item
        if (!refItemElement) {
            return;
        }
        if (!href) {
            throw new Error('No href found');
        }
        this._submitModal();
    }
    #getTarget(href) {
        if (href && href.startsWith('http')) {
            return '_blank';
        }
        return '_self';
    }
    render() {
        return html `
			<uui-dialog-layout headline="${this.localize.term('general_create')}">
				${this._apiControllers.length === 0
            ? html `<div>No create options available.</div>`
            : html `
							<uui-ref-list>
								${repeat(this._apiControllers, (controller) => controller.manifest?.alias, (controller, index) => this.#renderRefItem(controller, index))}
							</uui-ref-list>
						`}
				<uui-button
					slot="actions"
					label=${this.localize.term('general_cancel')}
					@click=${this._rejectModal}></uui-button>
			</uui-dialog-layout>
		`;
    }
    #renderRefItem(controller, index) {
        const manifest = controller.manifest;
        if (!manifest)
            throw new Error('No manifest found');
        const label = manifest.meta.label ? this.localize.string(manifest.meta.label) : manifest.name;
        const description = manifest.meta.description ? this.localize.string(manifest.meta.description) : undefined;
        const href = this._hrefList[index];
        return html `
			<umb-ref-item
				name=${manifest.meta.additionalOptions ? label + '...' : label}
				detail=${ifDefined(description)}
				icon=${manifest.meta.icon}
				href=${ifDefined(href)}
				target=${this.#getTarget(href)}
				@open=${async (event) => await this.#onOpen(event, controller).catch(() => undefined)}
				@click=${(event) => this.#onNavigate(event, href)}>
			</umb-ref-item>
		`;
    }
    static { this.styles = css `
		:host {
			max-width: 800px;
		}
	`; }
};
__decorate([
    state()
], UmbEntityCreateOptionActionListModalElement.prototype, "_apiControllers", void 0);
__decorate([
    state()
], UmbEntityCreateOptionActionListModalElement.prototype, "_hrefList", void 0);
UmbEntityCreateOptionActionListModalElement = __decorate([
    customElement('umb-entity-create-option-action-list-modal')
], UmbEntityCreateOptionActionListModalElement);
export { UmbEntityCreateOptionActionListModalElement };
export { UmbEntityCreateOptionActionListModalElement as element };
