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 { UMB_ENTITY_DETAIL_WORKSPACE_CONTEXT } from '../entity-detail-workspace.context-token.js';
import { UmbLitElement } from '../../../lit-element/index.js';
import { css, customElement, html, ifDefined, nothing, property, state } from '../../../../../external/lit/index.js';
import { UMB_ACTION_EVENT_CONTEXT } from '../../../action/index.js';
import { UmbEntityDeletedEvent } from '../../../entity-action/index.js';
let UmbEntityDetailWorkspaceEditorElement = class UmbEntityDetailWorkspaceEditorElement extends UmbLitElement {
    #context;
    #eventContext;
    #unique;
    constructor() {
        super();
        this._isLoading = false;
        this._isForbidden = false;
        this._exists = false;
        this._isNew = false;
        this.#onEntityDeletedEvent = (event) => {
            // Ignore events if we don't have a unique identifier (e.g., new unsaved entity)
            if (!this.#unique)
                return;
            if (event.getEntityType() !== this._entityType)
                return;
            if (event.getUnique() !== this.#unique)
                return;
            // Navigate to the back path when the entity is deleted
            if (this.backPath) {
                history.pushState({}, '', this.backPath);
            }
        };
        this.consumeContext(UMB_ENTITY_DETAIL_WORKSPACE_CONTEXT, (context) => {
            this.#context = context;
            this.observe(this.#context?.entityType, (entityType) => (this._entityType = entityType));
            this.observe(this.#context?.loading.isOn, (isLoading) => (this._isLoading = isLoading ?? false));
            this.observe(this.#context?.forbidden.isOn, (isForbidden) => (this._isForbidden = isForbidden ?? false));
            this.observe(this.#context?.data, (data) => (this._exists = !!data));
            this.observe(this.#context?.isNew, (isNew) => (this._isNew = isNew));
            this.observe(this.#context?.unique, (unique) => (this.#unique = unique));
        });
        this.consumeContext(UMB_ACTION_EVENT_CONTEXT, (context) => {
            this.#eventContext?.removeEventListener(UmbEntityDeletedEvent.TYPE, this.#onEntityDeletedEvent);
            this.#eventContext = context;
            this.#eventContext?.addEventListener(UmbEntityDeletedEvent.TYPE, this.#onEntityDeletedEvent);
        });
    }
    #onEntityDeletedEvent;
    #renderForbidden() {
        if (!this._isLoading && this._isForbidden) {
            return html `<umb-entity-detail-forbidden
				entity-type=${ifDefined(this._entityType)}></umb-entity-detail-forbidden>`;
        }
        return nothing;
    }
    #renderNotFound() {
        if (!this._isLoading && !this._exists) {
            return html `<umb-entity-detail-not-found
				entity-type=${ifDefined(this._entityType)}></umb-entity-detail-not-found>`;
        }
        return nothing;
    }
    render() {
        return html ` ${this.#renderForbidden()} ${this.#renderNotFound()}

			<!-- TODO: It is currently on purpose that the workspace editor is always in the DOM, even when it doesn't have data.
			 We currently rely on the entity actions to be available to execute, and we ran into an issue when the entity got deleted; then the DOM got cleared, and the delete action couldn't complete.
			 We need to look into loading the entity actions in the workspace context instead so we don't rely on the DOM.
		 -->
			<umb-workspace-editor
				?loading=${this._isLoading}
				.backPath=${this.backPath}
				class="${this._exists === false ? 'hide' : ''}">
				<slot name="header" slot="header"></slot>
				${this.#renderEntityActions()}
				<slot></slot>
			</umb-workspace-editor>`;
    }
    #renderEntityActions() {
        if (this._isNew)
            return nothing;
        return html `<umb-workspace-entity-action-menu
			slot="action-menu"
			data-mark="workspace:action-menu"></umb-workspace-entity-action-menu>`;
    }
    destroy() {
        this.#eventContext?.removeEventListener(UmbEntityDeletedEvent.TYPE, this.#onEntityDeletedEvent);
        super.destroy();
    }
    static { this.styles = [
        css `
			umb-workspace-editor {
				visibility: visible;
			}

			umb-workspace-editor.hide {
				visibility: hidden;
			}
		`,
    ]; }
};
__decorate([
    property({ attribute: 'back-path' })
], UmbEntityDetailWorkspaceEditorElement.prototype, "backPath", void 0);
__decorate([
    state()
], UmbEntityDetailWorkspaceEditorElement.prototype, "_entityType", void 0);
__decorate([
    state()
], UmbEntityDetailWorkspaceEditorElement.prototype, "_isLoading", void 0);
__decorate([
    state()
], UmbEntityDetailWorkspaceEditorElement.prototype, "_isForbidden", void 0);
__decorate([
    state()
], UmbEntityDetailWorkspaceEditorElement.prototype, "_exists", void 0);
__decorate([
    state()
], UmbEntityDetailWorkspaceEditorElement.prototype, "_isNew", void 0);
UmbEntityDetailWorkspaceEditorElement = __decorate([
    customElement('umb-entity-detail-workspace-editor')
], UmbEntityDetailWorkspaceEditorElement);
export { UmbEntityDetailWorkspaceEditorElement };
