import { stream as e } from "../utils/stream.js";
import { cancellation_exports as t } from "../utils/cancellation.js";
import { Deferred as n, interruptAndCheck as r } from "../utils/promise-utils.js";
import { URI as i } from "../../../vscode-uri/lib/esm/index.js";
import { UriUtils as a } from "../utils/uri-utils.js";
//#region ../../node_modules/langium/lib/workspace/workspace-manager.js
var o = class {
	constructor(e) {
		this.initialBuildOptions = {}, this._ready = new n(), this.serviceRegistry = e.ServiceRegistry, this.langiumDocuments = e.workspace.LangiumDocuments, this.documentBuilder = e.workspace.DocumentBuilder, this.fileSystemProvider = e.workspace.FileSystemProvider, this.mutex = e.workspace.WorkspaceLock;
	}
	get ready() {
		return this._ready.promise;
	}
	get workspaceFolders() {
		return this.folders;
	}
	initialize(e) {
		this.folders = e.workspaceFolders ?? void 0;
	}
	initialized(e) {
		return this.mutex.write((e) => this.initializeWorkspace(this.folders ?? [], e));
	}
	async initializeWorkspace(e, n = t.CancellationToken.None) {
		let i = await this.performStartup(e);
		await r(n), await this.documentBuilder.build(i, this.initialBuildOptions, n);
	}
	async performStartup(t) {
		let n = [], r = (e) => {
			n.push(e), this.langiumDocuments.hasDocument(e.uri) || this.langiumDocuments.addDocument(e);
		};
		await this.loadAdditionalDocuments(t, r);
		let i = [];
		await Promise.all(t.map((e) => this.getRootFolder(e)).map(async (e) => this.traverseFolder(e, i)));
		let a = e(i).distinct((e) => e.toString()).filter((e) => !this.langiumDocuments.hasDocument(e));
		return await this.loadWorkspaceDocuments(a, r), this._ready.resolve(), n;
	}
	async loadWorkspaceDocuments(e, t) {
		await Promise.all(e.map(async (e) => {
			t(await this.langiumDocuments.getOrCreateDocument(e));
		}));
	}
	loadAdditionalDocuments(e, t) {
		return Promise.resolve();
	}
	getRootFolder(e) {
		return i.parse(e.uri);
	}
	async traverseFolder(e, t) {
		try {
			let n = await this.fileSystemProvider.readDirectory(e);
			await Promise.all(n.map(async (e) => {
				this.shouldIncludeEntry(e) && (e.isDirectory ? await this.traverseFolder(e.uri, t) : e.isFile && t.push(e.uri));
			}));
		} catch (t) {
			console.error("Failure to read directory content of " + e.toString(!0), t);
		}
	}
	async searchFolder(e) {
		let t = [];
		return await this.traverseFolder(e, t), t;
	}
	shouldIncludeEntry(e) {
		let t = a.basename(e.uri);
		return t.startsWith(".") ? !1 : e.isDirectory ? t !== "node_modules" && t !== "out" : e.isFile ? this.serviceRegistry.hasServices(e.uri) : !1;
	}
};
//#endregion
export { o as DefaultWorkspaceManager };
