"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqlTemplateAutocomplete = void 0;
const tslib_1 = require("tslib");
const tsserverlibrary_1 = tslib_1.__importDefault(require("typescript/lib/tsserverlibrary"));
class SqlTemplateAutocomplete {
    constructor(db_info_search_index, db_info) {
        this.db_info_search_index = db_info_search_index;
        this.db_info = db_info;
    }
    getCompletionEntryDetails({}, {}, name) {
        const documentation = this.db_info_search_index.search(name).map(({ ref }) => {
            const db_info_item = this.db_info[ref];
            return {
                kind: tsserverlibrary_1.default.ScriptElementKind.keyword,
                text: `
            schema: ${db_info_item.schema},
            table: ${db_info_item.table},
            column: ${db_info_item.column}
          `.replace(/          /gm, "    "),
            };
        });
        return {
            name,
            displayParts: [],
            kind: tsserverlibrary_1.default.ScriptElementKind.keyword,
            kindModifiers: "ts-sql-plugin",
            documentation,
        };
    }
    getCompletionsAtPosition(context, position) {
        var _a;
        const entries = new Map();
        const query = (_a = context.text
            .split(/\n/g)[position.line].slice(0, position.character)
            .match(/[\s\.]+([\w]*)$/)) === null || _a === void 0 ? void 0 : _a[1];
        if (query) {
            this.db_info_search_index.search(`*${query}*`).forEach(({ ref }) => {
                const db_info_item = this.db_info[ref];
                if (db_info_item.schema.match(query)) {
                    entries.set(`schema|${db_info_item.schema}`, {
                        name: db_info_item.schema,
                        kind: tsserverlibrary_1.default.ScriptElementKind.keyword,
                        sortText: `3 - ${db_info_item.schema}`,
                    });
                }
                if (db_info_item.table.match(query)) {
                    entries.set(`table|${db_info_item.table}`, {
                        name: db_info_item.table,
                        kind: tsserverlibrary_1.default.ScriptElementKind.string,
                        sortText: `2 - ${db_info_item.table}`,
                    });
                }
                if (db_info_item.column.match(query)) {
                    entries.set(`column|${db_info_item.column}`, {
                        name: db_info_item.column,
                        kind: tsserverlibrary_1.default.ScriptElementKind.constElement,
                        sortText: `1 - ${db_info_item.column}`,
                    });
                }
            });
        }
        return {
            isGlobalCompletion: false,
            isMemberCompletion: false,
            isNewIdentifierLocation: false,
            entries: Array.from(entries.values()),
        };
    }
}
exports.SqlTemplateAutocomplete = SqlTemplateAutocomplete;
//# sourceMappingURL=autocomplete.js.map