import path from 'path';
import vscode from 'vscode';
import { LanguageClient, TransportKind } from 'vscode-languageclient/node';

let client: LanguageClient;

export async function activate(context: vscode.ExtensionContext) {
	const serverExecutable = path.join(context.asAbsolutePath('server'), "nublsp");

	client = new LanguageClient(
		'nub',
		'nub lsp client',
		{
			run: {
				command: serverExecutable,
				transport: TransportKind.stdio,
			},
			debug: {
				command: serverExecutable,
				transport: TransportKind.stdio,
				args: ['--debug'],
			}
		},
		{
			documentSelector: [
				{ scheme: 'file', language: 'nub' },
				{ scheme: 'file', pattern: '**/*.nub' }
			],
			synchronize: {
				fileEvents: vscode.workspace.createFileSystemWatcher('**/.clientrc')
			}
		}
	);

	client.start();
}

export function deactivate(): Thenable<void> | undefined {
	if (!client) {
		return undefined;
	}

	return client.stop();
}