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 { UmbActionExecutedEvent } from '../../event/index.js';
import { html, customElement, property, state } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../lit-element/index.js';
import { createExtensionApi } from '../../../../libs/extension-api/index.js';
let UmbCollectionActionButtonElement = class UmbCollectionActionButtonElement extends UmbLitElement {
    #api;
    get manifest() {
        return this._manifest;
    }
    set manifest(value) {
        const oldValue = this._manifest;
        if (oldValue !== value) {
            this._manifest = value;
            this._href = this.manifest?.meta.href;
            this._additionalOptions = this.manifest?.meta.additionalOptions;
            this.#createApi();
            this.requestUpdate('manifest', oldValue);
        }
    }
    async #createApi() {
        if (!this._manifest)
            throw new Error('No manifest defined');
        if (!this._manifest.api)
            return;
        this.#api = await createExtensionApi(this, this._manifest, [this._manifest]);
        this._href = (await this.#api?.getHref?.()) ?? this.manifest?.meta.href;
        this._additionalOptions = (await this.#api?.hasAddionalOptions?.()) ?? this.manifest?.meta.additionalOptions;
    }
    async _onClick() {
        // If its a link or has additional options, then we do not want to display state on the button. [NL]
        if (!this._href) {
            if (!this._additionalOptions) {
                this._buttonState = 'waiting';
            }
            try {
                if (!this.#api)
                    throw new Error('No api defined');
                await this.#api.execute();
                if (!this._additionalOptions) {
                    this._buttonState = 'success';
                }
            }
            catch {
                if (!this._additionalOptions) {
                    this._buttonState = 'failed';
                }
            }
        }
        this.dispatchEvent(new UmbActionExecutedEvent());
    }
    render() {
        const label = this.manifest?.meta.label
            ? this.localize.string(this.manifest.meta.label)
            : (this.manifest?.name ?? '');
        return html `
			<uui-button
				data-mark="collection-action:${this.manifest?.alias}"
				color="default"
				look="outline"
				.label=${this._additionalOptions ? label + '…' : label}
				.href=${this._href}
				.state=${this._buttonState}
				@click=${this._onClick}></uui-button>
		`;
    }
};
__decorate([
    property({ type: Object, attribute: false })
], UmbCollectionActionButtonElement.prototype, "manifest", null);
__decorate([
    state()
], UmbCollectionActionButtonElement.prototype, "_buttonState", void 0);
__decorate([
    state()
], UmbCollectionActionButtonElement.prototype, "_additionalOptions", void 0);
__decorate([
    state()
], UmbCollectionActionButtonElement.prototype, "_href", void 0);
UmbCollectionActionButtonElement = __decorate([
    customElement('umb-collection-action-button')
], UmbCollectionActionButtonElement);
export { UmbCollectionActionButtonElement };
export default UmbCollectionActionButtonElement;
