"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ACCompleter = void 0;
const Autocompletion_js_1 = require("../core/Autocompletion.js");
const autocomplete_1 = require("@codemirror/autocomplete");
function render(completion, _state) {
    let text = completion.apply ? completion.apply : completion.label;
    const el = document.createElement("span");
    el.innerHTML = text;
    completion.label = "";
    return el;
}
/**
 * This class corresponds to the Python MyCompleter class.
 */
class ACCompleter {
    constructor(view) {
        this.completions = [];
        this.view = view;
        const getCompletionsFn = (c) => this.getCompletions(c);
        this.autocompletionExtension = (0, autocomplete_1.autocompletion)({
            activateOnTyping: false,
            defaultKeymap: false,
            interactionDelay: 0,
            closeOnBlur: false,
            override: [getCompletionsFn],
            addToOptions: [{
                    render: render,
                    position: 0
                }]
        });
    }
    selectNext() {
        (0, autocomplete_1.moveCompletionSelection)(true)(this.view);
    }
    selectPrevious() {
        (0, autocomplete_1.moveCompletionSelection)(false)(this.view);
    }
    setCompletions(completions) {
        this.completions = completions;
    }
    getSelected() {
        const ret = (0, autocomplete_1.selectedCompletion)(this.view.state);
        if (ret === null)
            return undefined;
        const idx = (0, autocomplete_1.currentCompletions)(this.view.state).indexOf(ret);
        if (idx < 0)
            return undefined;
        return ret === null ? undefined : this.completions[idx];
    }
    completionPrefix() {
        return this.completions[0].getAlreadyEntered();
    }
    hidePopup() {
        (0, autocomplete_1.closeCompletion)(this.view);
    }
    complete() {
        (0, autocomplete_1.startCompletion)(this.view);
    }
    isActive() {
        return (0, autocomplete_1.completionStatus)(this.view.state) !== null && (0, autocomplete_1.currentCompletions)(this.view.state).length > 1;
    }
    getCompletions(context) {
        let alreadyEnteredLength = undefined;
        const options = this.completions.map((a) => {
            if (alreadyEnteredLength === undefined)
                alreadyEnteredLength = a.getAlreadyEntered().length;
            let apply = a.getCompletion(Autocompletion_js_1.Autocompletion.Purpose.FOR_MENU);
            let completion = apply;
            apply = apply.replaceAll("${", "<b>").replaceAll("}", "</b>");
            // const parsedParams: ParsedParam[] = [];
            // const insertionString: string = ParameterizedCompletion.parseParameters(a, parsedParams);
            // if(parsedParams.length > 0) {
            //     let sb = insertionString;
            //     for(let i = parsedParams.length - 1; i >= 0; i--) {
            //         let param: ParsedParam = parsedParams[i];
            //         sb = sb.substring(0, param.i1) + "</b>" + sb.substring(param.i1);
            //         sb = sb.substring(0, param.i0) + "<b>" + sb.substring(param.i0);
            //     }
            //     apply = sb;
            // }
            if (apply.startsWith("\n") || apply.startsWith("\r"))
                apply = "&lt;new line&gt;";
            if (apply === "")
                apply = "&lt;empty&gt;";
            return { label: completion, apply: apply };
        });
        if (alreadyEnteredLength === undefined)
            alreadyEnteredLength = 0;
        return {
            from: context.pos - alreadyEnteredLength,
            filter: false,
            options: options
        };
    }
}
exports.ACCompleter = ACCompleter;
//# sourceMappingURL=ACCompleter.js.map