import { html, nothing } from 'lit';
import { live } from 'lit/directives/live.js';
import { ModelingElement } from './ModelingElement.js';
import { cancelEvent } from '@api-client/core/lib/events/Utils.js';
import { renderHelp } from '../../help/render.js';
/**
 * An UI element that renders an editor for a DomainNamespace.
 */
export default class DomainNamespaceEditor extends ModelingElement {
    domainElement;
    willUpdate(cp) {
        if (cp.has('selected') || cp.has('root') || cp.has('namespace')) {
            this.computeNamespace();
        }
        super.willUpdate(cp);
    }
    computeNamespace() {
        const { root, selected, namespace } = this;
        if (!root || !selected) {
            this.domainElement = undefined;
            return;
        }
        this.domainElement = root.findNamespace(selected, namespace);
    }
    handleValueChange(e) {
        const { domainElement } = this;
        if (!domainElement) {
            return;
        }
        const node = e.target;
        const { name, value } = node;
        if (!['name', 'description'].includes(name)) {
            return;
        }
        if (name === 'name' && !value) {
            return;
        }
        domainElement.info[name] = value;
        this.notifyChange();
    }
    render() {
        const { domainElement } = this;
        if (!domainElement) {
            return nothing;
        }
        return html `${this.renderForm(domainElement)}`;
    }
    renderForm(model) {
        const disabled = !this.canEdit;
        return html `
      <form name="data-model" @submit="${cancelEvent}" data-key="${model.key}" class="modeling-form">
        <div class="editor-section">
          <div class="row input-row">
            <ui-outlined-text-field
              class="input"
              name="name"
              .value="${live(model.info.name || '')}"
              label="Namespace name"
              required
              ?disabled="${disabled}"
              @change="${this.handleValueChange}"
            ></ui-outlined-text-field>
            ${renderHelp('namespace', 'name-input')}
          </div>
          <div class="row input-row">
            <ui-outlined-text-field
              class="input"
              name="description"
              .value="${live(model.info.description || '')}"
              label="Description (optional)"
              ?disabled="${disabled}"
              @change="${this.handleValueChange}"
            ></ui-outlined-text-field>
            ${renderHelp('namespace', 'description-input')}
          </div>
        </div>
      </form>
    `;
    }
}
//# sourceMappingURL=DomainNamespaceEditor.js.map