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_NAMABLE_WORKSPACE_CONTEXT } from '../../namable/index.js';
import { UmbTextStyles } from '../../../style/index.js';
import { css, html, customElement, state, property } from '../../../../../external/lit/index.js';
import { umbFocus, UmbLitElement } from '../../../lit-element/index.js';
import { UUIInputEvent } from '../../../../../external/uui/index.js';
import { umbBindToValidation } from '../../../validation/index.js';
let UmbWorkspaceHeaderNameEditableElement = class UmbWorkspaceHeaderNameEditableElement extends UmbLitElement {
    #workspaceContext;
    constructor() {
        super();
        /**
         * The readonly state of the inner input.
         * @attr
         */
        this.readonly = false;
        this._name = '';
        this._isWritableName = true;
        this.consumeContext(UMB_NAMABLE_WORKSPACE_CONTEXT, (workspaceContext) => {
            this.#workspaceContext = workspaceContext;
            this.#observeName();
            this.#observeNameWriteGuardRules();
        });
    }
    #observeNameWriteGuardRules() {
        this.observe(this.#workspaceContext?.nameWriteGuard?.isPermittedForName(), (isPermitted) => {
            this._isWritableName = isPermitted ?? true;
        }, 'umbObserveWorkspaceNameWriteGuardRules');
    }
    #observeName() {
        if (!this.#workspaceContext)
            return;
        this.observe(this.#workspaceContext.name, (name) => {
            if (name !== this._name) {
                this._name = name ?? '';
            }
        }, 'observeWorkspaceName');
    }
    #onNameInput(event) {
        if (event instanceof UUIInputEvent) {
            const target = event.composedPath()[0];
            if (typeof target?.value === 'string') {
                this.#workspaceContext?.setName(target.value);
            }
        }
    }
    render() {
        return html `<uui-input
			id="nameInput"
			data-mark="input:workspace-name"
			.value=${this._name}
			@input="${this.#onNameInput}"
			label=${this.label ?? this.localize.term('placeholders_entername')}
			placeholder=${this.placeholder ?? this.localize.term('placeholders_entername')}
			?readonly=${this.readonly || !this._isWritableName}
			required
			${umbBindToValidation(this, `$.name`, this._name)}
			${umbFocus()}></uui-input>`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				display: contents;
			}

			#nameInput {
				flex: 1 1 auto;
			}
		`,
    ]; }
};
__decorate([
    property()
], UmbWorkspaceHeaderNameEditableElement.prototype, "label", void 0);
__decorate([
    property()
], UmbWorkspaceHeaderNameEditableElement.prototype, "placeholder", void 0);
__decorate([
    state()
], UmbWorkspaceHeaderNameEditableElement.prototype, "_name", void 0);
__decorate([
    state()
], UmbWorkspaceHeaderNameEditableElement.prototype, "_isWritableName", void 0);
UmbWorkspaceHeaderNameEditableElement = __decorate([
    customElement('umb-workspace-header-name-editable')
], UmbWorkspaceHeaderNameEditableElement);
export { UmbWorkspaceHeaderNameEditableElement };
