"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.register = void 0;
const PConst = __importStar(require("../protocol.const"));
const vscode = __importStar(require("vscode-languageserver-protocol"));
const modifiers_1 = require("../utils/modifiers");
const shared_1 = require("../shared");
const getSymbolKind = (kind) => {
    switch (kind) {
        case PConst.Kind.module: return vscode.SymbolKind.Module;
        case PConst.Kind.class: return vscode.SymbolKind.Class;
        case PConst.Kind.enum: return vscode.SymbolKind.Enum;
        case PConst.Kind.interface: return vscode.SymbolKind.Interface;
        case PConst.Kind.method: return vscode.SymbolKind.Method;
        case PConst.Kind.memberVariable: return vscode.SymbolKind.Property;
        case PConst.Kind.memberGetAccessor: return vscode.SymbolKind.Property;
        case PConst.Kind.memberSetAccessor: return vscode.SymbolKind.Property;
        case PConst.Kind.variable: return vscode.SymbolKind.Variable;
        case PConst.Kind.const: return vscode.SymbolKind.Variable;
        case PConst.Kind.localVariable: return vscode.SymbolKind.Variable;
        case PConst.Kind.function: return vscode.SymbolKind.Function;
        case PConst.Kind.localFunction: return vscode.SymbolKind.Function;
        case PConst.Kind.constructSignature: return vscode.SymbolKind.Constructor;
        case PConst.Kind.constructorImplementation: return vscode.SymbolKind.Constructor;
    }
    return vscode.SymbolKind.Variable;
};
function register(ctx) {
    return (uri) => {
        const document = ctx.getTextDocument(uri);
        if (!document)
            return [];
        const fileName = ctx.uriToFileName(document.uri);
        const barItems = (0, shared_1.safeCall)(() => ctx.typescript.languageService.getNavigationTree(fileName));
        if (!barItems)
            return [];
        // The root represents the file. Ignore this when showing in the UI
        const result = barItems.childItems
            ?.map(function convertNavTree(item) {
            if (!shouldIncludeEntry(item)) {
                return [];
            }
            let remain = item.childItems ?? [];
            return item.spans.map(span => {
                const childItems = [];
                remain = remain.filter(child => {
                    const childStart = child.spans[0].start;
                    const childEnd = child.spans[child.spans.length - 1].start + child.spans[child.spans.length - 1].length;
                    if (childStart >= span.start && childEnd <= span.start + span.length) {
                        childItems.push(child);
                        return false;
                    }
                    return true;
                });
                const nameSpan = item.spans.length === 1
                    ? (item.nameSpan ?? span)
                    : span;
                const fullRange = {
                    start: Math.min(span.start, nameSpan.start),
                    end: Math.max(span.start + span.length, nameSpan.start + nameSpan.length),
                };
                const symbol = vscode.DocumentSymbol.create(item.text, undefined, getSymbolKind(item.kind), vscode.Range.create(document.positionAt(fullRange.start), document.positionAt(fullRange.end)), vscode.Range.create(document.positionAt(nameSpan.start), document.positionAt(nameSpan.start + nameSpan.length)), childItems.map(convertNavTree).flat());
                const kindModifiers = (0, modifiers_1.parseKindModifier)(item.kindModifiers);
                if (kindModifiers.has(PConst.KindModifiers.deprecated)) {
                    symbol.deprecated = true;
                    symbol.tags ??= [];
                    symbol.tags.push(vscode.SymbolTag.Deprecated);
                }
                return symbol;
            });
        })
            .flat();
        return result ?? [];
        function shouldIncludeEntry(item) {
            if (item.kind === PConst.Kind.alias) {
                return false;
            }
            return !!(item.text && item.text !== '<function>' && item.text !== '<class>');
        }
    };
}
exports.register = register;
//# sourceMappingURL=documentSymbol.js.map