"use strict";

const https = require('https');
const pjson = require('../package.json');

import Completers from "./classes/Completers";
import EkoProject from './classes/EkoProject';

import {
    createConnection,
    TextDocuments,
    ProposedFeatures,
    CompletionItem,
    TextDocumentPositionParams,
    TextDocumentChangeEvent
} from "vscode-languageserver/lib/main";

const Sentry = require('@sentry/node');
Sentry.init({ dsn: 'https://f298f9ce12e34065a3df8785863a4ecc@sentry.io/33357' });

export let connection = createConnection(ProposedFeatures.all);

let documents: TextDocuments = new TextDocuments();
let completers: Completers = new Completers;

completers.init().then(() => {
    let ekoProject: EkoProject = new EkoProject;
    let projectPath: string = null;
    let apiCompletions: any = null;
    let documentUri:string = null;
    
    documents.onDidChangeContent((e: TextDocumentChangeEvent) => {
        documentUri = e.document.uri;
        projectPath = ekoProject.getPath(documentUri);

        if (projectPath) {
            completers.setPath(projectPath);
        }
        
    });
    
    function fetchDeclarationFile() {
        const endpoint = pjson['declaration-file-endpoint']['production'];
        
        https.get(endpoint, (res: any) => {
            connection.console.info(`Fetching declarations file...`);
            let data = '';
            
            res.on('data', (chunk: any) => {
                data += chunk;
            });
            
            res.on('end', () => {
                connection.console.info(`Successfully fetched declarations file`);
                apiCompletions = data;
<<<<<<< HEAD
                if (apiCompletions && projectPath) {
                    ekoProject.addAPICompletions(documentUri, apiCompletions);
                } else {
                    connection.console.info(`No Eko project detected`);
=======
                if (apiCompletions) {
                    connection.console.info(`Successfully fetched declarations file`);
                    ekoProject.addAPICompletions(documentUri, apiCompletions);
>>>>>>> c9d3ed7f... Eko Autocompleter: updating existing declarations file
                }
            });
        }).on("error", (err: any) => {
            connection.console.error(`Failed to fetch declarations file: ${err}`);
            Sentry.captureException(err);
        });

    };

    connection.onInitialize(() => {

        fetchDeclarationFile();

        return {
            capabilities: {
                completionProvider: {
                    resolveProvider: false
                }
            }
        };
    });

    connection.onCompletion(
        (_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
            if (projectPath) {
                return completers.getItems(_textDocumentPosition);
            }
        }
    );

    documents.listen(connection);
    connection.listen();

}).catch((err) => {
    Sentry.captureException(err);
});



