import { __decorate } from "tslib";
import { html } from 'lit';
import { property, state } from 'lit/decorators.js';
import { DataEntityKind, DataModelKind, DataNamespaceKind, } from '@api-client/core/build/browser.js';
import AppNavigation from '../navigation/AppNavigationElement.js';
import { Events } from '../../events/Events.js';
import '../../define/ui/ui-icon.js';
export default class SchemaDesignNavigation extends AppNavigation {
    connectedCallback() {
        super.connectedCallback();
        this.setAttribute('aria-label', 'Schema model');
    }
    _toggleNsSelector() {
        this._nsSelectorOpened = !this._nsSelectorOpened;
    }
    _commitName(key, kind, name) {
        const { root } = this;
        if (!root) {
            return;
        }
        if (!name) {
            return;
        }
        if (kind === DataNamespaceKind) {
            const object = key === root.key ? root : root.findNamespace(key);
            if (!object) {
                throw new Error(`Invalid state. Namespace not found.`);
            }
            object.info.name = name;
        }
        else if (kind === DataModelKind) {
            const object = root.findDataModel(key);
            if (!object) {
                throw new Error(`Invalid state. Data model not found.`);
            }
            object.info.name = name;
        }
        else if (kind === DataEntityKind) {
            const object = root.definitions.entities.find(e => e.key === key);
            if (!object) {
                throw new Error(`Invalid state. Entity not found.`);
            }
            object.info.name = name;
        }
        else {
            throw new Error(`Invalid state. Unknown kind.`);
        }
        this.edited = undefined;
        Events.SchemaDesign.changed(this);
        Events.SchemaDesign.State.nameChanged(key, kind, this);
    }
    render() {
        const { root } = this;
        if (!root) {
            return html ``;
        }
        const hasChildren = root.items.some(i => [DataNamespaceKind, DataModelKind].includes(i.kind));
        if (!hasChildren) {
            return this.renderEmptyInfo();
        }
        return html `
      <div class="headline">Namespace</div>
      ${this._namespaceTreeTemplate(root)}
    `;
    }
    renderEmptyInfo() {
        return html `
    <div class="empty-view">
      <p class="empty-message">There are no schemas to render.</p>
      <p class="info">Right-click here to create your first data model.</p>
    </div>
    `;
    }
    _namespaceTreeTemplate(root) {
        const namespaces = root.listNamespaces();
        const dataModels = root.listDataModels();
        const contents = [];
        namespaces.forEach(ns => {
            contents.push(this._namespaceItemTemplate(ns));
        });
        dataModels.forEach(ns => {
            contents.push(this._dataModelItemTemplate(ns));
        });
        return this._outerListTemplate(contents);
    }
    _nsChildrenTemplate(current) {
        const namespaces = current.listNamespaces();
        const dataModels = current.listDataModels();
        const contents = [];
        namespaces.forEach(ns => {
            contents.push(this._namespaceItemTemplate(ns));
        });
        dataModels.forEach(ns => {
            contents.push(this._dataModelItemTemplate(ns));
        });
        return this._parentListItemTemplate(current.key, current.kind, current.info.name || 'Unnamed namespace', contents, {
            parentIcon: 'schemaNamespace',
        });
    }
    _namespaceItemTemplate(item) {
        const hasChildren = item.items.some(i => [DataNamespaceKind, DataModelKind].includes(i.kind));
        if (hasChildren) {
            return this._nsChildrenTemplate(item);
        }
        const label = item.info.renderLabel;
        const content = this._itemContentTemplate('schemaNamespace', label);
        return this._listItemTemplate(item.key, item.kind, label, content);
    }
    _dataModelItemTemplate(current) {
        const hasChildren = !!current.entities.length;
        const label = current.info.renderLabel;
        if (!hasChildren) {
            const content = this._itemContentTemplate('schemaModel', label);
            return this._listItemTemplate(current.key, current.kind, label, content);
        }
        const entities = current.entities.map(i => this._entityItemTemplate(i));
        return this._parentListItemTemplate(current.key, current.kind, label, entities, {
            parentIcon: 'schemaModel',
        });
    }
    _entityItemTemplate(item) {
        const label = item.info.renderLabel;
        const content = this._itemContentTemplate('schemaEntity', label);
        return this._listItemTemplate(item.key, item.kind, label, content, {
            draggable: true,
        });
    }
}
__decorate([
    property({ type: Object })
], SchemaDesignNavigation.prototype, "root", void 0);
__decorate([
    property({ type: String })
], SchemaDesignNavigation.prototype, "parentNamespace", void 0);
__decorate([
    state()
], SchemaDesignNavigation.prototype, "_nsSelectorOpened", void 0);
//# sourceMappingURL=SchemaDesignNavigation.js.map