import { workspace, ExtensionContext, window } from 'vscode';
import { LanguageClient, TransportKind } from 'vscode-languageclient/node';

let client: LanguageClient;

export function activate(context: ExtensionContext) {
	const outputChannel = window.createOutputChannel('My Language Server');
	outputChannel.appendLine('Extension activating...');

	const serverExecutable = '/home/oliste/repos/nub-lang/compiler/NubLang.LSP/bin/Debug/net9.0/NubLang.LSP';

	outputChannel.appendLine(`Server path: ${serverExecutable}`);

	client = new LanguageClient(
		'nub-lang',
		'nub lang',
		{
			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: workspace.createFileSystemWatcher('**/.clientrc')
			}
		}
	);

	client.start();
}

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