import { __esDecorate, __runInitializers } from "tslib";
import { LitElement } from 'lit';
import { state, property } from 'lit/decorators.js';
import { bound } from '../../decorators/bound.js';
/**
 * The base class for the modeling editors.
 * Contains common properties and logic for all API editors.
 *
 * Each editor has to have the API `model` property, which is the model being edited.
 * Then each class has to implement its own rendering and editing logic.
 *
 * @fires change - An event dispatched when the editor value was changed.
 */
let ApiEditor = (() => {
    let _classSuper = LitElement;
    let _instanceExtraInitializers = [];
    let _readonly_decorators;
    let _readonly_initializers = [];
    let _readonly_extraInitializers = [];
    let _model_decorators;
    let _model_initializers = [];
    let _model_extraInitializers = [];
    let _domainKey_decorators;
    let _domainKey_initializers = [];
    let _domainKey_extraInitializers = [];
    let _domainNamespace_decorators;
    let _domainNamespace_initializers = [];
    let _domainNamespace_extraInitializers = [];
    let _dirty_decorators;
    let _dirty_initializers = [];
    let _dirty_extraInitializers = [];
    let _handleModelChange_decorators;
    return class ApiEditor extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _readonly_decorators = [property({ type: Boolean })];
            _model_decorators = [property({ type: Object })];
            _domainKey_decorators = [property({ type: String })];
            _domainNamespace_decorators = [property({ type: String })];
            _dirty_decorators = [state()];
            _handleModelChange_decorators = [bound];
            __esDecorate(this, null, _readonly_decorators, { kind: "accessor", name: "readonly", static: false, private: false, access: { has: obj => "readonly" in obj, get: obj => obj.readonly, set: (obj, value) => { obj.readonly = value; } }, metadata: _metadata }, _readonly_initializers, _readonly_extraInitializers);
            __esDecorate(this, null, _model_decorators, { kind: "accessor", name: "model", static: false, private: false, access: { has: obj => "model" in obj, get: obj => obj.model, set: (obj, value) => { obj.model = value; } }, metadata: _metadata }, _model_initializers, _model_extraInitializers);
            __esDecorate(this, null, _domainKey_decorators, { kind: "accessor", name: "domainKey", static: false, private: false, access: { has: obj => "domainKey" in obj, get: obj => obj.domainKey, set: (obj, value) => { obj.domainKey = value; } }, metadata: _metadata }, _domainKey_initializers, _domainKey_extraInitializers);
            __esDecorate(this, null, _domainNamespace_decorators, { kind: "accessor", name: "domainNamespace", static: false, private: false, access: { has: obj => "domainNamespace" in obj, get: obj => obj.domainNamespace, set: (obj, value) => { obj.domainNamespace = value; } }, metadata: _metadata }, _domainNamespace_initializers, _domainNamespace_extraInitializers);
            __esDecorate(this, null, _dirty_decorators, { kind: "accessor", name: "dirty", static: false, private: false, access: { has: obj => "dirty" in obj, get: obj => obj.dirty, set: (obj, value) => { obj.dirty = value; } }, metadata: _metadata }, _dirty_initializers, _dirty_extraInitializers);
            __esDecorate(this, null, _handleModelChange_decorators, { kind: "method", name: "handleModelChange", static: false, private: false, access: { has: obj => "handleModelChange" in obj, get: obj => obj.handleModelChange }, metadata: _metadata }, null, _instanceExtraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #readonly_accessor_storage = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _readonly_initializers, void 0));
        /**
         * If true, the editor is in read-only mode.
         * @attribute {boolean} readonly
         */
        get readonly() { return this.#readonly_accessor_storage; }
        set readonly(value) { this.#readonly_accessor_storage = value; }
        #model_accessor_storage = (__runInitializers(this, _readonly_extraInitializers), __runInitializers(this, _model_initializers, void 0));
        /**
         * The API model being edited.
         * @attribute {object} model
         */
        get model() { return this.#model_accessor_storage; }
        set model(value) { this.#model_accessor_storage = value; }
        #domainKey_accessor_storage = (__runInitializers(this, _model_extraInitializers), __runInitializers(this, _domainKey_initializers, void 0));
        /**
         * The ID of the node in the model graph that is being edited.
         * @attribute {string} domainKey
         */
        get domainKey() { return this.#domainKey_accessor_storage; }
        set domainKey(value) { this.#domainKey_accessor_storage = value; }
        #domainNamespace_accessor_storage = (__runInitializers(this, _domainKey_extraInitializers), __runInitializers(this, _domainNamespace_initializers, void 0));
        /**
         * The namespace key of the node in the model graph that is being edited.
         * To be used only when the edited node comes from a specific domain.
         * @attribute {string} domainNamespace
         */
        get domainNamespace() { return this.#domainNamespace_accessor_storage; }
        set domainNamespace(value) { this.#domainNamespace_accessor_storage = value; }
        #dirty_accessor_storage = (__runInitializers(this, _domainNamespace_extraInitializers), __runInitializers(this, _dirty_initializers, void 0));
        /**
         * If true, the editor has unsaved changes.
         * This is a general purpose property that can be used by the editor
         * to indicate that the model was changed but not saved.
         * @attribute {boolean} dirty
         */
        get dirty() { return this.#dirty_accessor_storage; }
        set dirty(value) { this.#dirty_accessor_storage = value; }
        constructor() {
            super();
            __runInitializers(this, _dirty_extraInitializers);
            this.readonly = false;
            this.dirty = false;
        }
        /**
         * A common way for an element to notify a change.
         */
        notifyChange() {
            this.dispatchEvent(new Event('change'));
        }
        willUpdate(cp) {
            if (cp.has('model')) {
                const old = cp.get('model');
                if (old) {
                    old.removeEventListener('change', this.handleModelChange);
                }
                this.model?.addEventListener('change', this.handleModelChange);
            }
            if (cp.has('model') || cp.has('domainKey') || cp.has('domainNamespace')) {
                this.setupModel(this.model, this.domainKey, this.domainNamespace);
            }
            super.willUpdate(cp);
        }
        handleModelChange() {
            this.requestUpdate();
        }
    };
})();
export { ApiEditor };
//# sourceMappingURL=ApiEditor.js.map