"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const html_1 = __importDefault(require("@volar-plugins/html"));
const language_service_1 = require("@volar/language-service");
const pug = __importStar(require("./languageService"));
exports.default = () => (context) => {
    if (!context) {
        return {};
    }
    const pugDocuments = new WeakMap();
    const htmlPlugin = (0, html_1.default)()(context);
    const pugLs = pug.getLanguageService(htmlPlugin.getHtmlLs());
    return {
        ...htmlPlugin,
        getPugLs: () => pugLs,
        getPugDocument,
        resolveRuleContext(context) {
            worker(context.document, (pugDocument) => {
                if (pugDocument.ast) {
                    context.pug = {
                        rootNode: pugDocument.ast,
                        languageService: pugLs,
                    };
                }
            });
            return context;
        },
        provideCompletionItems(document, position, _) {
            return worker(document, (pugDocument) => {
                return pugLs.doComplete(pugDocument, position, context.documentContext);
            });
        },
        provideSyntacticDiagnostics(document) {
            return worker(document, (pugDocument) => {
                if (pugDocument.error) {
                    return [{
                            source: 'pug',
                            code: pugDocument.error.code,
                            message: pugDocument.error.msg,
                            range: {
                                start: { line: pugDocument.error.line, character: pugDocument.error.column },
                                end: { line: pugDocument.error.line, character: pugDocument.error.column },
                            },
                        }];
                }
                return [];
            });
        },
        provideHover(document, position) {
            return worker(document, async (pugDocument) => {
                const hoverSettings = await context.configurationHost?.getConfiguration('html.hover');
                return pugLs.doHover(pugDocument, position, hoverSettings);
            });
        },
        provideDocumentHighlights(document, position) {
            return worker(document, (pugDocument) => {
                return pugLs.findDocumentHighlights(pugDocument, position);
            });
        },
        provideDocumentLinks(document) {
            return worker(document, (pugDocument) => {
                if (context.documentContext) {
                    return pugLs.findDocumentLinks(pugDocument, context.documentContext);
                }
            });
        },
        provideDocumentSymbols(document, token) {
            return worker(document, async (pugDoc) => {
                const htmlResult = await htmlPlugin.provideDocumentSymbols?.(pugDoc.map.virtualFileDocument, token) ?? [];
                const pugResult = htmlResult.map(htmlSymbol => language_service_1.transformer.asDocumentSymbol(htmlSymbol, range => pugDoc.map.toSourceRange(range))).filter((symbol) => symbol !== undefined);
                return pugResult;
            });
        },
        provideFoldingRanges(document) {
            return worker(document, (pugDocument) => {
                return pugLs.getFoldingRanges(pugDocument);
            });
        },
        provideSelectionRanges(document, positions) {
            return worker(document, (pugDocument) => {
                return pugLs.getSelectionRanges(pugDocument, positions);
            });
        },
        async provideAutoInsertionEdit(document, position, insertContext) {
            return worker(document, async (pugDocument) => {
                const lastCharacter = insertContext.lastChange.text[insertContext.lastChange.text.length - 1];
                if (insertContext.lastChange.rangeLength === 0 && lastCharacter === '=') {
                    const enabled = (await context.configurationHost?.getConfiguration('html.autoCreateQuotes')) ?? true;
                    if (enabled) {
                        const text = pugLs.doQuoteComplete(pugDocument, position, await context.configurationHost?.getConfiguration('html.completion'));
                        if (text) {
                            return text;
                        }
                    }
                }
            });
        },
    };
    function worker(document, callback) {
        const pugDocument = getPugDocument(document);
        if (!pugDocument)
            return;
        return callback(pugDocument);
    }
    function getPugDocument(document) {
        if (document.languageId !== 'jade')
            return;
        const cache = pugDocuments.get(document);
        if (cache) {
            const [cacheVersion, cacheDoc] = cache;
            if (cacheVersion === document.version) {
                return cacheDoc;
            }
        }
        const doc = pugLs.parsePugDocument(document.getText());
        pugDocuments.set(document, [document.version, doc]);
        return doc;
    }
};
//# sourceMappingURL=index.js.map