"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleKindModifiers = exports.register = void 0;
const semver = __importStar(require("semver"));
const vscode = __importStar(require("vscode-languageserver-protocol"));
const getUserPreferences_1 = require("../../configs/getUserPreferences");
const PConst = __importStar(require("../../protocol.const"));
const modifiers_1 = require("../../utils/modifiers");
const shared_1 = require("../../shared");
function register(ctx) {
    const ts = ctx.typescript.module;
    const lt_320 = semver.lt(ts.version, '3.2.0');
    const gte_300 = semver.gte(ts.version, '3.0.0');
    return async (uri, position, options) => {
        const document = ctx.getTextDocument(uri);
        if (!document)
            return;
        const preferences = await (0, getUserPreferences_1.getUserPreferences)(ctx, document);
        const fileName = ctx.uriToFileName(document.uri);
        const offset = document.offsetAt(position);
        const completionContext = (0, shared_1.safeCall)(() => ctx.typescript.languageService.getCompletionsAtPosition(fileName, offset, {
            ...preferences,
            ...options,
        }));
        if (completionContext === undefined)
            return;
        const wordRange = completionContext.optionalReplacementSpan ? vscode.Range.create(document.positionAt(completionContext.optionalReplacementSpan.start), document.positionAt(completionContext.optionalReplacementSpan.start + completionContext.optionalReplacementSpan.length)) : undefined;
        let line = document.getText({
            start: { line: position.line, character: 0 },
            end: { line: position.line + 1, character: 0 },
        });
        if (line.endsWith('\n')) {
            line = line.substring(0, line.length - 1);
        }
        const dotAccessorContext = getDotAccessorContext(document);
        const entries = completionContext.entries
            .map(tsEntry => toVScodeItem(tsEntry, document));
        return {
            isIncomplete: !!completionContext.isIncomplete,
            items: entries,
        };
        function toVScodeItem(tsEntry, document) {
            const item = vscode.CompletionItem.create(tsEntry.name);
            item.kind = convertKind(tsEntry.kind);
            if (tsEntry.source && tsEntry.hasAction) {
                // De-prioritize auto-imports
                // https://github.com/microsoft/vscode/issues/40311
                item.sortText = '\uffff' + tsEntry.sortText;
            }
            else {
                item.sortText = tsEntry.sortText;
            }
            const { sourceDisplay, isSnippet, labelDetails } = tsEntry;
            if (sourceDisplay) {
                item.labelDetails ??= {};
                item.labelDetails.description = ts.displayPartsToString(sourceDisplay);
            }
            if (labelDetails) {
                item.labelDetails ??= {};
                Object.assign(item.labelDetails, labelDetails);
            }
            item.preselect = tsEntry.isRecommended;
            let range = getRangeFromReplacementSpan(tsEntry, document);
            item.commitCharacters = getCommitCharacters(tsEntry, {
                isNewIdentifierLocation: completionContext.isNewIdentifierLocation,
                isInValidCommitCharacterContext: isInValidCommitCharacterContext(document, position),
                enableCallCompletions: true, // TODO: suggest.completeFunctionCalls
            });
            item.insertText = tsEntry.insertText;
            item.insertTextFormat = isSnippet ? vscode.InsertTextFormat.Snippet : vscode.InsertTextFormat.PlainText;
            item.filterText = getFilterText(tsEntry, wordRange, line, tsEntry.insertText);
            if (completionContext?.isMemberCompletion && dotAccessorContext && !isSnippet) {
                item.filterText = dotAccessorContext.text + (item.insertText || item.label);
                if (!range) {
                    const replacementRange = wordRange;
                    if (replacementRange) {
                        range = {
                            inserting: dotAccessorContext.range,
                            replacing: rangeUnion(dotAccessorContext.range, replacementRange),
                        };
                    }
                    else {
                        range = dotAccessorContext.range;
                    }
                    item.insertText = item.filterText;
                }
            }
            handleKindModifiers(item, tsEntry);
            if (!range && wordRange) {
                range = {
                    inserting: vscode.Range.create(wordRange.start, position),
                    replacing: wordRange,
                };
            }
            if (range) {
                if (vscode.Range.is(range)) {
                    item.textEdit = vscode.TextEdit.replace(range, item.insertText || item.label);
                }
                else {
                    item.textEdit = vscode.InsertReplaceEdit.create(item.insertText || item.label, range.inserting, range.replacing);
                }
            }
            return {
                ...item,
                data: {
                    uri,
                    fileName,
                    offset,
                    originalItem: {
                        name: tsEntry.name,
                        source: tsEntry.source,
                        data: tsEntry.data,
                        labelDetails: tsEntry.labelDetails,
                    },
                },
            };
        }
        function getDotAccessorContext(document) {
            let dotAccessorContext;
            if (gte_300) {
                if (!completionContext)
                    return;
                const isMemberCompletion = completionContext.isMemberCompletion;
                if (isMemberCompletion) {
                    const dotMatch = line.slice(0, position.character).match(/\??\.\s*$/) || undefined;
                    if (dotMatch) {
                        const range = vscode.Range.create({ line: position.line, character: position.character - dotMatch[0].length }, position);
                        const text = document.getText(range);
                        dotAccessorContext = { range, text };
                    }
                }
            }
            return dotAccessorContext;
        }
        // from vscode typescript
        function getRangeFromReplacementSpan(tsEntry, document) {
            if (!tsEntry.replacementSpan) {
                return;
            }
            let replaceRange = vscode.Range.create(document.positionAt(tsEntry.replacementSpan.start), document.positionAt(tsEntry.replacementSpan.start + tsEntry.replacementSpan.length));
            // Make sure we only replace a single line at most
            if (replaceRange.start.line !== replaceRange.end.line) {
                replaceRange = vscode.Range.create(replaceRange.start.line, replaceRange.start.character, replaceRange.start.line, document.positionAt(document.offsetAt({ line: replaceRange.start.line + 1, character: 0 }) - 1).character);
            }
            // If TS returns an explicit replacement range, we should use it for both types of completion
            return {
                inserting: replaceRange,
                replacing: replaceRange,
            };
        }
        function getFilterText(tsEntry, wordRange, line, insertText) {
            // Handle private field completions
            if (tsEntry.name.startsWith('#')) {
                const wordStart = wordRange ? line.charAt(wordRange.start.character) : undefined;
                if (insertText) {
                    if (insertText.startsWith('this.#')) {
                        return wordStart === '#' ? insertText : insertText.replace(/^this\.#/, '');
                    }
                    else {
                        return insertText;
                    }
                }
                else {
                    return wordStart === '#' ? undefined : tsEntry.name.replace(/^#/, '');
                }
            }
            // For `this.` completions, generally don't set the filter text since we don't want them to be overly prioritized. #74164
            if (insertText?.startsWith('this.')) {
                return undefined;
            }
            // Handle the case:
            // ```
            // const xyz = { 'ab c': 1 };
            // xyz.ab|
            // ```
            // In which case we want to insert a bracket accessor but should use `.abc` as the filter text instead of
            // the bracketed insert text.
            else if (insertText?.startsWith('[')) {
                return insertText.replace(/^\[['"](.+)[['"]\]$/, '.$1');
            }
            // In all other cases, fallback to using the insertText
            return insertText;
        }
        function convertKind(kind) {
            switch (kind) {
                case PConst.Kind.primitiveType:
                case PConst.Kind.keyword:
                    return vscode.CompletionItemKind.Keyword;
                case PConst.Kind.const:
                case PConst.Kind.let:
                case PConst.Kind.variable:
                case PConst.Kind.localVariable:
                case PConst.Kind.alias:
                case PConst.Kind.parameter:
                    return vscode.CompletionItemKind.Variable;
                case PConst.Kind.memberVariable:
                case PConst.Kind.memberGetAccessor:
                case PConst.Kind.memberSetAccessor:
                    return vscode.CompletionItemKind.Field;
                case PConst.Kind.function:
                case PConst.Kind.localFunction:
                    return vscode.CompletionItemKind.Function;
                case PConst.Kind.method:
                case PConst.Kind.constructSignature:
                case PConst.Kind.callSignature:
                case PConst.Kind.indexSignature:
                    return vscode.CompletionItemKind.Method;
                case PConst.Kind.enum:
                    return vscode.CompletionItemKind.Enum;
                case PConst.Kind.enumMember:
                    return vscode.CompletionItemKind.EnumMember;
                case PConst.Kind.module:
                case PConst.Kind.externalModuleName:
                    return vscode.CompletionItemKind.Module;
                case PConst.Kind.class:
                case PConst.Kind.type:
                    return vscode.CompletionItemKind.Class;
                case PConst.Kind.interface:
                    return vscode.CompletionItemKind.Interface;
                case PConst.Kind.warning:
                    return vscode.CompletionItemKind.Text;
                case PConst.Kind.script:
                    return vscode.CompletionItemKind.File;
                case PConst.Kind.directory:
                    return vscode.CompletionItemKind.Folder;
                case PConst.Kind.string:
                    return vscode.CompletionItemKind.Constant;
                default:
                    return vscode.CompletionItemKind.Property;
            }
        }
        function getCommitCharacters(entry, context) {
            if (entry.kind === PConst.Kind.warning) { // Ambient JS word based suggestion
                return undefined;
            }
            if (context.isNewIdentifierLocation || !context.isInValidCommitCharacterContext) {
                return undefined;
            }
            const commitCharacters = ['.', ',', ';'];
            if (context.enableCallCompletions) {
                commitCharacters.push('(');
            }
            return commitCharacters;
        }
        function isInValidCommitCharacterContext(document, position) {
            if (lt_320) {
                // Workaround for https://github.com/microsoft/TypeScript/issues/27742
                // Only enable dot completions when the previous character is not a dot preceded by whitespace.
                // Prevents incorrectly completing while typing spread operators.
                if (position.character > 1) {
                    const preText = document.getText(vscode.Range.create(position.line, 0, position.line, position.character));
                    return preText.match(/(\s|^)\.$/ig) === null;
                }
            }
            return true;
        }
    };
}
exports.register = register;
function handleKindModifiers(item, tsEntry) {
    if (tsEntry.kindModifiers) {
        const kindModifiers = (0, modifiers_1.parseKindModifier)(tsEntry.kindModifiers);
        if (kindModifiers.has(PConst.KindModifiers.optional)) {
            if (!item.insertText) {
                item.insertText = item.label;
            }
            if (!item.filterText) {
                item.filterText = item.label;
            }
            item.label += '?';
        }
        if (kindModifiers.has(PConst.KindModifiers.deprecated)) {
            item.tags = [vscode.CompletionItemTag.Deprecated];
        }
        if (kindModifiers.has(PConst.KindModifiers.color)) {
            item.kind = vscode.CompletionItemKind.Color;
        }
        if (tsEntry.kind === PConst.Kind.script) {
            for (const extModifier of PConst.KindModifiers.fileExtensionKindModifiers) {
                if (kindModifiers.has(extModifier)) {
                    if (tsEntry.name.toLowerCase().endsWith(extModifier)) {
                        item.detail = tsEntry.name;
                    }
                    else {
                        item.detail = tsEntry.name + extModifier;
                    }
                    break;
                }
            }
        }
    }
}
exports.handleKindModifiers = handleKindModifiers;
function rangeUnion(a, b) {
    const start = (a.start.line < b.start.line || (a.start.line === b.start.line && a.start.character < b.start.character)) ? a.start : b.start;
    const end = (a.end.line > b.end.line || (a.end.line === b.end.line && a.end.character > b.end.character)) ? a.end : b.end;
    return { start, end };
}
//# sourceMappingURL=basic.js.map