import * as vscode from 'vscode';
import { MergeRequestModel } from '../coding/mergeRequestModel';
import { MergeRequestNode } from './nodes/mergeRequestNode';
import { TreeNode } from './nodes/treeNode';

const PROVIDER_ID = 'coding.views.merge-request';

export class MergeRequestsTreeDataProvider implements vscode.TreeDataProvider<vscode.TreeItem>, vscode.Disposable {
	static id = PROVIDER_ID;
	// private _disposables: vscode.Disposable[] = []; // 待实现 Listener 后使用
	private _view: vscode.TreeView<vscode.TreeItem>;

	constructor(private _mergeRequestModel: MergeRequestModel) {
		this._view = vscode.window.createTreeView(PROVIDER_ID, {
			treeDataProvider: this,
		});

		const { mergeId } = this._mergeRequestModel;
		this._view.title = `#${mergeId} merge request 文件变更列表`;
	}

	dispose() {
		// this._disposables.forEach(disposable => disposable.dispose());
	}

	getTreeItem(element: vscode.TreeItem): vscode.TreeItem | Thenable<vscode.TreeItem> {
		return element;
	}

	async getChildren(element?: TreeNode): Promise<TreeNode[] | undefined> {
		if (element) return element.getChildren();

		const children = await new MergeRequestNode(this._mergeRequestModel).getChildren();

		return children;
	}

	resolveTreeItem(item: vscode.TreeItem, _element: vscode.TreeItem): vscode.ProviderResult<vscode.TreeItem> {
		return item;
	}
}
