"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 });
const emmet = __importStar(require("@vscode/emmet-helper"));
const html_1 = require("@volar-plugins/html");
exports.default = () => (context) => {
    const triggerCharacters = {
        // https://docs.emmet.io/abbreviations/syntax/
        triggerCharacters: '>+^*()#.[]$@-{}'.split(''),
    };
    if (!context) {
        return triggerCharacters;
    }
    return {
        ...triggerCharacters,
        isAdditionalCompletion: true,
        async provideCompletionItems(textDocument, position) {
            const syntax = emmet.getEmmetMode(textDocument.languageId === 'vue' ? 'html' : textDocument.languageId);
            if (!syntax)
                return;
            // fix https://github.com/vuejs/language-tools/issues/1329
            if (syntax === 'html') {
                const htmlDocument = (0, html_1.getHtmlDocument)(textDocument);
                const node = htmlDocument.findNodeAt(textDocument.offsetAt(position));
                if (node.tag) {
                    let insideBlock = false;
                    if (node.startTagEnd !== undefined && node.endTagStart !== undefined) {
                        insideBlock = textDocument.offsetAt(position) >= node.startTagEnd && textDocument.offsetAt(position) <= node.endTagStart;
                    }
                    if (!insideBlock) {
                        return;
                    }
                }
            }
            // monkey fix https://github.com/johnsoncodehk/volar/issues/1105
            if (syntax === 'jsx')
                return;
            const emmetConfig = await getEmmetConfig(syntax);
            return emmet.doComplete(textDocument, position, syntax, emmetConfig);
        },
    };
    async function getEmmetConfig(syntax) {
        const emmetConfig = await context?.configurationHost?.getConfiguration('emmet') ?? {};
        const syntaxProfiles = Object.assign({}, emmetConfig['syntaxProfiles'] || {});
        const preferences = Object.assign({}, emmetConfig['preferences'] || {});
        // jsx, xml and xsl syntaxes need to have self closing tags unless otherwise configured by user
        if (syntax === 'jsx' || syntax === 'xml' || syntax === 'xsl') {
            syntaxProfiles[syntax] = syntaxProfiles[syntax] || {};
            if (typeof syntaxProfiles[syntax] === 'object'
                && !syntaxProfiles[syntax].hasOwnProperty('self_closing_tag') // Old Emmet format
                && !syntaxProfiles[syntax].hasOwnProperty('selfClosingStyle') // Emmet 2.0 format
            ) {
                syntaxProfiles[syntax] = {
                    ...syntaxProfiles[syntax],
                    selfClosingStyle: 'xml'
                };
            }
        }
        return {
            preferences,
            showExpandedAbbreviation: emmetConfig['showExpandedAbbreviation'],
            showAbbreviationSuggestions: emmetConfig['showAbbreviationSuggestions'],
            syntaxProfiles,
            variables: emmetConfig['variables'],
            excludeLanguages: emmetConfig['excludeLanguages'],
            showSuggestionsAsSnippets: emmetConfig['showSuggestionsAsSnippets']
        };
    }
};
//# sourceMappingURL=index.js.map