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 { UmbEntityContext, UMB_ENTITY_CONTEXT } from '../entity/index.js';
import { html, customElement, property, state, css } from '../../../external/lit/index.js';
import { UmbLitElement } from '../lit-element/index.js';
import { UmbDeprecation } from '../utils/index.js';
import { observeMultiple } from '../../../libs/observable-api/index.js';
const umbEntityActionListDeprecation = new UmbDeprecation({
    deprecated: 'The `entityType` and `unique` properties on `<umb-entity-action-list>`.',
    removeInVersion: '19',
    solution: 'Provide the entity type and unique via the UMB_ENTITY_CONTEXT context instead.',
});
let UmbEntityActionListElement = class UmbEntityActionListElement extends UmbLitElement {
    /**
     * @deprecated Provide through the UMB_ENTITY_CONTEXT context instead. Will be removed in Umbraco 19.
     * @returns {string | undefined} The entity type.
     */
    get entityType() {
        return this._props.entityType;
    }
    set entityType(value) {
        if (value === undefined || value === this._props.entityType)
            return;
        umbEntityActionListDeprecation.warn();
        this._props.entityType = value;
        this.#ensureFallbackEntityContext().setEntityType(value);
        this.#generateApiArgs();
    }
    /**
     * @deprecated Provide through the UMB_ENTITY_CONTEXT context instead. Will be removed in Umbraco 19.
     * @returns {string | null | undefined} The unique key.
     */
    get unique() {
        return this._props.unique;
    }
    set unique(value) {
        if (value === this._props.unique)
            return;
        umbEntityActionListDeprecation.warn();
        this._props.unique = value;
        this.#ensureFallbackEntityContext().setUnique(value ?? null);
        this.#generateApiArgs();
    }
    #fallbackEntityContext;
    constructor() {
        super();
        this._props = {};
        this.consumeContext(UMB_ENTITY_CONTEXT, (context) => {
            if (!context)
                return;
            this.observe(observeMultiple([context.entityType, context.unique]), ([entityType, unique]) => {
                this._props.entityType = entityType ?? undefined;
                this._props.unique = unique;
                this.#generateApiArgs();
            });
        });
    }
    // TODO: v19 remove when fallback context is no longer needed
    #ensureFallbackEntityContext() {
        if (!this.#fallbackEntityContext) {
            this.#fallbackEntityContext = new UmbEntityContext(this);
        }
        return this.#fallbackEntityContext;
    }
    #generateApiArgs() {
        if (!this._props.entityType || this._props.unique === undefined)
            return;
        this._filter = (extension) => extension.forEntityTypes.includes(this._props.entityType);
        this.#hasRenderedOnce = false;
        this._apiArgs = (manifest) => {
            return [{ entityType: this._props.entityType, unique: this._props.unique, meta: manifest.meta }];
        };
    }
    focus() {
        this.#firstEntityAction?.focus();
    }
    #firstEntityAction;
    #hasRenderedOnce;
    render() {
        return this._filter
            ? html `
					<umb-extension-with-api-slot
						type="entityAction"
						.filter=${this._filter}
						.elementProps=${this._props}
						.apiArgs=${this._apiArgs}
						.renderMethod=${(ext, i) => {
                if (!this.#hasRenderedOnce && i === 0) {
                    this.#firstEntityAction = ext.component;
                    this.#firstEntityAction?.focus();
                    this.#hasRenderedOnce = true;
                }
                return ext.component;
            }}></umb-extension-with-api-slot>
				`
            : '';
    }
    static { this.styles = [
        css `
			:host {
				--uui-menu-item-indent: 0;
				--uui-menu-item-flat-structure: 1;
			}
		`,
    ]; }
};
__decorate([
    property({ type: String, attribute: 'entity-type' })
], UmbEntityActionListElement.prototype, "entityType", null);
__decorate([
    property({ type: String })
], UmbEntityActionListElement.prototype, "unique", null);
__decorate([
    state()
], UmbEntityActionListElement.prototype, "_filter", void 0);
__decorate([
    state()
], UmbEntityActionListElement.prototype, "_props", void 0);
__decorate([
    state()
], UmbEntityActionListElement.prototype, "_apiArgs", void 0);
UmbEntityActionListElement = __decorate([
    customElement('umb-entity-action-list')
], UmbEntityActionListElement);
export { UmbEntityActionListElement };
