"use strict";
/* --------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 * ------------------------------------------------------------------------------------------ */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaRegistry = exports.SchemaNodeKind = void 0;
var SchemaNodeKind;
(function (SchemaNodeKind) {
    SchemaNodeKind["CATALOG"] = "catalog";
    SchemaNodeKind["DATABASE"] = "database";
    SchemaNodeKind["TABLE"] = "table";
    SchemaNodeKind["VIEW"] = "view";
    SchemaNodeKind["COLUMN"] = "column";
})(SchemaNodeKind = exports.SchemaNodeKind || (exports.SchemaNodeKind = {}));
class SchemaRegistry {
    constructor() {
        this._schemaNodes = [];
    }
    getSchemas(kind) {
        return kind ? this._schemaNodes.filter(n => n.kind === kind) : this._schemaNodes;
    }
    getSchemaCatalogs() {
        return this._schemaNodes.filter(n => n.kind === SchemaNodeKind.CATALOG);
    }
    getSchemaDatabases(schema = {}) {
        return this._schemaNodes.filter(n => n.kind === SchemaNodeKind.DATABASE && (schema.catalog === undefined || n.catalog === schema.catalog));
    }
    getSchemaTables(schema = {}) {
        return this._schemaNodes.filter(n => n.kind === SchemaNodeKind.TABLE &&
            (schema.catalog === undefined || n.catalog === schema.catalog) &&
            (schema.database === undefined || n.database === schema.database));
    }
    getSchemaColumns(schema = {}) {
        return this._schemaNodes.filter(n => n.kind === SchemaNodeKind.COLUMN &&
            (schema.catalog === undefined || n.catalog === schema.catalog) &&
            (schema.database === undefined || n.database === schema.database) &&
            (schema.table === undefined || n.table === schema.table));
    }
    getSchemaViews(schema = {}) {
        return this._schemaNodes.filter(n => n.kind === SchemaNodeKind.VIEW &&
            (schema.catalog === undefined || n.catalog === schema.catalog) &&
            (schema.database === undefined || n.database === schema.database));
    }
    register(schemas) {
        if (schemas.catalogs?.length) {
            this.registerCatalogs(...schemas.catalogs);
        }
        if (schemas.databases?.length) {
            this.registerDatabases(...schemas.databases);
        }
        if (schemas.tables?.length) {
            this.registerTables(...schemas.tables);
        }
        if (schemas.columns?.length) {
            this.registerColumns(...schemas.columns);
        }
        if (schemas.views?.length) {
            this.registerViews(...schemas.views);
        }
    }
    registerCatalogs(...catalogs) {
        catalogs.forEach(c => {
            const duplicate = this._schemaNodes.find(n => n.kind === SchemaNodeKind.CATALOG && n.label === c.label);
            if (!duplicate) {
                this._schemaNodes.push({
                    kind: SchemaNodeKind.CATALOG,
                    label: c.label,
                    description: c.description
                });
            }
            else {
                duplicate.description = c.description;
            }
        });
    }
    registerDatabases(...databases) {
        databases.forEach(d => {
            const duplicate = this._schemaNodes.find(n => n.kind === SchemaNodeKind.DATABASE && n.label === d.label && n.catalog === d.catalog);
            if (!duplicate) {
                this._schemaNodes.push({
                    kind: SchemaNodeKind.DATABASE,
                    catalog: d.catalog,
                    label: d.label,
                    description: d.description
                });
            }
            else {
                duplicate.description = d.description;
            }
        });
    }
    registerTables(...tables) {
        tables.forEach(t => {
            const duplicate = this._schemaNodes.find(n => n.kind === SchemaNodeKind.TABLE && n.label === t.label && n.catalog === t.catalog && n.database === t.database);
            if (!duplicate) {
                this._schemaNodes.push({
                    kind: SchemaNodeKind.TABLE,
                    catalog: t.catalog,
                    database: t.database,
                    label: t.label,
                    description: t.description
                });
            }
            else {
                duplicate.description = t.description;
            }
        });
    }
    registerColumns(...columns) {
        columns.forEach(c => {
            const duplicate = this._schemaNodes.find(n => n.kind === SchemaNodeKind.COLUMN &&
                n.label === c.label &&
                n.catalog === c.catalog &&
                n.database === c.database &&
                n.table === c.table);
            if (!duplicate) {
                this._schemaNodes.push({
                    kind: SchemaNodeKind.COLUMN,
                    catalog: c.catalog,
                    database: c.database,
                    table: c.table,
                    label: c.label,
                    description: c.description
                });
            }
            else {
                duplicate.description = c.description;
            }
        });
    }
    registerViews(...views) {
        views.forEach(v => {
            const duplicate = this._schemaNodes.find(n => n.kind === SchemaNodeKind.VIEW && n.label === v.label && n.catalog === v.catalog && n.database === v.database);
            if (!duplicate) {
                this._schemaNodes.push({
                    kind: SchemaNodeKind.VIEW,
                    catalog: v.catalog,
                    database: v.database,
                    label: v.label,
                    description: v.description
                });
            }
        });
    }
}
exports.SchemaRegistry = SchemaRegistry;
//# sourceMappingURL=schema-registry.js.map