"use strict";
// This file is adapted from https://github.com/James-Yu/LaTeX-Workshop/blob/master/src/providers/completion.ts
// Original license below:
//////////////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2016 James Yu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
Object.defineProperty(exports, "__esModule", { value: true });
exports.Completer = void 0;
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//////////////////////////////////////////////////////////////////////////////////////////
const vscode = require("vscode");
const citation_1 = require("./completer/citation");
const crossref_1 = require("./completer/crossref");
class Completer {
    constructor(extension) {
        this.extension = extension;
        this.citation = new citation_1.Citation(extension);
        this.crossref = new crossref_1.Crossref(extension);
    }
    provideCompletionItems(document, position, _token, context) {
        const invoker = document.lineAt(position.line).text[position.character - 1];
        // Some user-configs will execute this function for each typed character. Terminate those calls early.
        if (invoker !== '@') {
            return;
        }
        const line = document.lineAt(position.line).text.substr(0, position.character).trim().split(" ");
        const trigger = line[line.length - 1];
        const suggestions = this.completion(trigger).concat(this.completionCrossref(trigger, document));
        this.extension.log(`Showing ${suggestions.length} suggestions`);
        if (suggestions.length > 0) {
            const configuration = vscode.workspace.getConfiguration('PandocCiter');
            if (configuration.get('ViewType') === 'browser') {
                setTimeout(() => this.citation.browser(), 10);
                return;
            }
            // current crossref do not support browser mode
            return suggestions;
        }
        return;
    }
    completion(line) {
        let reg = /(?:^|[ ;\[-])\@([^\]\s]*)/;
        let provider = this.citation;
        const result = line.match(reg);
        let suggestions = [];
        if (result) {
            suggestions = provider.provide();
        }
        return suggestions;
    }
    completionCrossref(line, doc) {
        return this.crossref.provide({ document: doc });
    }
}
exports.Completer = Completer;
//# sourceMappingURL=completion.js.map