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 { getQuerySnippet } from '../../utils/index.js';
import { UMB_PARTIAL_VIEW_WORKSPACE_CONTEXT } from './partial-view-workspace.context-token.js';
import { css, customElement, html, nothing, query, state } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../../core/lit-element/index.js';
import { umbOpenModal } from '../../../core/modal/index.js';
import { UMB_TEMPLATE_QUERY_BUILDER_MODAL } from '../../templates/index.js';
import { UMB_SERVER_CONTEXT } from '../../../core/server/index.js';
import '../../../code-editor/index.js';
import '../../local-components/insert-menu/index.js';
let UmbPartialViewWorkspaceEditorElement = class UmbPartialViewWorkspaceEditorElement extends UmbLitElement {
    #workspaceContext;
    constructor() {
        super();
        this._content = '';
        /**
         * Whether editing is restricted. True when in production mode OR when runtime mode is still unknown.
         * This ensures a safe default (restricted) until we confirm the runtime mode.
         */
        this._isRestricted = true;
        this.consumeContext(UMB_SERVER_CONTEXT, (context) => {
            this.observe(context?.isProductionMode, (isProductionMode) => {
                // Restricted until we confirm it's NOT production mode (safe default).
                this._isRestricted = isProductionMode !== false;
            });
        });
        this.consumeContext(UMB_PARTIAL_VIEW_WORKSPACE_CONTEXT, (workspaceContext) => {
            this.#workspaceContext = workspaceContext;
            this.observe(this.#workspaceContext?.content, (content) => {
                this._content = content;
            });
            this.observe(this.#workspaceContext?.isNew, (isNew) => {
                this._isNew = isNew;
            });
        });
    }
    #onCodeEditorInput(event) {
        const target = event.target;
        const value = target.code;
        this.#workspaceContext?.setContent(value);
    }
    #insertSnippet(event) {
        const target = event.target;
        const value = target.value;
        this._codeEditor?.insert(value);
    }
    async #openQueryBuilder() {
        const queryBuilderModalValue = await umbOpenModal(this, UMB_TEMPLATE_QUERY_BUILDER_MODAL).catch(() => undefined);
        if (queryBuilderModalValue?.value) {
            this._codeEditor?.insert(getQuerySnippet(queryBuilderModalValue.value));
        }
    }
    render() {
        return html `
			<umb-entity-detail-workspace-editor>
				<umb-workspace-header-name-editable
					slot="header"
					?readonly=${this._isNew === false || this._isRestricted}></umb-workspace-header-name-editable>
				<uui-box>
					<div slot="header" id="code-editor-menu-container">
						<umb-templating-insert-menu
							@insert=${this.#insertSnippet}
							?disabled=${this._isRestricted}
							hidePartialViews></umb-templating-insert-menu>
						<uui-button
							look="secondary"
							id="query-builder-button"
							label=${this.localize.term('template_queryBuilder')}
							?disabled=${this._isRestricted}
							@click=${this.#openQueryBuilder}>
							<uui-icon name="icon-wand"></uui-icon>
							<umb-localize key="template_queryBuilder">Query builder</umb-localize>
						</uui-button>
					</div>
					${this.#renderCodeEditor()}
				</uui-box>
			</umb-entity-detail-workspace-editor>
		`;
    }
    #renderCodeEditor() {
        if (this._content === undefined) {
            return nothing;
        }
        return html `
			<umb-code-editor
				id="content"
				language="razor"
				.code=${this._content ?? ''}
				?readonly=${this._isRestricted}
				@input=${this.#onCodeEditorInput}></umb-code-editor>
		`;
    }
    static { this.styles = [
        css `
			:host {
				display: block;
				width: 100%;
				height: 100%;
			}

			umb-code-editor {
				--editor-height: calc(100dvh - 300px);
			}

			uui-box {
				min-height: calc(100dvh - 300px);
				margin: var(--uui-size-layout-1);
				--uui-box-default-padding: 0;
				/* remove header border bottom as code editor looks better in this box */
				--uui-color-divider-standalone: transparent;
			}

			#code-editor-menu-container uui-icon:not([name='icon-delete']) {
				margin-right: var(--uui-size-space-3);
			}

			#insert-menu {
				margin: 0;
				padding: 0;
				margin-top: var(--uui-size-space-3);
				background-color: var(--uui-color-surface);
				box-shadow: var(--uui-shadow-depth-3);
				min-width: calc(100% + var(--uui-size-8, 24px));
			}

			#insert-menu > li,
			ul {
				padding: 0;
				width: 100%;
				list-style: none;
			}

			.insert-menu-item {
				width: 100%;
			}

			#code-editor-menu-container {
				display: flex;
				justify-content: flex-end;
				gap: var(--uui-size-space-3);
				width: 100%;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbPartialViewWorkspaceEditorElement.prototype, "_content", void 0);
__decorate([
    state()
], UmbPartialViewWorkspaceEditorElement.prototype, "_isNew", void 0);
__decorate([
    state()
], UmbPartialViewWorkspaceEditorElement.prototype, "_isRestricted", void 0);
__decorate([
    query('umb-code-editor')
], UmbPartialViewWorkspaceEditorElement.prototype, "_codeEditor", void 0);
UmbPartialViewWorkspaceEditorElement = __decorate([
    customElement('umb-partial-view-workspace-editor')
], UmbPartialViewWorkspaceEditorElement);
export { UmbPartialViewWorkspaceEditorElement };
export default UmbPartialViewWorkspaceEditorElement;
